In: Computer Science
Is there anyway I could get an example to set this python function set up? any help would be amazing.
basetonum (S, B) --- int:
This function accepts as input a string S and a base B (int) where S represents a number in base B where B is between 2 and 10 inclusive. It should then return an integer in base 10 representing the same number as S. It should output 0 when S is the empty string. This function is the inverse of the previous function numtobase().
Parameters: S (string), B (int)
Returns : integer
The function displays nothing.
Steps to convert any base system to decimal:
Step 1 − Determine the column (positional) value of each digit
(this depends on the position of the digit and the base of the
number system).
Step 2 − Multiply the obtained column values (in Step 1) by the
digits in the corresponding columns.
Step 3 − Sum the products calculated in Step 2. The total is the equivalent value in decimal.
Example with S = '11101' and B = 2 Binary Number − 11101 Calculating Decimal Equivalent −
Nb43210 Step 1 11101 (1×2) + (1×2) + (1×2) + (0×2) + (1×2)
Step2 11101 16+8+4+0+1 Step 3 11101 29
Binary Number − 11101 = Decimal Number – 29
Again, the key is to ask yourself... what has to change in order to output base B instead of base 2?
In [1]: basetonum('11101', 2) Out[1]: 29
In [2]: basetonum('', 4) Out[2]: 0
Below given the python solution and the sample output to convert any base system to decimal.
val(c) and basetonum(S,B) are the
two functions used in the program to convert any base system to
decimal.