classSolution: defcountTriples(self, n: int) -> int: ans = 0 for c inrange(1, n+1): for b inrange(1, c+1): for a inrange(1, b+1): if a * a + b * b == c * c: if a == b: ans += 1 else: ans += 2 return ans
classSolution: defsumGame(self, num: str) -> bool: n = len(num) leftSum = 0 rightSum = 0 left = 0 right = 0 for i inrange(n): if num[i] == '?': if i >= n // 2: right += 1 else: left += 1 else: if i >= n // 2: rightSum += ord(num[i]) - ord('0') else: leftSum += ord(num[i]) - ord('0') total = right + left # print ('total: ', total) if total % 2 == 1: returnTrue alice = True for _ inrange(total): if alice: if left == 0or (right != 0and left > right): # pick right if leftSum <= rightSum or rightSum + 9 > leftSum: rightSum += 9 else: rightSum += 0 right -= 1 elif right == 0or left < right: # pick left if leftSum >= rightSum or leftSum + 9 > rightSum: leftSum += 9 else: leftSum += 0 left -= 1 else: if leftSum == rightSum: returnFalse elif leftSum > rightSum: leftSum += 9 left -= 1 else: rightSum += 9 right -= 1 else: if left == 0or (right != 0and left > right): # pick right if leftSum > rightSum: rightSum += 9if (leftSum - rightSum > 9) else leftSum - rightSum else: rightSum += 0 right -= 1 elif right == 0or left < right: # pick left if leftSum < rightSum: leftSum += 9if (rightSum - leftSum > 9) else rightSum - leftSum else: leftSum += 0 left -= 1 # print (left, leftSum, rightSum, right) alice = not alice return leftSum != rightSum