A
A
Alexander123452021-11-28 11:51:20
Python
Alexander12345, 2021-11-28 11:51:20

How to convert a number to the binary system consisting of 2 and 5?

The very essence of the problem is in the title. There is some number in the number system consisting of 2 and 5, for example 22. If you convert it to decimal, you get 3. The count starts from 2, that is: 2 - 1; 5 - 2 and so on. I was able to implement it like:

N = int(input())
print(str(int(str(N).replace('2', '0').replace('5', '1'), 2)))

Of course, it doesn't work correctly. When you enter 2, it outputs 0. When you enter 22, 222 and so on - the same. Is there an easy way to solve my problem? Library for custom number systems? And no looping at all. Glad for any answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-11-28
@Vindicar

Converting a number to a binary string is bin(). The only thing is to tear off the first two characters of the 0b prefix. For more convenient replacement of several characters at once, it is better to use .translate(). As a result, we get:

table = {ord('0'):'2',ord('1'):'5'}
print(bin(N)[2:].translate(table))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question