Printing Character Map - python

I need to write a program that gives an output of the following table:
chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: # A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
Any help would be appreciated, though I would like to ask to not be given the entire answer, but rather hints, so there is some challenge involved for me. Thanks.

The ord and chr functions will help you out:
ord('a') # 97
chr(97) # 'a'
Add to a range, and you got a stew going on!

for i in range(32,128):
print (i, chr(i))
or to be even closer to what you want:
#!/usr/bin/python3
def f(x,y):
for i in range(x,y):
print ('%3d '%i,end=''),
print()
for i in range(x,y):
print ('%3s '%chr(i),end='')
print()
for x in range(32,128,16):
f(x,x+16)

print '''chr: ! " # $ % & ' ( ) * + , - . /
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: # A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127'''

Hint: use loops in the loops with given range thus distinguishing lines as cases. The converted characters may be added as strings together and you only print the line after you have all strings added.
The full code would be:
for i in range (2,14):
#range(1,13 would have been correct as well, but I want to use the parity)
if i%2==0: #use even numbers for "chr..." line
a="chr: "
for j in range(int((i/2-1)*16+32),int((i/2-1)*16+48)):
#range is a bit complicated due to the i-range
b=str(chr(j))
#the following ifs are used to regulate space depending on character length
if len(b)==1:
s=" "
if len(b)==2:
s=" "
if len(b)==3:
s=" "
a=a+b+s #add new characters with space to the previous ones
print(a)
if i%2==1: #use odd numbers for asc:... line
a="asc: "
for j in range(int(((i-1)/2-1)*16+32),int(((i-1)/2-1)*16+48)):
b=str(j) #in this line you need only the numbers
#the following ifs are used to regulate space depending on character length
if len(b)==1:
s=" "
if len(b)==2:
s=" "
if len(b)==3:
s=" "
a=a+b+s
print(a)

