===== Cvičení 7 ===== ==== 07a ==== [[ https://youtu.be/H1xcNmNMYy0 | Rekurze ]] reverse_str.txt: def reverze(n, s): if n nested_sum.py sez=[1,2,[3,4,5],[[6,7],8],[9,[10]]] def nested_sum(x): soucet=0 for i in x: if type(i) == type([]): s = nested_sum(i) soucet+=s else: soucet+=i print("Soucet",x,"je",soucet) return soucet print(nested_sum(sez)) ==== 07b ==== [[ https://youtu.be/7M-FMkA4G4o | Hanoiské věže ]] hanoi.py tycky = [ [20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], [], [] ] def hanoi(n, ze, do, pom): if n>0: hanoi(n-1, ze, pom, do) t = tycky[ze].pop() if (len(tycky[do])==0 or tycky[do][-1]>t): tycky[do].append(t) else: print("ERROR - Chyba davame vesti kruh na mensi") print("0=", tycky[0], "1=", tycky[1], "2=", tycky[2]) hanoi(n-1, pom, do, ze) print("0=", tycky[0], "1=", tycky[1], "2=", tycky[2]) hanoi(len(tycky[0]), 0, 2, 1) ==== 07c ==== [[ https://youtu.be/WGzS19cG0zU | Hlavolam]] hlavolam pro tisk {{ :courses:b3b33alp:cviceni:kratka_videa:skladacka2.pdf | skladacka.pdf}} hlavolam.py karty=[ [1,0,6,4], [0,4,6,4], [5,0,7,2], [1,5,7,3], [0,5,7,3], [1,5,7,2], [2,4,7,2], [3,1,6,2], [7,5,1,2]] def souhlas(a,b): if a>b: a,b=b,a return (a%2==0) and (a+1==b) pouzit = [0]*9 jaka = [-1]*9 otoc = [0]*9 def pasuje(n, k, o): if (n==0): return True elif (n==1 or n==2): #test vlevo return souhlas(karty[jaka[n-1]][(1-otoc[n-1])%4], karty[k][(3-o)%4]) elif (n==3 or n==6): #test nahore return souhlas(karty[jaka[n-3]][(2-otoc[n-3])%4], karty[k][(0-o)%4]) else: #test vlevo i nahore return souhlas(karty[jaka[n-1]][(1-otoc[n-1])%4], karty[k][(3-o)%4]) and souhlas(karty[jaka[n-3]][(2-otoc[n-3])%4], karty[k][(0-o)%4]) def najdi(n): if (n>=9): for l in range(9): print("Karta",l,"je",jaka[l],"otoceni",otoc[l]) print("-------------------------------------") else: k=0 while (k<9): if pouzit[k]==0: o=0 while (o<4): if pasuje(n,k,o): pouzit[k]=1 otoc[n]=o jaka[n]=k najdi(n+1) pouzit[k]=0 o+=1 k+=1 najdi(0) hlavolam_animace.py (potřebuje instalované opencv z https://opencv.org/) Také potřebujete obrázky skládačky ve stejném adresáři, ze kterého python spouštíte {{ :courses:b3b33alp:cviceni:kratka_videa:parts.tgz | parts.tgz}} import cv2 import numpy as np import time karty=[ [1,0,6,4], [0,4,6,4], [5,0,7,2], [1,5,7,3], [0,5,7,3], [1,5,7,2], [2,4,7,2], [3,1,6,2], [7,5,1,2]] def souhlas(a,b): if a>b: a,b=b,a return (a%2==0) and (a+1==b) parts=[] for i in range(9): parts.append(cv2.imread("part"+str(i)+".png")) img = np.zeros((3*235, 3*235, 3), np.uint8); def print_img(): img.fill(255) for r in range(3): for s in range(3): if jaka[r*3+s]>=0: i=parts[jaka[r*3+s]] if otoc[r*3+s]==1: i=cv2.rotate(parts[jaka[r*3+s]], cv2.ROTATE_90_CLOCKWISE) elif otoc[r*3+s]==2: i=cv2.rotate(parts[jaka[r*3+s]], cv2.ROTATE_180) elif otoc[r*3+s]==3: i=cv2.rotate(parts[jaka[r*3+s]], cv2.ROTATE_90_COUNTERCLOCKWISE) img[235*r:235*r+i.shape[0],235*s:235*s+i.shape[1]] = i cv2.imshow("Animation", img) cv2.waitKey(10) pouzit = [0]*9 jaka = [-1]*9 otoc = [0]*9 def pasuje(n, k, o): if (n==0): return True elif (n==1 or n==2): #test vlevo return souhlas(karty[jaka[n-1]][(1-otoc[n-1])%4], karty[k][(3-o)%4]) elif (n==3 or n==6): #test nahore return souhlas(karty[jaka[n-3]][(2-otoc[n-3])%4], karty[k][(0-o)%4]) else: #test vlevo i nahore return souhlas(karty[jaka[n-1]][(1-otoc[n-1])%4], karty[k][(3-o)%4]) and souhlas(karty[jaka[n-3]][(2-otoc[n-3])%4], karty[k][(0-o)%4]) def najdi(n): if (n>=9): for l in range(9): print("Karta",l,"je",jaka[l],"otoceni",otoc[l]) print("-------------------------------------") time.sleep(5) else: k=0 while (k<9): if pouzit[k]==0: o=0 while (o<4): if pasuje(n,k,o): pouzit[k]=1 otoc[n]=o jaka[n]=k print_img() najdi(n+1) pouzit[k]=0 jaka[n]=-1 o+=1 k+=1 najdi(0)