Advertisement
Guest User

Untitled

a guest
Dec 25th, 2022
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. def snafutodec(s):
  2. digits={"=": -2, "-": -1, "0": 0, "1": 1, "2": 2}
  3. n=0
  4. for i in range(len(s)):
  5. n+=5**i*digits[s[-i-1]]
  6. return n
  7.  
  8. def dectosnafu(n):
  9. digits={-2: "=", -1: "-", 0: "0", 1: "1", 2: "2"}
  10. s=""
  11. while n!=0:
  12. s+=digits[(n+2)%5-2]
  13. n=(n-((n+2)%5-2))//5
  14. return s[::-1]
  15.  
  16. inputfile="input25.txt"
  17. n=0
  18. with open(inputfile) as myfile:
  19. for line in myfile:
  20. n+=snafutodec(line.strip())
  21. print(dectosnafu(n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement