Skip to content

Instantly share code, notes, and snippets.

@duanebester
Created October 22, 2020 02:34
Show Gist options
  • Save duanebester/1b143cb0f72f046ac0cc105614897657 to your computer and use it in GitHub Desktop.
Save duanebester/1b143cb0f72f046ac0cc105614897657 to your computer and use it in GitHub Desktop.
Python Reduce Steps Challenge
def calc_steps(num):
count = 0
while num > 1:
if num % 2 == 0: # bitmask: *0
num = num // 2
elif num == 3 or num % 4 == 1: # bitmask: 01
num -= 1
else: # bitmask: 11
num += 1
count += 1
return count
def solution(s):
return calc_steps(int(s))
print solution('199999999999999999') # 73
print solution('15') # 5
print solution('4') # 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment