String Formatting in Python - HackerRank Solution
Problem :
- Decimal
- Octal
- Hexadecimal (capitalized)
- Binary
The four values must be printed on a single line in the order specified above
for each i from 1 to n. Each value should be space-padded to match the width
of the binary value of n.
Input Format :
A single integer denoting n.
Constraints :
- 1 <= n <= 99
Output Format :
Print n lines where each line i (in the range 1< i < n ) contains
the respective decimal, octal, capitalized hexadecimal, and binary values of
i. Each printed value must be formatted to the width of the binary value of n.
Sample Input :
17
Sample Output :
1 1 1 1 2 2 2 10 3 3 3 11 4 4 4 100 5 5 5 101 6 6 6 110 7 7 7 111 8 10 8 1000 9 11 9 1001 10 12 A 1010 11 13 B 1011 12 14 C 1100 13 15 D 1101 14 16 E 1110 15 17 F 1111 16 20 10 10000 17 21 11 10001
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # String Formatting in Python - HackerRank Solution def print_formatted(number): # your code goes here # String Formatting in Python - HackerRank Solution START l1 = len(bin(number)[2:]) for i in range(1,number+1): print(str(i).rjust(l1,' '),end=" ") print(oct(i)[2:].rjust(l1,' '),end=" ") print(((hex(i)[2:]).upper()).rjust(l1,' '),end=" ") print(bin(i)[2:].rjust(l1,' '),end=" ") print("") # String Formatting in Python - HackerRank Solution END if __name__ == '__main__': n = int(input()) print_formatted(n) |
Solution 2 :
Note : This Solution is not passed the cases of hackerrank solution. but the output of this problem statement comes out true.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # String Formatting in Python - HackerRank Solution def print_formatted(number): # your code goes here # String Formatting in Python - HackerRank Solution START for i in range(1,number+1): binlen = len(str(bin(number))) octf = oct(i).split('o') hexf = hex(i).split('x') binf = bin(i).split('b') print(i , octf[1] , hexf[1].upper() , binf[1] ) # String Formatting in Python - HackerRank Solution END if __name__ == '__main__': n = int(input()) print_formatted(n) |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
both solution not working. i tried pyt 2 and 3 but didnt work
the code didn't pass the test cases because of the spaces, every format needed to be padded according to the length of binary format of 'number'
Use solution no. 1
solution 1 is not working
def print_formatted(number):
# your code goes here
x=len(str(bin(number))[2:])
for i in range(1,number+1):
print(str(i).rjust(x),str(oct(i))[2:].rjust(x),str(hex(i))[2:].rjust(x).upper(),str(bin(i))[2:].rjust(x))
if __name__ == '__main__':
n = int(input())
print_formatted(n)
this will work on all cases
What is use of 2: is here
It is being used to removed white space I think
def print_formatted(number):
if 1<=number<=99:
blen = len(str(format(number,"b")))
for i in range(1,n+1):
print("{0:d}".format(i).rjust(blen),end=' ')
print("{0:o}".format(i).rjust(blen),end=' ')
print("{0:X}".format(i).rjust(blen),end=' ')
print("{0:b}".format(i).rjust(blen))
Nce Post!
Best Python Online Course
Python Online Training Course
Problem is in end=" " which can be solved using format printing in hacker rank console
l1 = len(bin(number)[2:])
for i in range(1,number+1):
one =str(i).rjust(l1,' ')
two= oct(i)[:].rjust(l1,' ')
three= ((hex(i)[2:]).upper()).rjust(l1,' ')
four= bin(i)[2:].rjust(l1,' ')
print("{} {} {} {}".format(one,two,three,four))
def print_formatted(number):
n = number
#print decimal
dm = []
for i in range(1,n+1):
dm.append(str(i))
# print octal
s = ''
octal = []
for i in range(1,n+1):
if i >= 8:
quo = i
rem = i
while(quo != 0):
rem = rem % 8
quo = quo // 8
s = str(rem) + s
rem = quo
octal.append(s)
s = ''
else:
octal.append(str(i))
# print hexadecimal
#This method returns hexaddecimal equivalent character to that number(10-15)
def hx_equiv(rem):
if rem == 10:
return 'A'
elif rem == 11:
return 'B'
elif rem == 12:
return 'C'
elif rem == 13:
return 'D'
elif rem == 14:
return 'E'
elif rem == 15:
return 'F'
…
for i in range(1,number+1):
print(i,end= " ")
print(oct(i)[2:],end= " ")
print(hex(i)[2:],end= " ")
str=bin(i)[2:]
print(str.rjust(2," "))
for i in range(1,number+1):
print("{} {} {} {}".format(str(i).rjust(li,' '),oct(i)[1:].rjust(li,' '),hex(i).upper()[2:].rjust(li,' '),bin(i)[2:].rjust(li,' ')))
def print_formatted(number):
# your code goes here
l1 = len(bin(number)[2:])
for i in range(1,number+1):
print(str(i).rjust(l1,' '),end=' ')
print(oct(i)[2:].rjust(l1,' '),end=' ')
print(((hex(i)[2:]).upper()).rjust(l1,' '),end=' ')
print(bin(i)[2:].rjust(l1,' '),end=' ')
print('')
if __name__ == '__main__':
n = int(input())
print_formatted(n)
inputs = input().split(" ")
n,m = [int(inputs[i]) for i in range(0,len(inputs))]
for i in range(0,n//2):
string = "-" *(m//2 -(1+3*i))
string = string+".|."*(1+2*i)
string = string+ "-"*(m//2 -(1+3*i))
print(string)
string = "-"*((m-7)//2) + "WELCOME" + "-"*((m-7)//2)
print(string)
for i in range((n//2)-1,-1,-1):
string = "-" *(m//2 -(1+3*i))
string = string+".|."*(1+2*i)
string = string+ "-"*(m//2 -(1+3*i))
print(string)
This can also be a solution
def print_formatted(number):
l = len(bin(number)[2:])
for n in range(1,number+1) :
print(f'{n:>{l}} {n:>{l}o} {n:>{l}x} {n:>{l}b}')
def print_formatted(number):
for i in range(1,number+1):
length_binary=len(bin(number)[2:])
print(f"{str(i).rjust(length_binary,' ')} {str(oct(i)[2:]).rjust(length_binary,' ')} {str(hex(i)[2:]).upper().rjust(length_binary,' ')} {str(bin(i)[2:]).rjust(length_binary,' ')} ")
if __name__ == '__main__':
n = int(input())
print_formatted(n)