-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_stringFormatting.py
More file actions
55 lines (48 loc) · 1.48 KB
/
6_stringFormatting.py
File metadata and controls
55 lines (48 loc) · 1.48 KB
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
def print_formatted(number):
"""
Given an integer,n , print the following values for each
integer i from 1 to n:
1. Decimal
2. Octal oct()
3. Hexadecimal (capitalized) hex()
4. Binary bin()
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 (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 3 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
"""
if 1<=number<=99:
width = len('{:b}'.format(number))
for i in range(1, number+1):
decimal = str.rjust(str(i), width)
octal = str.rjust((oct(i)[2:]), width)
hexadecimal = str.rjust(hex(i)[2:].upper(), width)
binary = str.rjust((bin(i)[2:]), width)
print(decimal, octal, hexadecimal, binary)