A good friend gave me this tip, and it works! The remaining thing to do is to adjust placeholders:
for i in range (0, 12):
if i%2 ==0:
content ="chr:"
for j in range (32, 48):
content=content+" "+str(chr(j+(i//2)*16))
print (content)
if i%2 ==1:
content = "asc:"
for j in range (32, 48):
content=content+" "+str(j+(i//2)*16)
print (content)

Forgive me as I am just beginning but the following worked for me:
i=32
while i <= 112:
print ('chr:\t'+chr(i)+'\t'+chr(i+1)+'\t'+chr(i+2)+'\t'+chr(i+3)+'\t'+chr(i+4)+'\t'+chr(i+5)+'\t'+chr(i+6)+'\t'+chr(i+7)+'\t'+chr(i+8)+'\t'+chr(i+9)+'\t'+chr(i+10)+'\t'+chr(i+11)+'\t'+chr(i+12)+'\t'+chr(i+13)+'\t'+chr(i+14)+'\t'+chr(i+15))
print ('asc:\t'+str(i)+'\t'+str(i+1)+'\t'+str(i+2)+'\t'+str(i+3)+'\t'+str(i+4)+'\t'+str(i+5)+'\t'+str(i+6)+'\t'+str(i+7)+'\t'+str(i+8)+'\t'+str(i+9)+'\t'+str(i+10)+'\t'+str(i+11)+'\t'+str(i+12)+'\t'+str(i+13)+'\t'+str(i+14)+'\t'+str(i+15))
i=i+16
Now I am just trying to create a second loop to take care of the progression/iteration

Related

Python, how can I execute the output in tabular form: ten code-symbol pairs in each line?

def display_code_ascii():
for i in range(32, 128):
print(chr(i))
print(display_code_ascii())
This is my code. the output is:
!
"
#
$
%
&
'
(
)
*
+
,
-
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
#
A
B
C
D
E
But I want to print in a console like this:
32 is 33 is ! 34 is " 35 is # 36 is $ 37 is % 38 is & 39 is ' 40 is ( 41 is )
42 is * 43 is + 44 is , 45 is - 46 is . 47 is / 48 is 0 49 is 1 50 is 2 51 is 3
52 is 4 53 is 5 54 is 6 55 is 7 56 is 8 57 is 9 58 is : 59 is ; 60 is < 61 is =
62 is > 63 is ? 64 is # 65 is A 66 is B 67 is C 68 is D 69 is E 70 is F 71 is G
72 is H 73 is I 74 is J 75 is K 76 is L 77 is M 78 is N 79 is O 80 is P 81 is Q
82 is R 83 is S 84 is T 85 is U 86 is V 87 is W 88 is X 89 is Y 90 is Z 91 is [
92 is \ 93 is ] 94 is ^ 95 is _ 96 is ` 97 is a 98 is b 99 is c 100 is d 101 is e
102 is f 103 is g 104 is h 105 is i 106 is j 107 is k 108 is l 109 is m 110 is n 111 is o
112 is p 113 is q 114 is r 115 is s 116 is t 117 is u 118 is v 119 is w 120 is x 121 is y
122 is z 123 is { 124 is | 125 is } 126 is ~ 127 is None
# set chunk_size which is number of initial elements to handle per each outputted line
chunk_size = 10
def format_element(x):
x = chr(x)
return x if x != '\x7f' else "None"
# prepare initial list elements with output strings
ll = [f"{x} is {format_element(x)}" for x in range(32, 128)]
# split list into chunks using chunk_size
ll = [ll[i:i+chunk_size] for i in range(len(ll))[::chunk_size]]
# join inner lists into output lines strings
ll = [" ".join(x) for x in ll]
# print each line separately
for i in ll:
print(i)
Output:
32 is 33 is ! 34 is " 35 is # 36 is $ 37 is % 38 is & 39 is ' 40 is ( 41 is )
42 is * 43 is + 44 is , 45 is - 46 is . 47 is / 48 is 0 49 is 1 50 is 2 51 is 3
52 is 4 53 is 5 54 is 6 55 is 7 56 is 8 57 is 9 58 is : 59 is ; 60 is < 61 is =
62 is > 63 is ? 64 is # 65 is A 66 is B 67 is C 68 is D 69 is E 70 is F 71 is G
72 is H 73 is I 74 is J 75 is K 76 is L 77 is M 78 is N 79 is O 80 is P 81 is Q
82 is R 83 is S 84 is T 85 is U 86 is V 87 is W 88 is X 89 is Y 90 is Z 91 is [
92 is \ 93 is ] 94 is ^ 95 is _ 96 is ` 97 is a 98 is b 99 is c 100 is d 101 is e
102 is f 103 is g 104 is h 105 is i 106 is j 107 is k 108 is l 109 is m 110 is n 111 is o
112 is p 113 is q 114 is r 115 is s 116 is t 117 is u 118 is v 119 is w 120 is x 121 is y
122 is z 123 is { 124 is | 125 is } 126 is ~ 127 is None

Print numbers serially in columns

I am struggling in one of the Pattern matching problems in Python
When input = 3, below is the expected output (input value is the number of columns it should print)
Expected output:
1
2 6
3 7 9
4 8
5
I am somehow moving in a wrong direction, hence would need some help in it.
This is the code I have tried so far:
def display():
n = 5
i = 1
# Outer loop for how many lines we want to print
while(i<=n):
k = i
j = 1
# Inner loop for printing natural number
while(j <= i):
print (k,end=" ")
# Logic to print natural value column-wise
k = k + n - j
j = j + 1
print("\r")
i = i + 1
#Driver code
display()
But it is giving me output as this:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Anybody who can help me with this?
n=10
for i in range(1,2*n):
k=i
for j in range(2*n-i if i>n else i):
print(k,end=' ')
k = k + 2*n - 2*j - 2
print()
Result
1
2 20
3 21 37
4 22 38 52
5 23 39 53 65
6 24 40 54 66 76
7 25 41 55 67 77 85
8 26 42 56 68 78 86 92
9 27 43 57 69 79 87 93 97
10 28 44 58 70 80 88 94 98 100
11 29 45 59 71 81 89 95 99
12 30 46 60 72 82 90 96
13 31 47 61 73 83 91
14 32 48 62 74 84
15 33 49 63 75
16 34 50 64
17 35 51
18 36
19
>
Here's a way, I started from scratch and not for code, much more easy for me
def build(nb_cols):
values = list(range(1, nb_cols ** 2 + 1))
res = []
for idx in range(nb_cols):
row_values, values = values[-(idx * 2 + 1):], values[:-(idx * 2 + 1)]
res.append([' '] * (nb_cols - idx - 1) + row_values + [' '] * (nb_cols - idx - 1))
for r in zip(*reversed(res)):
print(" ".join(map(str, r)))
Here's a recursive solution:
def col_counter(start, end):
yield start
if start < end:
yield from col_counter(start+1, end)
yield start
def row_generator(start, col, N, i=1):
if i < col:
start = start + 2*(N - i)
yield start
yield from row_generator(start, col, N, i+1)
def display(N):
for i, col_num in enumerate(col_counter(1, N), 1):
print(i, *row_generator(i, col_num, N))
Output:
>>> display(3)
1
2 6
3 7 9
4 8
5
>>> display(4)
1
2 8
3 9 13
4 10 14 16
5 11 15
6 12
7
>>> display(10)
1
2 20
3 21 37
4 22 38 52
5 23 39 53 65
6 24 40 54 66 76
7 25 41 55 67 77 85
8 26 42 56 68 78 86 92
9 27 43 57 69 79 87 93 97
10 28 44 58 70 80 88 94 98 100
11 29 45 59 71 81 89 95 99
12 30 46 60 72 82 90 96
13 31 47 61 73 83 91
14 32 48 62 74 84
15 33 49 63 75
16 34 50 64
17 35 51
18 36
19
Here is the solution using simple loops
def display(n):
nrow = 2*n -1 #Number of rows
i = 1
noofcols = 1 #Number of columns in each row
t = 1
while (i <= nrow):
print(i,end=' ')
if i <= n:
noofcols = i
else:
noofcols = 2*n - i
m =i
if t < noofcols:
for x in range(1,noofcols):
m = nrow + m -(2*x-1)
print(m, end=' ')
i = i+1
print()

From Matlab to Python Code [z,index]=sort(abs(z));

i am trying to convert code from matlab to python.
Can you please help me to convert this code from matlab to python?
in matlab code
z is list and z length is 121
z= 7.0502 5.8030 4.4657 3.0404 1.5416 0 -1.5416 -3.0404 -4.4657
-5.8030 -7.0502 7.5944 6.3059 4.8990 3.3662 1.7189 0 -1.7189 -3.3662 -4.8990 -6.3059 -7.5944 8.2427 6.9282 5.4611 3.8122 1.9735 0 -1.9735 -3.8122 -5.4611 -6.9282 -8.2427 9.0135 7.7027 6.2075 4.4590 2.3803 0 -2.3803 -4.4590 -6.2075 -7.7027 -9.0135 9.9185 8.6576 7.2038 5.4466 3.1530 0 -3.1530 -5.4466 -7.2038 -8.6576 -9.9185 10.9545 9.7980 8.4853 6.9282 4.8990 0 -4.8990 -6.9282 -8.4853 -9.7980 -10.9545 12.0986 11.0885 9.9947 8.8128 7.6119 -6.9282 -7.6119 -8.8128 -9.9947 -11.0885 -12.0986 13.3133 12.4632 11.5988 10.7649 10.0829 -9.7980 -10.0829 -10.7649 -11.5988 -12.4632 -13.3133 14.5583 13.8564 13.1842 12.5910 12.1612 -12.0000 -12.1612 -12.5910 -13.1842 -13.8564 -14.5583 15.8011 15.2238 14.6969 14.2594 13.9626 -13.8564 -13.9626 -14.2594 -14.6969 -15.2238 -15.8011 17.0207 16.5431 16.1227 15.7875 15.5684 -15.4919 -15.5684 -15.7875 -16.1227 -16.5431 -17.0207
Matlab code : [z,index]=sort(abs(z));
after the code
z = 0 0 0 0 0 0 1.5416 1.5416 1.7189 1.7189 1.9735 1.9735 2.3803 2.3803 3.0404 3.0404 3.1530 3.1530 3.3662 3.3662 3.8122 3.8122 4.4590 4.4590 4.4657 4.4657 4.8990 4.8990 4.8990 4.8990 5.4466 5.4466 5.4611 5.4611 5.8030 5.8030 6.2075 6.2075 6.3059 6.3059 6.9282 6.9282 6.9282 6.9282 6.9282 7.0502 7.0502 7.2038 7.2038 7.5944 7.5944 7.6119 7.6119 7.7027 7.7027 8.2427 8.2427 8.4853 8.4853 8.6576 8.6576 8.8128 8.8128 9.0135 9.0135 9.7980 9.7980 9.7980 9.9185 9.9185 9.9947 9.9947 10.0829 10.0829 10.7649 10.7649 10.9545 10.9545 11.0885 11.0885 11.5988 11.5988 12.0000 12.0986 12.0986 12.1612 12.1612 12.4632 12.4632 12.5910
12.5910 13.1842 13.1842 13.3133 13.3133 13.8564 13.8564 13.8564 13.9626 13.9626 14.2594 14.2594 14.5583 14.5583 14.6969 14.6969 15.2238 15.2238 15.4919 15.5684 15.5684 15.7875 15.7875 15.8011 15.8011 16.1227 16.1227 16.5431 16.5431 17.0207 17.0207
and index is
index = 6 17 28 39 50 61 5 7 16 18 27 29 38 40 4 8 49 51 15 19 26 30 37 41 3 9 14 20 60 62 48 52 25 31 2 10 36 42 13 21 24 32 59 63 72 1 11 47 53 12 22 71 73 35 43 23 33 58 64 46 54 70 74 34 44 57 65 83 45 55 69 75 82 84 81 85 56 66 68 76 80 86 94 67 77 93 95 79 87 92 96 91 97 78 88 90 98 105 104 106 103 107 89 99 102 108 101 109 116 115 117 114 118 100 110 113 119 112 120 111 121
so what is the [z,index] in python ?
Do you need to return the index? If you don't, you could use:
z = abs(z)
new_list = sorted(map(abs, z))
index = sorted(range(len(z)), key=lambda k: z[k])
where x is the output and z is the list.
EDIT:
Try that now

Python Nested Loop For Sequence of Several Rowes

How do i use a loop nested within a loop to create this output:
100
101 102
103 104 105
106 107 108 109
You can do this using a while loop with certain other variables:
>>> st, end, length = 100, 110, 1
>>> while st < end:
... print (" ".join(map(lambda x: "%3d" % x, range(st,st+length))))
... st += length
... length += 1
...
100
101 102
103 104 105
106 107 108 109
Note that I have used a lambda instead of str in the map, this is so that different width numbers don't break up the indentation.
You can similarly base this on the width of the final line as below (in which case there is no need to keep track of the end variable I was doing above):
>>> st, width = 1, 1
>>> while width <= 15:
... print (" ".join(map(lambda x: "%3d" % x, range(st, st + width))))
... st += width
... width += 1
...
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 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
Something like this???
start = 100
lines = 5
for i in range(lines):
stop = start + i + 1
for j in range(a, stop):
print j,
start = stop
print
Just use two-level for loop:
x = 100
for i in range(1, 5):
for j in range(i):
print(x, end=" ")
x += 1
print("")

Python: using for loops to output ASCII table

For a tutorial, I need to output the following table on python using nested for loops:
asc: 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
chr: 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
asc: 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
chr: # A B C D E F G H I J K L M N O
asc: 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
chr: P Q R S T U V W X Y Z [ \ ] ^ _
asc: 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
chr: ` a b c d e f g h i j k l m n o
asc: 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
chr: p q r s t u v w x y z { | } ~
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127'''
Since this is the beginners class we have not learned def. This question has been answered in previous posts but the answers use def and I cannot
So far my code looks like this:
X=0
for Rows in range(0,12,2):
X=X+1
if Rows%2==0:
print('chr:',end="")
for chrColumns in range (32,48):
print('%4s'%chr(chrColumns+(X-1)*16), end="")
print()
if Rows%2==1:
print('asc:',end="")
for ascColumns in range (16,32):
print(ascColumns+(X-1)*16,end="")
print()
I cannot find a way for the "chr:" rows to alternate with "asc:" rows. Please help me
How about something like this?
for c in range(32, 128, 16):
#print chr line
for c1 in range(c, c+16):
# print chr
#print asc line
for c2 in range(c, c+16):
# print asc
for c in range(32, 128, 16):
print('chr:',end="")
for c1 in range(c, c+16):
print('%4s'%chr(c1), end="")
print()
print('asc:',end="")
for c2 in range(c, c+16):
print('%4s'%c2, end="")
print()
Here's my inelegant solution:
P = 32
for line in range (0,6):
print ("chr:", end = " ")
for chrCols in range (P,P+16):
print (chr(chrCols), end=" ")
print ('\n' "asc: ", end = "")
for ascCols in range (P,P+15):
if ascCols < 100:
print (ascCols, end=" ")
if ascCols > 99:
print (ascCols, end=" ")
print(ascCols+1)
P = P + 16
servers = ["server1", "server2", "server3"]
table_column = 2
row_format ="{:>15}" * (table_column)
print(row_format.format("Server", "Status"))
print ("--"*18)
for server in servers:
print(row_format.format(server, "code"))
Output

Categories

Resources