'How can I print an output in reverse in Python?
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.
Solution 1:[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
Solution 2:[2]
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))
Solution 3:[3]
>>> 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'
Solution 4:[4]
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
Solution 5:[5]
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
Solution 6:[6]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | trincot |
| Solution 2 | BrokenBenchmark |
| Solution 3 | user3327034 |
| Solution 4 | Blazing Blast |
| Solution 5 | |
| Solution 6 |
