In: Computer Science
Question:
Using this code as a starting point, restructure the solution so that each sub-task (there are three) is performed in its own, separate function. When done, the function toDecimal should (1) do none of the actual "work" itself and (2) should call three separate functions in an appropriate order to complete the conversion process. This is similar to the work you did in exercise 6.4 (cengage mindtap) to restructure function newton. However, there is no recursion involved in this code.
CODE:
def toDecimal(rep, base):
"""
Converts a number in any base from 2 to 16 to a decimal (base 10)
equivalent value.
Parameters
----------
rep : string
A string representation of an integer value in some base.
base : integer
An integer representing the base.
Returns
-------
decimal : integer
The base-10 value of the provided representation
"""
# The conversion table for bases up to 16
repTable = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, ‘5': 5, '6':6, '7': 7,
'8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14,'F': 15}
# Make sure that the offered string has SOME data, otherwise, return zero
if len(rep) < 1:
return 0
# The base must be between 2 and 16, inclusive, else return 0
elif base < 2 or base > 16:
return 0
else:
# The arguments are good, let's go
decimal = 0
exp = len(rep) - 1
for digit in rep:
decimal += repTable[digit] * base ** exp
exp -= 1
return decimal
# Explanation
# I have split toDecimal(rep,base) function into three functions : data_check(rep) , base_check(base) and return_decimal(rep,base,repTable)
# data_check(rep) function checks if some data is present or not . If data is present it returns 1 otherwise it returns 0
# In the similar way base_check(base) function checks if base is between 2 and 16 inclusive , If yes then it returns 1 , otherwise it returns 0
# Finally return_decimal(rep,base,repTable) function is called only if we get 1 in return from both the above functions : data_check(rep) and base_check(base) , which calculates the decimal value
# If we get 0 in return from any of the two functions : data_check(rep) and base_check(base) , decimal value will be set to 0
# Lastly we return decimal which can either be 0 or any value calculated from return_decimal(rep,base,repTable) function
#Functions
def toDecimal(rep, base):
# The conversion table for bases up to 16
repTable = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6':6, '7': 7,
'8': 8, '9': 9, 'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14,'F': 15}
valid_data = data_check(rep)
valid_base = base_check(base)
if valid_base == 1 and valid_data == 1:
decimal = return_decimal(rep,base,repTable)
else:
decimal = 0
return decimal
#To check if data is present or not
def data_check(rep):
if len(rep) < 1:
return 0
else:
return 1
# To check for valid base , should be between 2 and 16 inclusive
def base_check(base):
if base < 2 or base > 16:
return 0
else:
return 1
# To calculate the decimal value
def return_decimal(rep,base,repTable):
decimal = 0
exp = len(rep) - 1
for digit in rep:
decimal += repTable[digit] * base ** exp
exp -= 1
return decimal