Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. N = int(input())
  2. for x in range(N):
  3. A = str(input())
  4. numberOfDigits = len(A)
  5. firstOddDigitIndex = -1
  6.  
  7. for y in range(numberOfDigits):
  8. if int(A[y]) % 2 != 0:
  9. firstOddDigitIndex = y
  10. break
  11.  
  12. if firstOddDigitIndex == -1:
  13. print("Case #{}: {}".format(int(x+1), 0))
  14. elif firstOddDigitIndex == numberOfDigits-1:
  15. print("Case #{}: {}".format(int(x+1), 1))
  16. else:
  17. # mainNum is the slice of the original number from the first odd digit to the end
  18. mainNum = int(A[firstOddDigitIndex: ])
  19. numberOfDigitsInMain = len(str(mainNum))
  20.  
  21. # numberWithZeroes is the number used to replace all the digits to the right of the first odd digit with zeroes
  22. numberWithZeroes = 10 ** (numberOfDigitsInMain - 1)
  23.  
  24. # numberWithEights is the number used to replace all the digits to the right of the first odd digit with eights
  25. numberWithEights = ''.join(['8' for x in range(numberOfDigitsInMain - 1)])
  26.  
  27. firstOddDigit = int(str(mainNum)[ :1])
  28.  
  29. UpNum = (firstOddDigit + 1) * numberWithZeroes
  30. DownNum = int(str(firstOddDigit - 1) + numberWithEights)
  31.  
  32. numberOfKeys = mainNum - DownNum if A[firstOddDigitIndex] == '9' else min(UpNum - mainNum, mainNum - DownNum)
  33.  
  34. print("Case #{}: {}".format(int(x+1), numberOfKeys))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement