Created
October 22, 2020 02:34
-
-
Save duanebester/1b143cb0f72f046ac0cc105614897657 to your computer and use it in GitHub Desktop.
Python Reduce Steps Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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