Sort the Binary Values
Coding Problem Keys
Sort the Binary Values
Problem Statement
Given N pairs consisting of an integer X representing the number of bits to consider and B representing a binary value as input, the program must find the decimal values based on the following conditions: - If X is positive then consider the first X bits in the binary value.
- If X is negative then consider the last X bits in the binary value.
- If X is zero then consider all the bits in the binary value.
Finally, the program must sort (in ascending order) based on the considered binary values converted to their decimal equivalent.
Boundary Condition(s)
1 <= N <= 100Input Format
The first line contains the value of N.Output Format
The first line contains N binary values separated by a space.Example Input/Output 1
Input
4Output
Explanation
Example Input/Output 2
Input
5Output
10000100010100 11100100101011 1011001101101 101001100111010 1010101110111Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
n=int(input())
l=[]
for i in range(n):
a,b=input().split()
a=int(a)
if(a<0):
c=int(b[a:],2)
elif a>0:
c=int(b[:a],2)
else:
c=int(b,2)
l.append([c,b])
for i in sorted(l,key=lambda x:x[0]):
print(i[-1],end=" ")
# Published By PKJCODERS
(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments