Question: Please do not copy the existing answer from chegg! That one is not entirely correct. Please create a new one! Write two functions, dtob and
Please do not copy the existing answer from chegg! That one is not entirely correct. Please create a new one!
Write two functions, dtob and btod, that use the algorithms we used in class (i.e., not Pythons built-in conversion functions) to convert a non-negative integer to its binary (base 2) representation, and to convert a binary string to its corresponding decimal (base 10) integer.
Function dtob will have one parameter, n, a non-negative integer, and will return the string of 0s and 1s that is the binary representation of n.
Function btod will have one parameter, b, a string of 0s and 1s, and will return a non-negative integer which is the decimal (base 10) representation of b.
Finally, write a top-level function, main. main should (a) use Pythons built-in input function to get a non-negative decimal integer; (b) call dtob to convert the integer to binary; (c) print the value returned by dtob; (d) call btod to convert the value returned by dtob back to decimal form; (e) print the value returned by btod.
The last line in your .py file should be a call to function main: main()to check results for additional decimal (base 10) numbers. Some examples with correct results are given below:
>>>dtob(27)
'11011'
>>>dtob(0)
'0'
>>>dtob(1)
'1'
>>>dtob(2)
'10'
>>>btod('0000')
0
>>>btod('1101')
13
>>>btod('111111')
63
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
