description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
result = [abs(a[x] + a[y] - k) for x in range(n) for y in range(x + 1, n)]
minimum = min(result)
print(minimum, result.count(minimum)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | t = int(input())
while t > 0:
n, k = map(int, input().split())
l = list(map(int, input().split()))
x = 3 * 10**9
a = []
for i in range(n):
for j in range(i + 1, n):
a.append(abs(l[j] + l[i] - k))
m = min(a)
print(m, a.count(m))
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | def inp():
n, k = input().split()
n = int(n)
k = int(k)
if n >= 2 and n <= 1000:
l = list(map(int, input().split()))
l = l[:n]
le = len(l)
s = []
p = []
i = 0
while i < le:
j = le - 1
while j > i:
s.append(l[i] + l[j])
j = j - 1
i = i + 1
for i in s:
d = k - i
if d < 0:
p.append(-d)
else:
p.append(d)
m = min(p)
c = p.count(m)
print(m, c)
t = int(input())
if t > 0 and t <= 50:
for i in range(0, t):
inp() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | tests = int(input())
for i in range(tests):
n, k = map(int, input().split())
arr = list(map(int, input().split()))
checked_numbers = set()
gap = abs(k - (arr[0] + arr[1]))
freq = {}
for j in range(n):
if arr[j] in freq:
freq[arr[j]] += 1
else:
freq[arr[j]] = 1
if arr[j] not in checked_numbers:
for l in range(j + 1, n):
gap = min(gap, abs(k - (arr[j] + arr[l])))
checked_numbers.add(arr[j])
ans = 0
used_combinations = set()
for j in freq.keys():
possiblities = [k + gap, k - gap]
for required in possiblities:
if (
required - j in freq
and (j, required - j) not in used_combinations
and (required - j, j) not in used_combinations
):
if required - j == j:
ans += freq[j] * (freq[j] - 1) // 2
else:
ans += freq[j] * freq[required - j]
used_combinations.add((j, required - j))
print(gap, ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | t = int(input())
for T in range(t):
n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
val = abs(a[0] + a[1] - k)
c = 0
for i in range(n):
for j in range(i + 1, n):
if val == abs(a[i] + a[j] - k):
c += 1
elif val > abs(a[i] + a[j] - k):
val = abs(a[i] + a[j] - k)
c = 1
else:
continue
print(val, c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | t = int(input())
for e in range(0, t):
n, k = input().strip().split()
n = int(n)
k = int(k)
a = list(map(int, input().strip().split()))
low = float("inf")
count = 0
for i in range(0, n - 1):
for j in range(i + 1, n):
if abs(a[i] + a[j] - k) < low:
low = abs(a[i] + a[j] - k)
count = 1
elif abs(a[i] + a[j] - k) == low:
count += 1
print(low, count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | for _ in range(int(input())):
a, k = map(int, input().split())
result = []
lst = list(map(int, input().split()))
for i in range(len(lst) - 1):
for j in range(i + 1, len(lst)):
a = abs(lst[i] + lst[j] - k)
result.append(a)
print(min(result), result.count(min(result))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | for _ in range(int(input())):
n, k = map(int, input().split())
list1 = list(map(int, input().split()))
list1.sort()
min1 = 1000000000000001
maxsum = 0
for i in range(len(list1)):
for j in range(i + 1, len(list1)):
temp = abs(list1[i] + list1[j] - k)
if temp < min1:
min1 = min(min1, temp)
if min1 != 0:
cand1 = k + min1
cand2 = k - min1
s = list()
cnt = 0
for i in list1:
t1 = cand1 - i
if t1 in s:
cnt += s.count(t1)
t2 = cand2 - i
if t2 in s:
cnt += s.count(t2)
s.append(i)
print(min1, cnt)
else:
cand = k + min1
s = list()
cnt = 0
for i in list1:
t1 = cand - i
if t1 in s:
cnt += s.count(t1)
s.append(i)
print(min1, cnt) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | from sys import stdin, stdout
def sin():
return stdin.readline().rstrip()
def listInput():
return list(map(int, sin().split()))
def printBS(li):
if not li:
return
for i in range(len(li) - 1):
stdout.write("%d " % li[i])
stdout.write("%d\n" % li[-1])
t = int(sin())
for _ in range(t):
n, k = listInput()
mc, mv = 0, 2 * 10**9
li = listInput()
for i in range(n):
for j in range(i + 1, n):
te = int(abs(li[i] + li[j] - k))
if te < mv:
mv, mc = te, 1
elif te == mv:
mc += 1
print(mv, mc) | FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR RETURN FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | for _ in range(int(input())):
n, k = map(int, input().split())
l = list(map(int, input().split()))
d = {}
min = 2**31 - 1
for i in range(n):
for j in range(i + 1, n):
diff = abs(l[i] + l[j] - k)
if min > diff:
min = abs(l[i] + l[j] - k)
if diff in d:
d[diff] = d[diff] + 1
else:
d[diff] = 1
print(min, d[min]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | t = int(input())
while t > 0:
n, k = map(int, input().split())
l = list(map(int, input().split()))
x = 3 * 10**9
c = 0
for i in range(n):
for j in range(i + 1, n):
y = abs(l[j] + l[i] - k)
if y < x:
x = y
for i in range(n):
for j in range(i + 1, n):
if abs(l[j] + l[i] - k) == x:
c += 1
print(x, c)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
You are given an array of N integers a1, a2, ..., aN and an integer K. Find the number of such unordered pairs {i, j} that
- i ≠ j
- |ai + aj - K| is minimal possible
Output the minimal possible value of |ai + aj - K| (where i ≠ j) and the number of such pairs for the given array and the integer K.
-----Input-----
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case consists of two space separated integers - N and K respectively.
The second line contains N single space separated integers - a1, a2, ..., aN respectively.
-----Output-----
For each test case, output a single line containing two single space separated integers - the minimal possible value of |ai + aj - K| and the number of unordered pairs {i, j} for which this minimal difference is reached.
-----Constraints-----
- 1 ≤ T ≤ 50
- 1 ≤ ai, K ≤ 109
- N = 2 - 31 point.
- 2 ≤ N ≤ 1000 - 69 points.
-----Example-----
Input:
1
4 9
4 4 2 6
Output:
1 4
-----Explanation:-----
The minimal possible absolute difference of 1 can be obtained by taking the pairs of a1 and a2, a1 and a4, a2 and a4, a3 and a4. | for _ in range(int(input())):
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
l.sort()
c = 0
mn = abs(l[0] + l[1] - k)
for i in range(n - 1):
for j in range(i + 1, n):
temp = abs(l[i] + l[j] - k)
if temp == mn:
c += 1
elif temp < mn:
mn = temp
c = 1
elif l[i] + l[j] - k > mn:
break
print(mn, c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | n, m = input().split()
n = int(n)
m = int(m)
c = 0
f = not (m == 0 and n == 0)
a = 1
px = 0
py = 0
while f:
co = 0
while co < a:
px += 1
co += 1
if n == px and m == py:
f = False
if not f:
break
c += 1
co = 0
while co < a:
py += 1
co += 1
if n == px and m == py:
f = False
if not f:
break
c += 1
co = 0
while co < a + 1:
px -= 1
co += 1
if n == px and m == py:
f = False
if not f:
break
c += 1
co = 0
while co < a + 1:
py -= 1
co += 1
if n == px and m == py:
f = False
if not f:
break
c += 1
a += 2
print(c) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = list(map(int, input().split()))
c = 0
if y == 0 and (x == 0 or x == 1):
c = 0
elif abs(x) == abs(y) and x > 0 and y > 0:
c = 4 * x - 3
elif abs(x) == abs(y) and x < 0 and y > 0:
c = 4 * y - 2
elif abs(x) == abs(y) and x < 0 and y < 0:
c = -4 * x - 1
elif abs(x) == abs(y) and x > 0 and y < 0:
c = -4 * y
elif x > 0 and y < 0 and y == -(x - 1):
c = -4 * y
elif x > y and abs(x) > abs(y):
c = 4 * x - 3
elif y > x and abs(y) > abs(x):
c = 4 * y - 2
elif y > x and abs(x) > abs(y):
c = -4 * x - 1
elif x > y and abs(x) < abs(y):
c = -4 * y
print(c) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | import time
x, y = map(int, input().split())
movements = [(1, 0), (0, 1), (-1, 0), (0, -1)]
sizeOfSteps = 1
x_0, y_0 = 0, 0
x_1, y_1 = 1, 0
step = 0
change = 1
while not (
(x_0 <= x <= x_1 or x_0 >= x >= x_1) and (y_0 <= y <= y_1 or y_0 >= y >= y_1)
):
step += 1
move_i = movements[step % 4]
if change % 2 == 0:
sizeOfSteps += 1
change += 1
x_0, y_0 = x_1, y_1
x_1 = x_1 + move_i[0] * sizeOfSteps
y_1 = y_1 + move_i[1] * sizeOfSteps
print(step) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
if x == 1 and y == 0:
print(0)
exit()
a = 4 * (x - 1) + 1 if x > 0 else -4 * x - 1 if x < 0 else 0
b = 4 * y - 2 if y > 0 else -4 * y if y < 0 else 0
if abs(x) == abs(y) and y >= x or y < 0 and abs(x) > abs(y):
print(min(a, b))
else:
print(max(a, b)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | m, n = map(int, input().split())
x = 1
y = 0
count = 0
if 0 <= m <= 1 and n == 0:
print(0)
else:
while True:
a1, a2 = x, y
b1, b2 = a1, abs(a2) + 1
if a1 == m and a2 < n <= b2:
count += 1
break
else:
count += 1
c1, c2 = -b1, b2
if c1 <= m < b1 and b2 == n:
count += 1
break
else:
count += 1
d1, d2 = -b1, -b2
if d1 == m and d2 <= n < c2:
count += 1
break
else:
count += 1
x = a1 + 1
y = -(abs(a2) + 1)
if d1 < m <= x and n == y:
count += 1
break
else:
count += 1
if (
a1 <= m <= b1
and b1 <= n <= b2
or c1 <= m <= b1
and c2 <= n <= b2
or c1 <= m <= d1
and d2 <= n <= c2
or d1 <= m <= x
and y <= n <= d2
):
break
print(count) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = list(map(int, input().split()))
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
def onl(x1, y1, x2, y2):
if x1 == x2:
return x == x1 and y in range(min(y1, y2), max(y1, y2) + 1)
elif y1 == y2:
return y == y1 and x in range(min(x1, x2), max(x1, x2) + 1)
def sp(cx, cy, t):
l = t // 2 + 1
nx, ny = cx + dx[t % 4] * l, cy + dy[t % 4] * l
return t if onl(cx, cy, nx, ny) else sp(nx, ny, t + 1)
print(sp(0, 0, 0)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def detquad(x, y):
if x > 0 and y > 0:
return 1
elif x < 0 and y > 0:
return 2
elif x < 0 and y < 0:
return 3
elif x > 0 and y < 0:
return 4
def findprevpoints(x, y):
if x == 0:
return y, y
elif y == 0:
if x > 0:
return x, -(x - 1)
else:
return x, -x
quad = detquad(x, y)
if quad == 1:
if x >= y:
prevx = x
prevy = -(x - 1)
else:
prevy = y
prevx = y
elif quad == 2:
if y < -x:
prevx = x
prevy = -x
else:
prevy = y
prevx = y
elif quad == 3:
if -y <= -x:
prevx = x
prevy = -x
else:
prevx = y
prevy = y
elif quad == 4:
if x <= -y + 1:
prevx = y
prevy = y
else:
prevx = x
prevy - (x - 1)
return prevx, prevy
x, y = map(int, input().split())
if x == 0 and y == 0 or x == 1 and y == 0:
print(0)
elif x == 1 and y == 1:
print(1)
else:
spiral = []
spiral.append((1, 0))
spiral.append((1, 1))
for i in range(1, 101):
spiral.append((-i, i))
spiral.append((-i, -i))
spiral.append((i + 1, -i))
spiral.append((i + 1, i + 1))
x1, y1 = findprevpoints(x, y)
print(spiral.index((x1, y1)) + 1) | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR VAR IF VAR NUMBER IF VAR NUMBER RETURN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = input().split()
x, y = int(x), int(y)
if x == y == 0:
print(0)
if y >= -x and y > x:
print(4 * y - 2)
if x + y < 0 and y >= x:
print(4 * abs(x) - 1)
if y < x and x + y <= 1:
print(4 * abs(y))
if x + y > 1 and y <= x:
print(4 * x - 3) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def on(segment, point):
start, end = segment
if point == start:
return False
if point == end:
return True
x1, y1 = start
x2, y2 = point
x3, y3 = end
if x1 == x2 and x2 == x3 and (y1 < y2 < y3 or y3 < y2 < y1):
return True
if y1 == y2 and y2 == y3 and (x1 < x2 < x3 or x3 < x2 < x1):
return True
return False
point = tuple(map(int, input().split()))
start, end = (0, 0), (1, 0)
if point in [start, end]:
print(0)
else:
start, end = (1, 0), (1, 1)
value, x_increment, y_increment, in_use = 2, True, False, 0
count = 0
while True:
count += 1
if on([start, end], point):
break
start = end
x = end[0] + (value if x_increment else 0) * (-1 if value % 2 == 0 else 1)
y = end[1] + (value if y_increment else 0) * (-1 if value % 2 == 0 else 1)
end = x, y
in_use += 1
if in_use == 2:
value += 1
in_use = 0
x_increment = not x_increment
y_increment = not y_increment
print(count) | FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR LIST VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | inputs = list(map(int, input().split()))
x = inputs[0]
y = inputs[1]
if y == 0 and 0 <= x <= 1:
print(0)
else:
a = max(abs(x), abs(y))
m = 1 + 4 * (a - 1)
if x == a and y == -(a - 1):
print(m - 1)
if x == a and -(a - 1) < y <= a:
print(m)
elif y == a and -a <= x < a:
print(m + 1)
elif x == -a and -a <= y < a:
print(m + 2)
elif y == -a and -a < x <= a + 1:
print(m + 3) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = [int(i) for i in input().split()]
if x >= 0:
if y > x:
zone = 3
elif y <= x and y > -x + 1:
zone = 2
else:
zone = 1
elif y >= -x:
zone = 3
elif y < -x and y >= x:
zone = 4
else:
zone = 1
if zone == 1:
ans = -y * 4
elif zone == 2:
ans = 1 + (x - 1) * 4
elif zone == 3:
ans = 2 + (y - 1) * 4
else:
ans = 3 + -1 * (x + 1) * 4
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def main():
x, y = map(int, input().split())
r, t = max(abs(x), abs(y)), 0
if x == r == 1 - y:
t = 4
elif x == r > -y:
t = 3
elif y == r > x:
t = 2
elif -x == r > y:
t = 1
print(
{
(0, 0): 0,
(1, 0): 0,
(1, 1): 1,
(0, 1): 2,
(-1, 1): 2,
(-1, 0): 3,
(-1, -1): 3,
(0, -1): 4,
(1, -1): 4,
}[x, y]
if r < 2
else r * 4 - t
)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = input().split()
x = int(x)
y = int(y)
p, q, z, i, j = 0, 0, 0, 1, 1
def GT(n):
t = 0
for i in range(1, n + 1):
if i % 2 != 0:
t += i
else:
t += -i
return t
while 1:
x1, y1 = max(GT(i), GT(i - 1)), min(GT(i), GT(i - 1))
x2, y2 = max(GT(j), GT(j - 1)), min(GT(j), GT(j - 1))
if z % 2 == 0:
p = GT(i)
i += 1
if z % 2 != 0:
q = GT(j)
j += 1
z += 1
if x >= y1 and x <= x1 and y == q:
break
if y >= y2 and y <= x2 and x == p:
break
if x == 0 and y == 0 or x == 1 and y == 0:
print(0)
else:
print(z) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = tuple(map(int, input().split()))
if x == 0 and y == 0:
print(0)
elif x >= 0 and y >= -x + 1 and y < x:
if y == -x + 1:
print(4 * (x - 1))
else:
print(4 * (x - 1) + 1)
elif y > 0 and (x == 0 or x <= abs(y) and x > -abs(y)):
if y == x:
print(4 * (abs(y) - 1) + 1)
else:
print(4 * (abs(y) - 1) + 2)
elif x <= 0 and y <= -x and y > x:
if y == -x:
print(4 * (abs(x) - 1) + 2)
else:
print(4 * (abs(x) - 1) + 1 + 2)
elif y == x:
print(4 * (abs(y) - 1) + 3)
else:
print(4 * (abs(y) - 1) + 1 + 3) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
sx = sy = 0
c = 0
i = j = 1
while 1:
p = sx
q = sy
if i % 2 == 0:
sy = sy + (-1) ** (j - 1) * j
j += 1
else:
sx = sx + (-1) ** (j - 1) * j
if (
(min(p, sx) <= x and x <= max(p, sx))
and (min(q, sy) <= y and y <= max(q, sy))
and (x == p and p == sx or y == q and q == sy)
):
if x == p and y == q:
c = c - 1
break
i += 1
c += 1
print(max(c, 0)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
u, v = abs(x), abs(y)
a = max(u, v)
s = 4 * (a - 1)
if u >= v and x > 0 and y > 1 - a:
s += 1
elif v >= u and y > 0:
s += 2
elif u >= v and x < 0:
s += 3
elif v >= u and y < 0:
s += 4
print(s if a > 0 else 0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
isCorner = x == y or -x == y and y > 0 or y == -x + 1 and x is not 0
reachX = 0 if x == 0 else 1 + (x - 1) * 4 if x > 0 else 3 + (abs(x) - 1) * 4
reachY = 0 if y == 0 else 2 + (y - 1) * 4 if y > 0 else abs(y) * 4
if isCorner:
print(min(reachX, reachY))
else:
print(max(reachX, reachY)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | p = [(0, 0)]
a, b = 0, 0
c = 1
while c < 202:
for i in range(c - 1):
a += 1
p.append((a, b))
p.append((a + 1, b, "*"))
a += 1
for j in range(c - 1):
b += 1
p.append((a, b))
p.append((a, b + 1, "*"))
b += 1
for k in range(c):
a -= 1
p.append((a, b))
p.append((a - 1, b, "*"))
a -= 1
for l in range(c):
b -= 1
p.append((a, b))
p.append((a, b - 1, "*"))
b -= 1
c += 2
x, y = list(map(int, input().split()))
if (x, y) in p:
z = p.index((x, y))
elif (x, y, "*") in p:
z = p.index((x, y, "*"))
t = 0
for w in p[:z]:
if len(w) == 3:
t += 1
print(t) | ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = [int(i) for i in input().split()]
x1, y1 = 0, 0
i = 1
direct = 2
counter = 0
br = 0
res = 0
while x1 != x or y1 != y:
if direct == 1:
direct = 4
for j in range(i):
y1 += 1
if x1 == x and y1 == y:
br = 1
break
elif direct == 2:
direct = 1
for j in range(i):
x1 += 1
if x1 == x and y1 == y:
br = 1
break
elif direct == 3:
direct = 2
for j in range(i):
y1 -= 1
if x1 == x and y1 == y:
br = 1
break
else:
direct = 3
for j in range(i):
x1 -= 1
if x1 == x and y1 == y:
br = 1
break
if br == 1:
break
counter += 1
if counter % 2 == 0:
i += 1
res += 1
print(res) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | a, b = map(int, input().split())
count = 0
q, e = 0, 0
i = 1
flag = 0
while q != a or e != b:
for j in range(i):
q += 1
if q == a and e == b:
flag = 1
break
if flag:
break
count += 1
for j in range(i):
e += 1
if q == a and e == b:
flag = 1
break
i += 1
if flag:
break
count += 1
for j in range(i):
q -= 1
if q == a and e == b:
flag = 1
break
if flag:
break
count += 1
for j in range(i):
e -= 1
if q == a and e == b:
flag = 1
break
if flag:
break
i += 1
count += 1
print(count) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = [int(y) for y in input().split()]
ans = 0
if (x == 0 or x == 1) and y == 0:
print(0)
else:
ans += 4 * max(abs(x), abs(y))
if x >= 0 and y <= 0:
if abs(y) == x - 1:
ans -= 4
if abs(y) < x - 1:
ans -= 3
elif x >= 0 and y >= 0:
if y <= x:
ans -= 3
else:
ans -= 2
elif x < 0 and y >= 0:
if y >= abs(x):
ans -= 2
else:
ans -= 1
elif abs(y) <= abs(x):
ans -= 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def nextStep(x, y):
if x == 0 and y == 0:
return 1, 0
if x == 1 and y == 0:
return 1, 1
if x > 0 and y > 0:
return -x, y
if x < 0 and y > 0:
return x, -y
if x < 0 and y < 0:
return -x + 1, y
if x > 0 and y < 0:
return x, -y + 1
def lie(cor1, cor2, x, y):
if cor1[0] != cor2[0]:
return y - cor1[1] == 0 and (
x <= max(cor1[0], cor2[0]) and x >= min(cor1[0], cor2[0])
)
else:
return x - cor1[0] == 0 and (
y <= max(cor1[1], cor2[1]) and y >= min(cor1[1], cor2[1])
)
def main():
a, b = map(int, input().split())
cor1 = 0, 0
cor2 = nextStep(*cor1)
turns = 0
while not lie(cor1, cor2, a, b):
cor1 = cor2
cor2 = nextStep(*cor2)
turns += 1
print(turns)
main() | FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | point = list(map(int, input().strip().split()))
i = 0
k = 0
segment = [[0, 0], [1, 0]]
seg_copy = [segment[0], point, segment[1]]
seg_copy.sort(key=lambda j: (j[1 - i], j[i]))
turns = 0
while seg_copy[1] != point:
turns += 1
segment[0] = list(segment[1])
segment[1][1] = segment[1][0]
if k == 0 or k == 2:
pass
elif k == 1:
segment[1][0] = -segment[1][0]
elif k == 3:
segment[1][0] = -segment[1][0] + 1
k = (k + 1) % 4
seg_copy = [segment[0], point, segment[1]]
i = (i + 1) % 2
seg_copy.sort(key=lambda j: (j[1 - i], j[i]))
print(turns) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | dest = list(map(int, input().split()))
side_len = 0
pos = [0, 0]
turns = 0
while True:
side_len += 1
if dest[1] == pos[1] and pos[0] <= dest[0] <= pos[0] + side_len:
break
turns += 1
pos[0] += side_len
if dest[0] == pos[0] and pos[1] <= dest[1] <= pos[1] + side_len:
break
turns += 1
pos[1] += side_len
side_len += 1
if dest[1] == pos[1] and pos[0] >= dest[0] >= pos[0] - side_len:
break
turns += 1
pos[0] -= side_len
if dest[0] == pos[0] and pos[1] >= dest[1] >= pos[1] - side_len:
break
turns += 1
pos[1] -= side_len
print(turns) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | entry = input().split(" ")
x, y = list(map(int, entry))
if abs(y) > abs(x) and y >= 0 or y > 0 and x == -y:
x, y = 0, y
position = 1
elif abs(x) > abs(y) and x <= 0 or x < 0 and x == y:
x, y = x, 0
position = 2
elif abs(y) > abs(x) and y <= 0 or x > 0 and (x == -y or y == -x + 1):
x, y = 0, y
position = 3
elif x == 0 and y == 0:
position = 0
print(0)
else:
x, y = x, 0
position = 4
if position == 4:
print(1 + 4 * (x - 1))
elif position == 3:
print(-4 * y)
elif position == 2:
print(3 - 4 * (x + 1))
elif position == 1:
print(2 + 4 * (y - 1)) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x = 0
y = 0
nx, ny = map(int, input().split())
if (nx == 0 or nx == 1) and ny == 0:
print(0)
else:
x = 1
turn = 0
flag = 0
while True:
turn = turn + 1
while y != x:
y = y + 1
if x == nx and y == ny:
flag = 1
break
if flag == 1:
break
k = x * -1
turn = turn + 1
while x != k:
x = x - 1
if x == nx and y == ny:
flag = 1
break
if flag == 1:
break
turn = turn + 1
while y != x:
y = y - 1
if x == nx and y == ny:
flag = 1
break
if flag == 1:
break
k = x * -1 + 1
turn = turn + 1
while x != k:
x = x + 1
if x == nx and y == ny:
flag = 1
break
if flag == 1:
break
print(turn) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
def fn(x, y):
if y == 0 and x == 0:
return 0
elif x < 0 and x + y == 0:
return 4 * abs(y) - 2
elif x == y:
if x > 0:
return 4 * x - 3
else:
return 4 * abs(x) - 1
elif x + y == 1 and x == abs(y) + 1:
return 4 * abs(y)
elif abs(x) <= abs(y):
if y < 0:
return 4 * abs(y)
else:
return 4 * y - 2
elif abs(x) >= abs(y):
if x > 0:
return 4 * x - 3
else:
return 4 * abs(x) - 1
print(fn(x, y)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER BIN_OP VAR VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP NUMBER FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def point_on_spiral(x_int, y_int) -> int:
if x_int == 0 and y_int == 0 or x_int == 1 and y_int == 0:
return 0
count = 0
x_prev, y_prev = 0, 0
while True:
spiral, count_mod_4 = divmod(count + 4, 4)
if count_mod_4 == 0:
x_curr, y_curr = spiral, -(spiral - 1)
elif count_mod_4 == 1:
x_curr, y_curr = spiral, spiral
elif count_mod_4 == 2:
x_curr, y_curr = -spiral, spiral
else:
x_curr, y_curr = -spiral, -spiral
dist_to_prev = abs(x_int - x_prev) + abs(y_int - y_prev)
dist_to_curr = abs(x_int - x_curr) + abs(y_int - y_curr)
dist_prev_curr = abs(x_curr - x_prev) + abs(y_curr - y_prev)
if dist_to_curr + dist_to_prev == dist_prev_curr:
return count
count += 1
x_prev, y_prev = x_curr, y_curr
x, y = list(map(int, input().split()))
print(point_on_spiral(x, y)) | FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
z = max(abs(x), abs(y))
c = (z - 1) * 4
if x == z and y > -z + 1 and y <= z:
c += 1
elif x < z and x >= -z and y == z:
c += 2
elif x == -z and y < z and y >= -z:
c += 3
elif y == -z and x > -z and x <= z + 1:
c += 4
print(max(0, c)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def main():
a, b = list(map(int, input().split()))
if a == 0 and b == 0:
return 0
x = 0
y = 0
step = 1
rep = 0
direction = 0
ans = 0
while x != a or y != b:
if direction == 0:
for i in range(step):
x += 1
if x == a and y == b:
return ans
direction = 1
ans += 1
elif direction == 1:
for i in range(step):
y += 1
if x == a and y == b:
return ans
direction = 2
step += 1
ans += 1
elif direction == 2:
for i in range(step):
x -= 1
if x == a and y == b:
return ans
direction = 3
ans += 1
else:
for i in range(step):
y -= 1
if x == a and y == b:
return ans
direction = 0
step += 1
ans += 1
print(main()) | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
i = 0
j = 0
n = 1
cnt = 0
while True:
if n % 2 == 1:
if cnt % 2 == 0:
if i <= x and x <= i + n and j == y:
break
else:
i += n
cnt += 1
elif i == x and j <= y and y <= j + n:
break
else:
j += n
cnt += 1
n += 1
elif cnt % 2 == 0:
if i - n <= x and x <= i and j == y:
break
else:
i -= n
cnt += 1
elif j - n <= y and y <= j and i == x:
break
else:
j -= n
cnt += 1
n += 1
print(cnt) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
cur_x, cur_y = 0, 0
i = 1
pos = 0
sign = [(1, 0), (0, 1), (-1, 0), (0, -1)]
ans = 0
j = 0
while cur_x != x or cur_y != y:
if j == i:
j = 0
if pos % 2 == 1:
i += 1
pos = (pos + 1) % 4
ans += 1
cur_x = cur_x + sign[pos][0]
cur_y = cur_y + sign[pos][1]
j += 1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
x1 = abs(x)
y1 = abs(y)
mx = max(x1, y1)
if x == 1 and y == 0:
print(0)
elif x == 0 and y == 0:
print(0)
elif x == 2 and y == -1:
print(4)
elif x1 == y1:
if x > 0:
if y > 0:
print(4 * x1 - 3)
elif y < 0:
print(4 * x1)
elif x < 0:
if y > 0:
print(4 * x1 - 2)
elif y < 0:
print(4 * x1 - 1)
elif x1 != y1:
if x1 == mx:
if x > 0:
print(4 * x1 - 3)
elif x < 0:
print(4 * x1 - 1)
elif y1 == mx:
if y > 0:
print(4 * y1 - 2)
elif y < 0:
print(4 * y1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = list(map(int, input().rstrip().split()))
if x == 0 and y == 0 or x == 1 and y == 0:
print(0)
elif x > 0 and y in range(-1 * x + 2, x + 1):
ans = 1 + 4 * (abs(x) - 1)
print(ans)
elif y > 0 and x in range(-1 * y, y):
ans = 1 + 4 * (y - 1)
print(ans + 1)
elif x < 0 and y in range(x - 1, abs(x)):
ans = 1 + 4 * (abs(x) - 1)
print(ans + 2)
else:
ans = 1 + 4 * (abs(y) - 1)
print(ans + 3) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | s = input().split()
x = int(s[0])
y = int(s[1])
dirx = [1, 0, -1, 0]
diry = [0, 1, 0, -1]
if x == 0 and y == 0:
print(0)
else:
cx = 0
cy = 0
dir = 0
steps = 1
count = 0
done = False
while not done:
for i in range(steps):
cx += dirx[dir]
cy += diry[dir]
if cx == x and cy == y:
print(count)
done = True
break
count += 1
dir = (dir + 1) % 4
if dir == 0 or dir == 2:
steps += 1 | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | U = 1
D = -1
x, y = map(int, input().split())
k = A = B = 0
while 1:
a, b = U, B
k += 1
if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
break
A = a
b = U
k += 1
if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
break
B = b
a = D
k += 1
if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
break
A = a
b = D
k += 1
if (A <= x <= a or a <= x <= A) and (B <= y <= b or b <= y <= B):
break
A, B = a, b
U += 1
D -= 1
if k < 1:
k = 1
print(k - 1) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | l = input().split(" ")
x, y = int(l[0]), int(l[1])
if y <= 0 and x >= 1 + y and x <= 1 - y:
print(-y * 4)
elif x > 0 and y >= 2 - x and y <= x:
print(x * 4 - 3)
elif y > 0 and x >= -y and x <= y - 1:
print(y * 4 - 2)
elif x < 0 and y >= x and y <= -1 - x:
print(-x * 4 - 1)
else:
print(0) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | def spiral():
X, Y = list(map(int, input().split()))
x = y = 0
dx = 0
dy = -1
turn = 0
if X == 0 and Y == 0:
turn = 1
else:
while 1:
if x == X and y == Y:
break
if x == y or x < 0 and x == -y or x > 0 and x == 1 - y:
dx, dy = -dy, dx
turn += 1
x, y = x + dx, y + dy
print(turn - 1)
spiral() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
start = [0, 0]
turns = 0
flag = 0
while [x, y] != [start[0], start[1]] and flag == 0:
turns += 1
if turns % 4 == 1:
for i in range(2 * (turns // 4 + 1) - 1):
start[0] += 1
if [x, y] == [start[0], start[1]]:
flag = 1
elif turns % 4 == 2:
for i in range(2 * (turns // 4 + 1) - 1):
start[1] += 1
if [x, y] == [start[0], start[1]]:
flag = 1
elif turns % 4 == 3:
z = -1 * start[0]
for i in range(start[0], -1 * start[0] - 1, -1):
if [i, start[1]] == [x, y]:
flag = 1
start[0] = -1 * start[0]
else:
for i in range(start[1], -1 * start[1] - 1, -1):
if [start[0], i] == [x, y]:
flag = 1
start[1] = -1 * start[1]
print(max(0, turns - 1)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE LIST VAR VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF LIST VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF LIST VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF LIST VAR VAR NUMBER LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER IF LIST VAR NUMBER VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
if x == y == 0 or x == 1 and y == 0:
print(0)
exit()
dx = [0, -1, 0, 1]
dy = [1, 0, -1, 0]
i = 1
ty = 0
tx = 1
c = 0
while 1:
i += 0.5
j = 0
c += 1
while j < int(i):
tx += dx[(c - 1) % 4]
ty += dy[(c - 1) % 4]
j += 1
if x == tx and y == ty:
print(c)
exit() | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = list(map(int, input().split()))
if y >= x and y < -x:
print(-4 * x - 1)
elif y > x and y >= -x:
print(4 * y - 2)
elif y <= x and y > 1 - x:
print(4 * x - 3)
elif y < x and y <= 1 - x:
print(-4 * y)
else:
print(0) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
a, b, cnt = 0, 0, 0
if x == y == 0:
print(0)
exit()
while 1:
while a + b != 1:
a += 1
if a == x and b == y:
print(cnt)
exit()
cnt += 1
while a - b != 0:
b += 1
if a == x and b == y:
print(cnt)
exit()
cnt += 1
while a + b != 0:
a -= 1
if a == x and b == y:
print(cnt)
exit()
cnt += 1
while a != b:
b -= 1
if a == x and b == y:
print(cnt)
exit()
cnt += 1 | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR WHILE NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)], [(1, 0), (1, 1)], [(1, 1), ( - 1, 1)], [( - 1, 1), ( - 1, - 1)], [( - 1, - 1), (2, - 1)], [(2, - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.
Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).
-----Input-----
The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).
-----Output-----
Print a single integer, showing how many times Valera has to turn.
-----Examples-----
Input
0 0
Output
0
Input
1 0
Output
0
Input
0 1
Output
2
Input
-1 -1
Output
3 | x, y = map(int, input().split())
sector = 0
flag = 0
if x == 0 and y == 0:
flag = 1
elif x == 1 and y == 0:
flag = 2
elif x > 0 and y >= 0 and x >= y:
sector = 1
elif x > 0 and y < 0 and x > 1 - y:
sector = 1
elif x >= 0 and y > 0 and x < y:
sector = 2
elif x < 0 and y > 0 and -x <= y:
sector = 2
elif x < 0 and y >= 0 and -x > y:
sector = 3
elif x < 0 and y < 0 and -x >= -y:
sector = 3
elif x <= 0 and y < 0 and -y > -x:
sector = 4
elif x > 0 and y < 0 and x <= 1 - y:
sector = 4
if flag == 1 or flag == 2:
print(0)
elif sector == 1:
print(4 * (x - 1) + 1)
elif sector == 3:
print(4 * (-x - 1) + 3)
elif sector == 2:
print(4 * (y - 1) + 2)
elif sector == 4:
print(4 * (-y - 1) + 4) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def ishappy(n, d):
if n == 1:
return True
elif n in d:
return False
d[n] = 0
a = str(n)
m = 0
for i in a:
i = int(i)
m = m + i * i
return ishappy(m, d)
d = {}
for i in range(N + 1, 109990909):
if ishappy(i, d):
return i | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def util(self, n):
s = 0
while n != 0:
s += n % 10 * (n % 10)
n = n // 10
return s
def nextHappy(self, N):
i = N + 1
while True:
li = []
s = i
while 1:
s = self.util(s)
if s == 1:
return i
if s in li:
break
li.append(s)
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | def isHappy(n):
k = n
d = {}
while True:
try:
if p[k]:
return True
except:
pass
s = 0
try:
return d[n]
except:
d[n] = False
while n:
s += (n % 10) ** 2
n = n // 10
n = s
if n == 1:
return True
return True
p = {}
for i in range(1, 5 * 10**4):
p[i] = isHappy(i)
class Solution:
def nextHappy(self, N):
for i in range(N + 1, 5 * 10**4):
if p[i]:
return i | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR DICT WHILE NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER IF VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
while True:
N += 1
result = self.isHappy(N)
if result:
return N
def isHappy(self, n):
slow = self.squared(n)
fast = self.squared(self.squared(n))
while slow != fast and fast != 1:
slow = self.squared(slow)
fast = self.squared(self.squared(fast))
return fast == 1
def squared(self, n: int) -> int:
res = 0
while n > 0:
digit = n % 10
res += digit**2
n = n // 10
return res
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.nextHappy(N)) | CLASS_DEF FUNC_DEF WHILE NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR NUMBER FUNC_DEF VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def recur(self, num):
if num == 1 or num == 7 or num == 10:
return True
if num < 10:
return False
sm = 0
while num > 0:
n = num % 10
sm += n * n
num //= 10
return self.recur(sm)
def nextHappy(self, N):
number = N + 1
while True:
if self.recur(number):
return number
number += 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def check(self, n):
if n // 10 == 0:
n = n % 10 * (n % 10)
while n // 10 != 0:
N = n
nh = 0
while N > 0:
nh += N % 10 * (N % 10)
N //= 10
n = nh
if n == 1:
return 1
else:
return 0
def nextHappy(self, N):
if N == 0:
return 1
if self.check(N + 1):
return N + 1
else:
return self.nextHappy(N + 1) | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def square_sum(self, num):
sqaure_sum1 = 0
while num != 0:
sqaure_sum1 += (num % 10) ** 2
num //= 10
return sqaure_sum1
def check_indv_num(self, num):
slow_ptr = num
fast_ptr = num
while True:
slow_ptr = self.square_sum(slow_ptr)
fast_ptr = self.square_sum(self.square_sum(fast_ptr))
if slow_ptr == fast_ptr:
break
return slow_ptr == 1
def nextHappy(self, N):
for i in range(N + 1, N + 100):
if self.check_indv_num(i):
return i
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def isHappy(self, N):
if N == 1 or N == 7:
return 1
sum = N
x = N
while sum > 9:
sum = 0
while x > 0:
d = x % 10
sum += d * d
x //= 10
if sum == 1:
return 1
x = sum
if sum == 7:
return 1
return 0
def nextHappy(self, N):
x = N + 1
res = x
while True:
if self.isHappy(x):
return x
x += 1
res = x
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.nextHappy(N)) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def happy(n):
s = 0
while n > 0:
s += (n % 10) ** 2
n //= 10
if s == 1:
return True
if s == 4:
return False
return happy(s)
i = N + 1
while not happy(i):
i += 1
return i | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | def sum_of_digits(n):
s = 0
while n > 0:
s = s + (n % 10) ** 2
n = n // 10
return s
def is_happy_num(n):
slow = n
fast = sum_of_digits(n)
while fast != 1 and slow != fast:
slow = sum_of_digits(slow)
fast = sum_of_digits(sum_of_digits(fast))
if fast == 1:
return True
return False
class Solution:
def nextHappy(self, N):
n = N + 1
while True:
if is_happy_num(n):
return n
n += 1 | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def check(self, n):
if n in self.prev:
return False
self.prev.add(n)
numbers = []
while n:
numbers.append(n % 10)
n = n // 10
ans = 0
for i in numbers:
ans += i * i
if ans == 1:
return True
return self.check(ans)
def nextHappy(self, N):
i = N + 1
while True:
self.prev = set()
if self.check(i):
return i
i += 1 | CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def isHappy(n):
l = [2, 3, 4, 5, 6, 8, 9]
while n != 1:
if n in l:
return False
s = 0
for i in str(n):
s += int(i) ** 2
n = s
return True
x = N + 1
while 1:
if isHappy(x):
return x
x += 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | def numSquareSum(n):
squareSum = 0
while n:
squareSum += n % 10 * (n % 10)
n = int(n / 10)
return squareSum
def isHappynumber(n):
st = set()
while 1:
n = numSquareSum(n)
if n == 1:
return True
if n in st:
return False
st.add(n)
class Solution:
def nextHappy(self, N):
n = N + 1
while True:
if isHappynumber(n):
return n
n += 1 | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def ifhappy(self, num):
s = 0
for i in str(num):
s += int(i) ** 2
if s == 1 or s == 7:
return 1
elif len(str(s)) == 1:
return 0
return self.ifhappy(s)
def nextHappy(self, N):
i = 1
while True:
if self.ifhappy(N + i) == 0:
i += 1
else:
break
return N + i | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def rec(n, dic):
if n == 1:
return True
curr = 0
while n != 0:
temp = (n % 10) ** 2
curr += temp
n //= 10
if curr in dic:
return False
dic.add(curr)
return rec(curr, dic)
while True:
dic = set()
if rec(N + 1, dic):
return N + 1
N += 1
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
ob = Solution()
print(ob.nextHappy(N)) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
li = []
for n in range(N, N + 100):
p = n
count_i = 0
while n != 1:
sum_i = 0
for i in str(n):
sum_i += int(i) ** 2
n = sum_i
count_i += 1
if count_i > 10:
break
if n == 1:
li.append(p)
if li[0] == N:
return li[1]
else:
return li[0] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR RETURN VAR NUMBER RETURN VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def isHappy(self, n):
k = n
while True:
k = str(k)
digitSqSum = 0
for i in k:
digitSqSum += int(i) * int(i)
if digitSqSum == 1 or digitSqSum == 7:
return True
if digitSqSum < 10:
return False
if digitSqSum == n:
return False
k = digitSqSum
def nextHappy(self, N):
N += 1
while not self.isHappy(N):
N += 1
return N | CLASS_DEF FUNC_DEF ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_DEF VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
for i in range(N + 1, 10000):
if self.check(i):
return i
return -1
def check(self, N):
if N == 7 or N == 1 or N == 10:
return True
elif N < 10:
return False
sum = 0
while N > 0:
r = N % 10
sum += r * r
N = N // 10
return self.check(sum) | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def getSum(self, N):
if N == 1 or N == 7 or N == 10:
return True
if N < 10:
return False
add = 0
while N > 0:
temp = N % 10
add += temp * temp
N = int(N / 10)
return self.getSum(add)
def nextHappy(self, N):
while True:
N = N + 1
if self.getSum(N):
return N | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
dp = {}
v = set()
def f(i):
if i in dp:
return dp[i]
if i == 1:
dp[1] = True
return True
if i in v:
dp[i] = False
return False
v.add(i)
dp[i] = f(sum(int(j) ** 2 for j in str(i)))
return dp[i]
i = N + 1
while True:
if f(i):
return i
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
n = N + 1
def ishappy(n1, sq):
while n1 != 0:
rem = n1 % 10
sq += rem * rem
n1 = n1 // 10
if n1 == 0:
n1 = sq
sq = 0
if n1 >= 0 and n1 <= 9:
break
if n1 == 1:
return True
else:
return False
while n:
n1 = n
sq = 0
if ishappy(n1, sq):
return n
else:
n += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
num = N + 1
while True:
isHappy = self.checkHappy(num)
if isHappy:
return num
num += 1
return None
def checkHappy(self, n):
seen = set()
while True:
n = self.getNextNumber(n)
if n == 1:
return True
if n in seen:
return False
seen.add(n)
return False
def getNextNumber(self, n):
total = 0
while n > 0:
rem = n % 10
total += rem**2
n = n // 10
return total | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN VAR VAR NUMBER RETURN NONE FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def check(self, n, l=[]):
if n in l:
return n == 1
q = sum([(int(x) ** 2) for x in str(n)])
return self.check(q, [*l, n])
def nextHappy(self, N):
while True:
N += 1
if self.check(N):
return N | CLASS_DEF FUNC_DEF LIST IF VAR VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR LIST VAR VAR FUNC_DEF WHILE NUMBER VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
count = True
temp = 0
status = False
def nextHappy(self, N):
if Solution.count:
Solution.temp = N
Solution.count = False
n = str(N)
t = 0
if N == 7 and Solution.status == True:
Solution.count = True
Solution.status = False
return Solution.temp
for i in range(0, len(n)):
t = t + int(n[i]) ** 2
if t == 1 and Solution.status == True:
Solution.count = True
Solution.status = False
return Solution.temp
elif t >= 2 and t <= 9 and t != 7 or t == 1 and Solution.status == False:
Solution.count = True
Solution.status = True
return self.nextHappy(Solution.temp + 1)
else:
return self.nextHappy(t) | CLASS_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def numSquareSum(n):
squareSum = 0
while n:
squareSum += n % 10 * (n % 10)
n = int(n / 10)
return squareSum
def isHappynumber(n):
slow = n
fast = n
while True:
slow = numSquareSum(slow)
fast = numSquareSum(numSquareSum(fast))
if slow != fast:
continue
else:
break
return slow == 1
while True:
if isHappynumber(N + 1):
return N + 1
N += 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def SumSquare(self, n):
summ = 0
while n != 0:
dg = n % 10
summ += dg * dg
n //= 10
return summ
def isHappy(self, n):
st = set()
st.add(n)
while True:
if n == 1:
return 1
n = self.SumSquare(n)
if n in st:
return 0
st.add(n)
return 0
def nextHappy(self, N):
for i in range(N + 1, N + 100):
if self.isHappy(i):
return i
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
N += 1
def ishappy(n):
s = n
while s > 9:
s = 0
while n > 0:
r = n % 10
s += r**2
n //= 10
if s > 9:
n = s
if s == 1 or s == 7:
return True
else:
return False
while 1:
if ishappy(N):
return N
break
N += 1 | CLASS_DEF FUNC_DEF VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | def happy(N):
l = 0
while N:
r = N % 10
l += r**2
N = N // 10
if l > 9 and N == 0:
N = l
l = 0
if l == 1:
return True
return False
class Solution:
def nextHappy(self, N):
N = N + 1
while happy(N) == False:
N = N + 1
return N | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def isHappy(self, n):
if n == 1 or n == 7:
return True
sos = n
x = n
while sos > 9:
sos = 0
while x > 0:
d = x % 10
sos += d * d
x //= 10
x = sos
if sos == 1 or sos == 7:
return True
return False
def nextHappy(self, N):
n = N + 1
while True:
if self.isHappy(n):
return n
n += 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
i = N + 1
while True:
j = i
l = []
while True:
if j in l:
break
if j == 1:
return i
sum = 0
l.append(j)
while j:
sum += j % 10 * (j % 10)
j //= 10
j = sum
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE NUMBER IF VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def dig(n):
while n > 9:
s = 0
while n:
t = n % 10
s += t * t
n //= 10
n = s
return n
n = N + 1
while True:
if dig(n) == 1 or dig(n) == 7:
return n
n += 1 | CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, n):
p, b = 0, 0
for i in range(n + 1, n + 50):
if i == 1 or i <= 7:
return 7
else:
b = self.check(i)
if b == 1:
return i
def check(self, m):
while m >= 10:
k = [int(d) for d in str(m)]
m = 0
for i in k:
m = m + i * i
return m | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR FUNC_DEF WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, n):
q = {(1): 1}
s = set()
def dfs(i):
if i in q:
return q[i]
if i in s:
return -1
s.add(i)
q[i] = dfs(sum(j * j for j in map(int, [*str(i)])))
return q[i]
n += 1
o = dfs(n)
while o == -1:
n += 1
o = dfs(n)
return n | CLASS_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR LIST FUNC_CALL VAR VAR RETURN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def happy(self, n):
s = set()
while True:
s.add(n)
if n == 1:
return True
val = 0
while n >= 1:
val += (n % 10) ** 2
n = n // 10
n = val
if n in s:
return False
def nextHappy(self, n):
if self.happy(n + 1):
return n + 1
return self.nextHappy(n + 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def squaresum(self, n):
s = 0
while n != 0:
s += n % 10 * (n % 10)
n //= 10
return s
def nextHappy(self, N):
n = N + 1
while True:
slow = n
fast = n
while True:
slow = self.squaresum(slow)
fast = self.squaresum(self.squaresum(fast))
if slow != fast:
continue
else:
break
if slow == 1:
return n
n += 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER RETURN VAR VAR NUMBER RETURN NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def happy(n):
def new(num):
c = 0
while num > 0:
a = num % 10
c += a**2
num = num // 10
return c
slow = new(n)
fast = new(new(n))
while fast != slow and fast != 1 and slow != 1:
slow = new(slow)
fast = new(new(fast))
if fast == 1 or slow == 1:
return True
return False
N = N + 1
while True:
if happy(N):
return N
N += 1 | CLASS_DEF FUNC_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def digits_N(self, N):
num = N
digits = []
while num:
digits.append(num % 10)
num = num // 10
return digits
def check_happiness(self, N):
num = N
non_happy = []
while num > 9:
digits = self.digits_N(num)
num = 0
for i in digits:
num += i * i
if num in non_happy:
return False
non_happy.append(num)
if num == 1:
return True
if num == 7:
return True
return False
def nextHappy(self, N):
happy_flag = False
while not happy_flag:
N = N + 1
if self.check_happiness(N):
happy_flag = True
return N | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def fun(i):
res = i
while res != 1:
i = res
res = 0
if a[i] == 0:
a[i] = 1
while i > 0:
res += pow(i % 10, 2)
i = i // 10
else:
return 0
if res == 1:
return 1
a = [0] * 10000
if N == 1:
return 10
for i in range(N + 1, 10000):
res = fun(i)
if res == 1:
return i | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
i = N + 1
while True:
if isHappy(i, set()):
return i
i += 1
def isHappy(N, s):
s.add(N)
a = N
val = N
while True:
sum_ = 0
while val != 0:
k = val % 10
val //= 10
sum_ += k * k
if sum_ == 1:
return 1
elif sum_ in s:
return 0
s.add(sum_)
val = sum_ | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR RETURN VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def happy(n):
s = 0
while n:
r = n % 10
s += r * r
n = n // 10
return s
def ishappy(n):
s = set()
while True:
n = happy(n)
if n == 1:
return True
if n in s:
return False
s.add(n)
while True:
N = N + 1
if ishappy(N):
return N | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def isHappy(self, N):
if N == 1 or N == 7:
return True
if N < 10:
return False
result = 0
while N > 0:
result += N % 10 * (N % 10)
N = N // 10
return self.isHappy(result)
def nextHappy(self, N):
N += 1
while True:
if self.isHappy(N):
return N
N += 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
def ishappy(n):
s = 0
while n:
r = n % 10
s += r**2
n = n // 10
return s
for i in range(N + 1, N + 100):
t = i
while i > 9:
i = ishappy(i)
if i == 1 or i == 7:
return t
else:
continue | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
N = N + 1
while True:
if self.ishappy(N):
return N
N += 1
def ishappy(self, N):
sum = 0
while N > 0:
digit = N % 10
sum += digit * digit
N = N // 10
if sum == 1:
return True
if sum < 10:
return False
return self.ishappy(sum) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR RETURN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def checkHappyYear(self, N, loopCheck):
if N == 1:
return True
s = 0
while N != 0:
s += (N % 10) ** 2
N //= 10
if s in loopCheck:
return False
loopCheck.add(s)
return self.checkHappyYear(s, loopCheck)
def solve(self, N):
loopCheck = set([N])
if self.checkHappyYear(N, loopCheck):
return N
return self.solve(N + 1)
def nextHappy(self, N):
return self.solve(N + 1) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER |
For a given non-negative integer N, find the next smallest Happy Number. A number is called happy if it leads to 1 after a sequence of steps. Wherein at each step the number is replaced by the sum of squares of its digits that is if we start with Happy Number and keep replacing it with sum of squares of its digits, we reach 1 at some point.
Example 1:
Input:
N = 8
Output:
10
Explanation:
Next happy number after 8 is 10 since
1*1 + 0*0 = 1
Example 2:
Input:
N = 10
Output
13
Explanation:
After 10, 13 is the smallest happy number because
1*1 + 3*3 = 10, so we replace 13 by 10 and 1*1 + 0*0 = 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function nextHappy() which takes an integer N as input parameters and returns an integer, next Happy number after N.
Expected Time Complexity: O(Nlog_{10}N)
Expected Space Complexity: O(1)
Constraints:
1<=N<=10^{5} | class Solution:
def nextHappy(self, N):
for j in range(N + 1, N**10):
if j == 1 or j == 7:
return j
q = j
while j > 9:
i = 0
x = str(j)
z = 0
while i < len(x):
z = z + int(x[i]) ** 2
i = i + 1
if z == 1:
return q
j = z | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.