Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please answer the following question: Programming Language: Python There are als

ID: 3733557 • Letter: P

Question

Please answer the following question:
Programming Language: Python

There are also 2 test codes for you to check the code to see if it gets the desired outputs.

Test Code 1:

Test Code 2:

The function has this signature: def fraction_to _binary(fraction_string, significant_binary_digits) Convert a string of decimal digits upto 32 digits long representing a positive decimal fractional value into its equivalent binary digit string. For this function to terminate the algorithm accepts the number of significant binary digits to produce. Binary digits in the result are significant if they are the first "1" in the result or any digits following the first "1". The last significant binary digit must be rounded in the final result. So the algorithm will print more digits than the specified number

Explanation / Answer

The function asked in the question:

def fraction_to_binary(fraction_string, significant_binary_digits):
binary = []
  
integral = int(float(fraction_string))
fraction = float(fraction_string) - integral
  
sig = int(significant_binary_digits)
  
while(integral):
remainder = integral % 2
binary.append(str(remainder))
integral = integral // 2
  
binary.reverse()
binary.append(".")
  
while(sig):
fraction *= 2
fract_bit = int(fraction)

if (fract_bit == 1):
fraction -= fract_bit
binary.append("1")
print "1 ",
else:
binary.append("0")
print "0 ",
sig -= 1
  
print " "
return ''.join(binary)


***NOTE: This is assuming input is taken using raw_input(). Also, the function returns the binary string and does not print the entire string.