1

A question is asking me to convert an int into binary, but it also must be in reverse (why!??!?!). After a bunch of tinkering, I was able to get it to print the number in binary. But I can't for the life of me figure out how to make it output in reverse.

The instructions say:

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

My code is:

x = int(input())

while x > 0:
    x = x//2
    print( x % 2, end = ' ')

Testing with input of 6, I get 1 1 0 but it wants me to output 011.

I even tried putting the answer into a list but when I try to reverse the list, I get an error. List method I tried:

x = int(input())

while x > 0:
    x = x//2
    J = [x % 2]

    L = reversed(J)

    print(L)

output using list method:

<list_reverseiterator object at 0x7f2cd69484f0>
<list_reverseiterator object at 0x7f2cd6948ee0>
<list_reverseiterator object at 0x7f2cd69484f0>

I feel like there's no way this needs some sort of slicing since that method hasn't even been covered yet in the material I'm learning.

7 Answers 7

1

You didn't follow the provided algorithm steps in the given order. Swap the statements in the while loop so they align with what was described.

And a small detail: there was no instruction to separate the output with spaces, so you should provide end = '':

x = int(input())

while x > 0:
    print( x % 2, end = '')
    x = x//2
Sign up to request clarification or add additional context in comments.

Comments

1

This code worked perfectly fine for my zybooks assignment test.

a: int = int(input())

if a > 0:
    a_str = f'{a:b}'
    print(a_str[::-1])

Also this is the textbook answer:

x: int = int(input())

while x > 0:
   print(x % 2, end='')
   x = x // 2
print()

Comments

0

You're reading in the least significant bit first, which results in the output being reversed. You don't need to make an explicit call to reversed().

This produces the desired output:

x = int(input())

result = []
while x > 0:
    result.append(x % 2)
    x = x // 2

# str() transforms each integer to a string.
# We then concatenate them all together
# to get the desired output using ''.join().
print(''.join(str(item) for item in result))

Comments

0
>>> x = 100
>>> res = []
>>> while x > 0:
...   x = x//2
...   J = x%2
...   res.append(J)
...
>>> res
[0, 1, 0, 0, 1, 1, 0]
>>> "".join(str(i) for i in res[::-1])
'0110010'

Comments

0
step1 = input("what number? ")#gets your input
step2 = int(step1) #makes sure it's an int not float
step3 = bin(step2) #converts it to binairy (you method doesn't work for e.g. 7)
step4 = step3.replace("0b", "") #removes 0b from the binairy number
step5 = step4[::-1] #reveses the string
print (step5)

should work or

print(bin(int(input("what number? "))).replace("0b", "")[::-1])

if you want in more compressed

Comments

0

you can use special function of python to convert integer to binary input and the print reverse of binary input by [:1:-1] in J list, so:

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
print(binary_of_integer_input[2:])               # Print binary of integer input
print(binary_of_integer_input[:1:-1]) # Print reverse binary of integer input

Example:

integer number = 8

binary of input = 1000

reverse of binary = 0001

integer_input = int(input())                     # input integert number
binary_of_integer_input = bin(integer_input)     # convert integer to binary
x = binary_of_integer_input[2:]
J = binary_of_integer_input[:1:-1]
print(x)                                         # Print binary of integer input
print(J)                                         # Print reverse binary of integer input

Comments

0

I am taking This class!!!! Here's a code with materials learned so far that works! For actual Binary. Except reversing a string may not have been mentioned [::-1].

The lab wants answers per strictly that algorithm. So reversed binary and expects it to end with new line.

num = int(input())

while num > 0:
    y =(num % 2)
    print(y, end='')
    num = (num//2)
print()

The Note: The above algorithm outputs the 0's and 1's in reverse order. If interpreted "as Convert to binary using this algorithm but reverse it"

num = int(input("Enter a number"))
string = ""

while num > 0:
    y =str(num % 2)
    string+=y
    num = (num//2)

reverse=string[::-1]
print(reverse)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.