-
Notifications
You must be signed in to change notification settings - Fork 0
/
10 string_formatting_with_padding_and_rjust.py
65 lines (50 loc) · 1.49 KB
/
10 string_formatting_with_padding_and_rjust.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Given an integer, , print the following values for each integer from to :
Decimal
Octal
Hexadecimal (capitalized)
Binary
Function Description
Complete the print_formatted function in the editor below.
print_formatted has the following parameters:
int number: the maximum value to print
Prints
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 and the values should be separated by a single space.
Input Format
A single integer denoting .
Constraints
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
'''
#%%
def print_formatted(number):
# your code goes here
for i in range (1, number+1):
normal = str(i)
octal = format(i,'o')
hexa = format(i, 'X')
binary = format(i, 'b')
maxlen = len(format(number, 'b'))
strout = normal.rjust(maxlen) + " " + octal.rjust(maxlen) + " " + hexa.rjust(maxlen) + " " + binary.rjust(maxlen)
print(strout)
if __name__ == '__main__':
n = int(input())
print_formatted(n)