Question

Python Programing : Convert to binary - functions Write a program that takes in a positive...

Python Programing

: Convert to binary - functions

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)

Note: This is a lab from a previous chapter that now requires the use of a function.

0 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
def integer_to_reverse_binary(integer_value):
    s = ''
    while integer_value > 0:
        s += str(integer_value % 2)
        integer_value //= 2
    return s


def reverse_string(input_string):
    reversed_string = ''
    for ch in input_string:
        reversed_string = ch + reversed_string
    return reversed_string


if __name__ == '__main__':
    print(reverse_string(integer_to_reverse_binary(int(input()))))

110 Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Python Programing : Convert to binary - functions Write a program that takes in a positive...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 5.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    5.14 LAB: Convert to binary 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 modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the...

  • 4.14 LAB: Convert to binary Write a program that takes in a positive integer as input,...

    4.14 LAB: Convert to binary 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. Ex: If the input is: 6 the output is:...

  • 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.

    #In Coral  CHALLENGE ACTIVITY 7.1.1: Convert to binary.  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 e Output x % 2 (remainder is either e or 1) x= x / 2 Note: The above algorithm outputs the O's and 1's in reverse order. Ex: If the input is 6, the output is: 011 (6 in binary is 110; the...

  • Python question: Write a program that takes in a positive integer as input, and outputs a...

    Python question: 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 modulo 2 (remainder is either 0 or 1) Assign x with x divided by 2

  • Write a python program write a function numDigits(num) which counts the digits in int num. Answer...

    Write a python program write a function numDigits(num) which counts the digits in int num. Answer code is given in the Answer tab (and starting code), which converts num to a str, then uses the len() function to count and return the number of digits. Note that this does NOT work correctly for num < 0. (Try it and see.) Fix this in two different ways, by creating new functions numDigits2(num) and numDigits3(num) that each return the correct number of...

  • Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9.

     4.18 LAB: Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Exx If the input is: 1995 the output is: yes Ex If the input is: 42,000 1995! the output is no Hint: Use a loop and the Character isDigitO function. 418.1: LAB: Checker for integer string

  • A heap can be encoded either as an array, or as a full binary tree. For...

    A heap can be encoded either as an array, or as a full binary tree. For this question, write a function that takes the array representation of a heap and outputs its binary tree representation. More specifically, you should write a function with the specifications given below. Specifications for the function: # def arrayToTree(A, j): # input: array A representing a heap, an index j in [0:len(A)] # output: a Node object storing the heap with root j in the...

  • 1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat,...

    1. Write a Python program, in a file called concat_strings.py, that includes the following functions: orderedConcat, a recursive function that takes two alphabetically-ordered strings and merges them, leaving the letters in alphabetical order. Note that the strings may be different lengths. a main method that inputs two ordered strings and calls the orderedConcat method to return a resulting merged, ordered string. Your main method should print the two input strings and the result. Note: You may not use any of...

  • Write a program that takes as input a positive real number s and a positive real...

    Write a program that takes as input a positive real number s and a positive real number E and outputs the approximate value of Vs to within the tolerance E. In other words, the output of your program should be a number s' such that Is SIS E. (Thus the distance" between the output s' and the correct answer Vs should be at most E.) Your program should implement a very old algorithm for approximating the square root of numbers,...

  • Write a Python program which implements the following two classical cryptosystem which we covered n class:...

    Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT