hacker rank python(string formatting)solution

STRING FORMATTING


def print_formatted(number):
    w = len(str(bin(number)).replace('0b',''))
    for i in range(1,number+1):   
        d = str(i).rjust(w,' ')
        b = bin(i)[2:].rjust(w,' ')   ## rjust is used for line alignment
        o = oct(i)[2:].rjust(w, ' ')
        h = hex(i)[2:].rjust(w, ' ').upper()
        print(d, o, h, b)

Compiler Message
Success
Input (stdin)Download
2
Expected OutputDownload
 1  1  1  1
 2  2  2 10

Comments

NikhilDesai said…
def print_formatted(number):
# your code goes here
w = len('{0:b}'.format(n))
for i in range(1,n+1):
print("{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = w))

if __name__ == '__main__':
Nimesh said…
why line allinement is done while we could do
print(d, " ", o, " ", h, " ", b)

Popular posts from this blog

Amazon Web Services

Google Code-In mentorship experience :)

Hacker Rank all java and python problem solutions