Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. def stepCount(n):
  2. n = int(n)
  3. steps = 0
  4. while n > 1:
  5. if n % 2 == 0:
  6. n = n // 2
  7. elif n == 3 or n % 4 == 1:
  8. n = n - 1
  9. else:
  10. n = n + 1
  11. steps += 1
  12. print(steps)
  13. return steps
  14.  
  15.  
  16. def bin_2_decimal(binary):
  17. binary1 = binary
  18. decimal, i, n = 0, 0, 0
  19. while binary != 0:
  20. dec = binary % 10
  21. decimal = decimal + dec * pow(2, i)
  22. binary = binary//10
  23. i += 1
  24. print(decimal)
  25.  
  26. if __name__ == "__main__":
  27. with open("test_input.txt", "r") as file:
  28. for line in file:
  29. stepCount(line)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement