Saturday, April 6, 2019

String Formatting - Hacker Rank Solution

Given an integer, , print the following values for each integer  from  to :
  1. Decimal
  2. Octal
  3. Hexadecimal (capitalized)
  4. Binary
The four values must be printed on a single line in the order specified above for each  from  to . Each value should be space-padded to match the width of the binary value of .
Input Format
A single integer denoting .
Constraints
Output Format
Print  lines where each line  (in the range ) contains the respective decimal, octal, capitalized hexadecimal, and binary values of . Each printed value must be formatted to the width of the binary value of .
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     
String Formatting - Hacker Rank Solution
We can solve this challenge using the .format tool.
"{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width)
  •  is the index indicating which format argument should be placed in that spot.
  • width indicates the padding space between two rows.
  •  and  converts the string into decimal, octal, hexadecimal, and binary format, respectively.
Note: Capital  is used to print the hexadecim

13 comments:

  1. def print_formatted(number):
    # your code goes here

    for i in range(1,number+1):
    l=len('{0:b}'.format(number))
    l=int(l)
    print('{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}'.format(i,width=l))



    if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

    ReplyDelete
  2. def print_formatted(number):
    width = len('{:b}'.format(number))
    for i in range(1,number+1):

    print(str.rjust(str(i),width),str.rjust(str(oct(i)[2:]),width),str.rjust(str(hex(i).upper()[2:]),width),str.rjust(str(bin(i)[2:]),width))
    if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

    ReplyDelete
  3. len('{0:b}'.format(number))
    please explain this

    ReplyDelete
    Replies
    1. 0 indicates the "number"
      b converts the "number" into binary

      Delete
  4. def print_formatted(number):
    # your code goes here
    for i in range(1,number+1):
    print("{} {} {} {}".format(i,oct(i)[1:],hex(i)[2:],bin(i)[2:]))

    ReplyDelete
  5. def print_formatted(number):
    # your code goes here
    for i in range(1,number+1):
    print("{} {} {} {}".format(i,oct(i)[1:],hex(i)[2:],bin(i)[2:]))

    ReplyDelete
  6. def print_formatted(number):
    # your code goes here
    for i in range(1,number+1):
    print("{} {} {} {}".format(i,oct(i)[1:],hex(i)[2:],bin(i)[2:]))

    ReplyDelete
  7. Hi ,

    Thanks for the code.

    Eventhough , the output is same as the Expected one.

    The cases are not passed.

    Could you please tell me , What exactly means hex(i)[2:]?

    seems issue related to the some padding spaces.

    Thanks in advance.

    ReplyDelete
    Replies
    1. hex is typecasting the int(i) into it's hexadecimal value and then [2:] is slicing the string after the index 2

      Delete
  8. def print_formatted(number):
    # your code goes here
    for i in range(1, n+1):
    l=len('{0:b}'.format(number))
    l=int(l)
    print('{0:{1}d} {0:{1}o} {0:{1}X} {0:{1}b}'.format(i,l))
    if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

    ReplyDelete
  9. def print_formatted(number):
    # your code goes here
    global l1, l2, l3, l4
    spac = " "

    for i in range(1, number + 1):

    oct1 = str(oct(i))[2:]
    hex1 = str(hex(i))[2:].upper()
    bin1 = str(bin(i))[2:]
    # print(f"{i} {oct1} {hex1} {bin1}")

    l1 = (len(str(i)))
    l2= (len(oct1))
    l3 = (len(hex1))
    l4 = (len(bin1))
    # print(f"{l1,l2,l3,l4}")

    for i in range(1, number + 1):
    oct1 = str(oct(i))[2:]
    hex1 = str(hex(i))[2:].upper()
    bin1 = str(bin(i))[2:]
    # print(f"{i} {oct1} {hex1} {bin1}")

    l11 = (len(str(i)))
    l22 = (len(oct1))
    l33 = (len(hex1))
    l44 = (len(bin1))
    # print(f"{l11, l22, l33, l44}")

    s1 = l4 - l11
    s2 = l4 - l22
    s3 = l4 - l33
    s4 = l4 - l44


    print(f"{s1 *spac}{i} {s2 *spac}{oct1} {s3 *spac}{hex1} {s4 *spac}{bin1}")







    if __name__ == '__main__':
    n = int(input())
    print_formatted(n)

    ReplyDelete

Powered by Blogger.