===== Cvičení 5 =====
==== 05a ====
[[ https://youtu.be/VAMxr6q1Q3E | 2D Pole ]]
mat.txt:
1 2 3
4 5 6
7 8 9
read_mat1.py:
f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))
for i in pole:
print(*i, sep='\t')
pole2=[]
for x in pole:
r=[]
for i in x:
r.append(i)
pole2.append(r)
pole[0][0]=-10
print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)
read_mat2.py:
f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))
for i in pole:
print(*i, sep='\t')
pole2=[ [ i for i in x ] for x in pole ]
pole[0][0]=-10
print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)
copy_deepcopy.py:
f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))
for i in pole:
print(*i, sep='\t')
import copy
pole2=copy.deepcopy(pole)
pole[0][0]=-10
print("pole2")
for r in pole2:
print(*r)
print("pole")
for r in pole:
print(*r)
==== 05b ====
[[ https://youtu.be/xuRBNxCVhYA | Práce s 2D polem ]]
Násobení matic:
f=open("mat.txt")
pole=[]
for l in f:
pole.append(list(map(int,l.split())))
f=open("mat2.txt")
pole2=[]
for l in f:
pole2.append(list(map(int,l.split())))
def print_mat(m):
for r in range(len(m)):
for s in range(len(m[r])):
print(m[r][s],end=' ')
print()
def mult_mat(a,b):
res=[]
for r in range(len(a)):
res_r=[]
for s in range(len(b[r])):
suma=0
for j in range(len(a[r])):
suma+=a[r][j]*b[j][s]
res_r.append(suma)
res.append(res_r)
return res
print("pole")
print_mat(pole)
print("pole2")
print_mat(pole2)
mult = mult_mat(pole,pole2)
print("Vysledek")
print_mat(mult)
mult2 = mult_mat(pole2,pole)
print("Vysledek2")
print_mat(mult2)
mat2.txt:
0 1 0
1 0 0
0 0 1
==== 05c ====
[[ https://youtu.be/89Kq3qkykqc | Piškvorky ]]
piskvorky.py:
def print_pole(m):
for i in m:
for j in i:
c='.'
if j==1:
c='X'
elif j==2:
c='O'
print(c,end='')
print()
dir_r=[ 1, 1, 1, 0]
dir_s=[-1, 0, 1, 1]
def check_winner(m):
winner=0
fail=True
for r in range(len(m)):
for s in range(len(m[r])):
if m[r][s]!=0:
for dir in range(4):
if r+4*dir_r[dir]>=0 and r+4*dir_r[dir]=0 and s+4*dir_s[dir]=0 and move[0]=0 and move[1]