-2

I failed an exam because of one question. The task is:

"Design a program that converts any number from any system to decimal.
We confine to the systems in the range from 2 to 22."

So there I am. I know the binary[2], octal[8], decimal[10] and hexadecimal[16] systems. There's 1 point for each conversion system, so it has to be a converter:

2->10
3->10
...
22->10

I have no idea how is that possible. I asked my professor after the exam how to do it and he said: "Just x to the power of y, multiply, and there it is. There's the same rule for all of them."

I might be mistaken in what he said because I was in the post-exam state of consciousness. Do you guys have any idea how to solve it?

I see that there were a few questions like that on stackoverflow already, but none of them does not solve the problem the way my professor said. Also, we started learning Python ~4 months ago and we haven't learned some of the options implemented in the replies.

"""IN
str/int, any base[2-22]
OUT
decimal int or float"""

Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
  • 1
    [This](http://www.purplemath.com/modules/numbbase.htm) should help you understand what your professor said – SirParselot Jan 25 '16 at 13:59
  • This is not a place for you to solve your homework/task. Show us what you have tried, and do you know how to change the base of a number without using calculator/compute? – Jeremy Jan 25 '16 at 13:59
  • What have you tried? Hint: If I give you this number, and tell you, it's in base 4, can you figure out what its decimal equivalent is? 313223 How did you accomplish it? – Gil Hamilton Jan 25 '16 at 14:00
  • 2
    You can also just use `int(str, x)` this will convert string of base x to base 10 – SirParselot Jan 25 '16 at 14:12
  • 2
    @Jeremy This *is* a place for learning. It this question https://stackoverflow.com/questions/2267362/how-to-convert-an-integer-in-any-base-to-a-string has merit (with 148 votes and 53 favorite marks at the time of writing), then I argue that this question also has merit. For helpful info on bases, check out https://cs.stackexchange.com/questions/10318/the-math-behind-converting-from-any-base-to-any-base-without-going-through-base. – Jonathan Komar Feb 22 '18 at 07:43

4 Answers4

10

The int() built-in function supports conversion of any number to any base. It requires a passed correct number within the base or else throws a ValueError.

Syntax: int('string', base) converts to decimal

Example:

Conversion of a number 3334 to base 5

>>> int('3334',5)

469

Conversion of number 3334 to base 9

>>>int('3334', 9)

2461

Conversion of the above to hex-decimal number

>>>hex(int('3334', 9))

'0x99d'
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Dry_accountant_09
  • 1,371
  • 16
  • 15
  • 1
    Built-ins should be preferred and used where possible, so this is the correct answer in my view. – srm Feb 15 '20 at 13:15
  • In python int() always displays/returns numbers in base 10/decimal, so int('3334', 5) isn't converting 3334⏨ to 469₅, it's actually converting 3334₅ to 469⏨, and likewise for the other examples. 469 can't be a base 5 number since it contains the digits 6 and 9. I couldn't find a mention for the return type's base in the [python docs](https://docs.python.org/3/library/functions.html#int) but converting through other means will prove this. The examples' titles confuse this point – alielbashir Jan 14 '21 at 15:58
5

I just coded the answer but was too slow. This code follows exactly daTokenizers solution

def converter(number, base):
    #split number in figures
    figures = [int(i,base) for i in str(number)]
    #invert oder of figures (lowest count first)
    figures = figures[::-1]
    result = 0
    #loop over all figures
    for i in range(len(figures)):
        #add the contirbution of the i-th figure
        result += figures[i]*base**i
    return result

converter(10,22)
>>> 22

converter(52,16)
>>> 82
Glostas
  • 1,090
  • 2
  • 11
  • 21
  • 1
    Need to fix your indentation. It's not legal python as shown. – Gil Hamilton Jan 25 '16 at 14:17
  • Thanks, didn't check after copy. This edited version should do it – Glostas Jan 25 '16 at 14:32
  • Thanks, your code looks very good and I understand it. It's really what I was looking for. I couldn't imagine that it's that simple. – Tom Wojcik Jan 26 '16 at 22:36
  • 1
    Mind you this doesn't work for numbers that contain letters in them, like the hexadecimal number 'A'. It gives `ValueError: invalid literal for int() with base 10: 'A'`. To make it work, you might want to add: `if base<=10: figures = [int(i) for i in str(number)]` `else: figures = [int(i,base) for i in str(number)]` – Kristada673 Nov 02 '18 at 06:16
3

the basic stages are so:

  1. understand what base you are in (to my understading this is given as var to you)
  2. for each of the chars in the input number you multiply it by the base to the power of the location. so "654",base 17 -> "6*17^2 + 5*17^1 + 4*17^0"
  3. the sum is your answer.
daTokenizer
  • 96
  • 1
  • 9
1

If n is the number, to convert from base 'other' to decimal, try this:

>>> other2dec = lambda n, other:  sum([(int(v) * other**i) for i, v in enumerate(list(str(n))[::-1])])
>>> other2dec(71,8)
57
>>> other2dec(1011,2)
11
Quinn
  • 4,394
  • 2
  • 21
  • 19
  • Well, it's not as simple as I expected, but it's shory and works very, very good. Now I will sit and look at this code trying to understand everything. Many thanks! – Tom Wojcik Jan 26 '16 at 22:09
  • SirParselot's suggestion `int(str, x)` is the answer you are looking for. For more info: http://stackoverflow.com/questions/209513/convert-hex-string-to-int-in-python – Quinn Jan 27 '16 at 14:21