Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a new function, dtobr that implements a recursive version of the algorithm implemented in p42_binary.py to convert a non-negative integer to its binary (base

Write a new function, dtobr that implements a recursive version of the algorithm implemented in p42_binary.py to convert a non-negative integer to its binary (base 2) representation.

As is the case for function dtob, dtobr 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.

Write test cases for dtobr and dtob (these can be the same). Test cases should include simple examples (as always), tests for boundary or edge conditions, and other examples to ensure adequate testing. Include the tests, and comments indicating what they are testing, in the docstrings of dtob and dtobr.

Next, write another new function, btodr, that implements a recursive version of the algorithm implemented in p42_binary.py to convert a binary string to its corresponding decimal integer.

As is the case for function btod, btodr, will have one parameter, b, a string of 0s and 1s, and will return a non-negative integer which is the decimal representation of b.

Write test cases for btod and btodr (these can be the same). Test cases should include simple examples (as always), tests for boundary or edge conditions, and other examples to ensure adequate testing. Include the tests, and comments indicating what they are testing, in the docstrings of btod and btodr.

Finally, revise the main function: call dtobr and btodr to convert a decimal number to binary and back again from a user input number.

Add code to your .py file to check the test cases included in the function docstrings: doctest.testmod().

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.

Since the program calls doctest.testmod, you should not include example function calls in the docstring for main. As usual, one very short description is enough documentation for the main function (see posted solutions).

Function dtobr should return correct results for your test cases and also for the test cases here:

>>>dtobr(27)

'11011'

>>>dtobr(8)

'1000'

>>>dtobr(44)

'101100'

>>>dtobr(0)

'0'

Function btodr should return correct results for your test cases and also for the test cases here:

>>>btodr('0000')

0

>>>btodr('1101')

13

>>>btodr('111111')

63

given code:

 import doctest def dtob(n): '''(int) -> str Convert non-negative decimal integer n to binary string. >>> dtob(4) '100' >>> dtob(0) '0' >>> dtob(1) '1' >>> dtob(2) '10' >>> dtob(27) '11011' >>> dtob(44) '101100' ''' assert n >= 0 assert isinstance(n, int) if n == 0: b = '0' else: b = '' next_n = n while next_n > 0: r = next_n % 2 b = str(r) + b next_n = next_n // 2 return b def btod(b): '''(str) -> integer Convert binary string to decimal integer. >>> btod('100') 4 >>> btod('11111') 31 >>> btod('0000') 0 >>> btod('11011') 27 >>> btod('101100') 44 ''' # type checking for bit in b: assert bit in '01' ''' # or for bit in b: if bit not in '01': raise Exception('Not a binary string.') ''' dn = 0 bctr = len(b) - 1 for bit in b: dn += int(bit) * (2**bctr) bctr -= 1 return dn def main(): '''() -> None check binary conversion functions by encoding and decoding number ''' d = int((input('Enter non-negative integer: '))) b = dtob(d) # convert to binary print('Binary format is {}'.format(b)) checkd = btod(b) # convert back to decimal print('Back to decimal: {}'.format(checkd)) return None print(doctest.testmod()) main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Brad Bulger, Jay Greenspan, David Wall

2nd Edition

0764549634, 9780764549632

Students also viewed these Databases questions

Question

List 3 ways the text book suggest that computer files can be lost

Answered: 1 week ago