def solution(n,a,b):
arr1 = [i for i in range(1, n+1)]
result = []
index = 0
count = 1
while True:
while index < len(arr1):
if arr1[index] == a and arr1[index+1] == b or arr1[index] == b and arr1[index+1] == a:
return count
exit()
elif arr1[index] == a or arr1[index+1] == a:
result.append(a)
index += 2
elif arr1[index] == b or arr1[index + 1] == b:
result.append(b)
index += 2
else:
result.append(arr1[index])
index += 2
arr1 = result[:]
result = []
index = 0
count += 1
return 0
직접 시뮬레이션 하면서 구현한 것이다.
def solution(n,a,b):
count = 0
while a != b:
a = (a+1) // 2
b = (b+1) // 2
count += 1
return count
print(solution(8, 4, 7), 3)