52.5k views
0 votes
6.23 LAB: Convert to binary - functions Instructor note: This is a lab from a previous chapter that now requires the use of a function. If you weren't able to solve it before, think of this as a new opportunity. Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x // 2 Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string. Ex: If the input is: 6 the output is: 110 Your program must define and call the following two functions. The function integer_to_reverse_binary() should return a string of 1's and 0's representing the integer in binary (in reverse). The function reverse_string() should return a string representing the input string in reverse. def integer_to_reverse_binary(integer_value) def reverse_string(input_string)

2 Answers

3 votes

Final answer:

The task requires defining two functions, one to convert an integer to a binary string in reverse order, and another to reverse a string. Together, they can convert an integer to its binary representation.

Step-by-step explanation:

To convert a positive integer to its binary representation, we can implement a function integer_to_reverse_binary that follows the provided algorithm. The binary number is built by taking the remainder when the integer is divided by 2 (using the modulus operator %) and then dividing the integer by 2 (using the floor division operator //) until the integer becomes zero. Since this process creates the binary number in reverse, we also need a function called reverse_string to reverse the string for the correct binary representation.

Function Definitions

The integer_to_reverse_binary function:

def integer_to_reverse_binary(integer_value):
binary_string = ''
while integer_value > 0:
binary_string += str(integer_value % 2)
integer_value = integer_value // 2
return binary_string

The reverse_string function:

def reverse_string(input_string):
return input_string[::-1]

Complete Program

Here's how these functions can be used together in a program:

def main():
user_input = int(input("Enter a positive integer: "))
reverse_binary = integer_to_reverse_binary(user_input)
correct_binary = reverse_string(reverse_binary)
print("The binary representation is:", correct_binary)

main()

With this program, if a user inputs the integer 6, the program will output '110' as the binary representation.

User Willy Wonka
by
5.7k points
2 votes

Final answer:

A program is required for converting a positive integer to its binary representation in reverse order and then reversing that string for the correct binary form. This is achieved using two functions: integer_to_reverse_binary() for creation of the binary string in reverse, followed by reverse_string() to obtain the final binary representation.

Step-by-step explanation:

The lab question involves writing a program that converts a positive integer to binary representation using two functions. To accomplish this, let's define the first function, integer_to_reverse_binary(), which will repeatedly divide the input integer by 2 and collect the remainders. This process will create a binary string in reverse order. The second function, reverse_string(), will simply take a string as input and reverse it.

We can implement these functions as follows:

def integer_to_reverse_binary(integer_value):
binary_string = ''
while integer_value > 0:
binary_string += str(integer_value % 2)
integer_value //= 2
return binary_string

def reverse_string(input_string):
return input_string[::-1]

To use these functions, the program can first call integer_to_reverse_binary() to convert a given integer to a reversed binary string, and then call reverse_string() to reverse the string and obtain the correct binary representation of the integer.

User Cloudanger
by
5.5k points