Menu

[solved]-Write Function Increment B Takes 8 Character String Representation Binary Returns Next Lar Q39048379

Write function increment(b) that takes an 8-character stringrepresentation of a binary # and returns the next largestbinary # as an 8-character string. Example:

>>> increment(‘00000000′)’00000001’

*Python 3.7, Intro to Comp Sci class – we do not knowfor OR while loops and NOTHING fancy because we are an introclass.

  • Special case for when input is ‘11111111’. This is the largestbinary # that can be represented using 8 bits, so incrementing itcauses the value to “wrap around” to give ‘00000000’. Functionshould start by testing for this special input, and handle it byreturning ‘00000000’.

  • You should not use recursion or perform anybinary arithmetic. Make use of the conversion functionsthat you wrote for Problem 1.

  • To ensure your result has the correct length, we encourage youto do the following:

    • Use your conversion functions to determine binary representationof the result and store it in a variable.

    • Determine the length of result using len() and store that lengthin a variable.

    • If result < 8 bits long, precede the result with the correctnumber of leading ‘0’s. You can use the multiplication operator toproduce the necessary string of ‘0’s.

Functions we can call upon from Problem 1:

def dec_to_bin(n):
”’Returns string version of binary representation of n usingR–>L method”’
if n == 0:
return n
elif n == 1:
return n
else:
m = str(n%2)
dec_rest =str(dec_to_bin(n//2))+m
return dec_rest

def bin_to_dec(b):
”’Converts # from binary to decimal, returning resultinginteger”’
if b == ‘0’:
return 0
elif b == ‘1’:
return 1
else:
b_rest = bin_to_dec(b[:-1])*2 + int(b[-1])
return b_rest

Expert Answer


Answer to Write function increment(b) that takes an 8-character string representation of a binary # and returns the next largest b… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *