1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Damm [24]
2 years ago
5

6.23 LAB: Convert to binary - functions Instructor note: This is a lab from a previous chapter that now requires the use of a fu

nction. 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)
Computers and Technology
1 answer:
Kitty [74]2 years ago
3 0

Answer:

The program in Python is as follows:

import math

def integer_to_reverse_binary(integer_value):

   remainder = ""

   while integer_value>=1:

       remainder+=str(integer_value % 2)

       integer_value=math.floor(integer_value/2)

   reverse_string(remainder)

def reverse_string(input_string):

   binaryOutput=""

   for i in range(len(input_string)-1,-1,-1):

       binaryOutput = binaryOutput + input_string[i]

   print(binaryOutput)

integer_value = int(input("Enter a Number : "))

integer_to_reverse_binary(integer_value)

Explanation:

This imports the math module

import math

This defines the integer_to_reverse_binary function

def integer_to_reverse_binary(integer_value):

This initializes the remainder to an empty string

   remainder = ""

This loop is repeated while the integer input is greater than or equal to 1

   while integer_value>=1:

This calculates the remainder after the integer value is divided by 2

       remainder+=str(integer_value % 2)

This gets the floor division of the integer input by 2

       integer_value=math.floor(integer_value/2)

This passes the remainder to the reverse_string function

   reverse_string(remainder)

The reverse_string function begins here

def reverse_string(input_string):

This initializes the binaryOutput to an empty string

   binaryOutput=""

This iterates through the input string i.e. the string of the remainders

   for i in range(len(input_string)-1,-1,-1):

This reverses the input string

       binaryOutput = binaryOutput + input_string[i]

This prints the binary output

   print(binaryOutput)

<u>The main begins here</u>

This gets input for integer_value

integer_value = int(input("Enter a Number : "))

This passes the integer value to the integer_to_reverse_binary function

integer_to_reverse_binary(integer_value)

You might be interested in
I want to build a video player on the html5 canvas here is my code
jok3333 [9.3K]

Answer:

hospital yestrday after noon ggpupg ki tyutrdcbhsjdvshsjjshsgsgdhsusfsuwwiiwhszia

8 0
1 year ago
Computer is an example of​
MAXImum [283]

Answer:

Computer is an example of electronic device.

7 0
3 years ago
Read 2 more answers
16.50. Suppose we have a sequential (ordered) file of 100,000 records where each record is 240 bytes. Assume that B = 2,400 byte
Nat2105 [25]

Answer and Explanation:

Given that total number of records in a file = 100000

Each record consists of= 240 bytes

Given that B= 2400 bytes.

Then total blocks in file = 100000 records * 240 / 2400

= 10000 blocks.

Time that will take for exhaustive read

= s + r + b.btt

= 16 + 8.3 + (10000) * 0.8

= 8024.3 msec

Now as per given X be the number of records that are searched randomly and it takes more than exhaustive read time.

Hence, X (s + r + btt) > 8024.3

X (16+8.3+0.8) > 8024.3

X > 8024.3/25.1

So that, X > 319.69

Hence, atleast 320 random reads should be made to search the file

8 0
3 years ago
How will you keep the buzz going post-hackathon?
Anna71 [15]

Answer:

it would be nice if you gave some form of info here

Explanation:

in other words without information no help aka I'm slow and just need points

8 0
1 year ago
Which camera does Angelina use?
julia-pushkina [17]

Answer:

mirrorless cameras have lot more space due to not having mirrors. and they have the same or better quality than DSLR's.

8 0
3 years ago
Other questions:
  • What are the two elements of creating a flyer
    12·2 answers
  • Design a module named getNumber, which uses a reference parameter variable to accept an Integer argument. The module should prom
    6·1 answer
  • You should use the longest possible shutter speed for all firework photographs.
    8·2 answers
  • Advancements in technology that might be helpful for individuals who need accommodations to perform computer tasks include _____
    15·2 answers
  • What is a Photojournalist
    5·1 answer
  • When a person or organization uses the services of another professional, that person or organization is called the _______
    15·1 answer
  • What simple machine is most often used to lift the blinds on a window?
    8·2 answers
  • any one that owns a chrome book there is a new update there is new features and there is a game on settings where you see what y
    15·2 answers
  • Fill in the blanks <br>a) an _______________ checks the files on the computer for viruses.​
    6·2 answers
  • Hi I got a new phone and my photos are taking so long to download, on Friday it was at 13000 what do I do it’s taking so long
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!