ΜΥΥ105: Εισαγωγή στον Προγραμματισμό Έλεγχος Ροής - Παραδείγματα
Δίσεκτο έτος Ένα έτος είναι δίσεκτο αν διαιρείται ακριβώς με το 400 ή διαιρείται ακριβώς με το 4 και δεν διαιρείται ακριβώς με το 100 Υπολογίστε αν ένα δοθέν έτος είναι δίσεκτο year = it(iput('eter a year: ')) if year%400 == 0: prit(year, 'is a leap year') elif year%4 == 0 ad year%100!= 0: prit(year, 'is a leap year') else: prit(year, 'is a commo year') 2
l(2) συγκλίνον άθροισμα Ο φυσικός λογάριθμος του 2 μπορεί να υπολογιστεί με την εξίσωση: l 2 = l(2) = 1/1 1/2 + 1/3 1/4 = 1X k=1 from math import log prit('computes l(2) up to a give accuracy ) log(): φυσικός error = float(iput('allowed error = ')) λογάριθµος sum=0 log(x,base)= previous=1000 #a very large iteger log(x)/log(base) sig=-1 i=1 while abs(sum-previous)>error: previous=sum sig=sig*(-1) sum=sum+sig/float(i) i=i+1 prit('%f %f %10.8f % (sum, log(2), abs(sum-log(2)))) ( 1) k+1 k 3
Προπαίδεια του 7 Τύπωσε στοιχισμένα την προπαίδεια του 7 prit ('Προπαίδεια του 7') for i i rage (1,11): prit('%2d x 7 = %2d'%(i,i*7)) 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63 10 x 7 = 70 4
Πίνακας προπαίδειας Τύπωσε στοιχισμένα τις προπαίδειες του 1 εώς του 10 prit ('Πίνακας Προπαίδειας') for i i rage (1,11): for j i rage (1,11): prit('%2dx%d=%2d'%(i,j,i*j),ed=' ') prit() Τυπώνει ένα διάστηµα στο τέλος αντί να αλλάξει γραµµή 1x1= 1 1x2= 2 1x3= 3 1x4= 4 1x5= 5 1x6= 6 1x7= 7 1x8= 8 1x9= 9 1x10=10 2x1= 2 2x2= 4 2x3= 6 2x4= 8 2x5=10 2x6=12 2x7=14 2x8=16 2x9=18 2x10=20 3x1= 3 3x2= 6 3x3= 9 3x4=12 3x5=15 3x6=18 3x7=21 3x8=24 3x9=27 3x10=30 4x1= 4 4x2= 8 4x3=12 4x4=16 4x5=20 4x6=24 4x7=28 4x8=32 4x9=36 4x10=40 5x1= 5 5x2=10 5x3=15 5x4=20 5x5=25 5x6=30 5x7=35 5x8=40 5x9=45 5x10=50 6x1= 6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 6x7=42 6x8=48 6x9=54 6x10=60 7x1= 7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 7x8=56 7x9=63 7x10=70 8x1= 8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 8x9=72 8x10=80 9x1= 9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 9x10=90 10x1=10 10x2=20 10x3=30 10x4=40 10x5=50 10x6=60 10x7=70 10x8=80 10x9=90 10x10=100 5
Τρίγωνο από αστεράκια Τυπώστε ένα ορθογώνιο τρίγωνο με βάση και ύψος x αστεράκια x = it(iput('iput triagle\'s height: ')) for i i rage (1,x+1): for j i rage (1,i+1): prit('*',ed='') prit() x = 5 * ** *** **** ***** x = it(iput('iput triagle\'s height: ')) for i i rage (1,x+1): prit(i*'*') 6
Δέντρο από αστεράκια Τυπώστε ένα δέντρο από αστεράκια με ύψος x x = it(iput('iput tree\'s height: ')) for i i rage (1,x+1): for j i rage (1,x-i+1): prit(' ',ed='') for j i rage (1,2*i): prit('*',ed='') prit() x = 5 * *** ***** ******* ********* x = it(iput('iput tree\'s height: ')) for i i rage (1,x+1): prit(" "*(x-i),ed='') prit("*"*(2*i-1)) 7
Ύψωση σε δύναμη: x y x = it(iput('iput base: ')) y = it(iput('iput power: ')) value = 1 for i i rage(0, y): value *= x prit('%d to the power of %d = %d' % (x, y, value)) x = it(iput('iput base: ')) y = it(iput('iput power: ')) from math import pow prit('%d to the power of %d = %d' % (x, y, pow(x,y))) 8
Παραγοντικό: x! x! = 1*2*3* *x x = it(iput('iput a umber: ')) f = 1 for i i rage(1,x+1): f *= i prit('%d! = %d' % (x,f)) x = it(iput('iput a umber: ')) from math import factorial prit('%d! = %d' % (x, factorial(x))) 9
Υπολογισμός αθροίσματος ψηφίων ακέραιου αριθμού x = 4579, dsum = 4+5+7+9 = 25 = it(iput('iput a umber: ')) dsum = 0 while >0: Ακέραια dsum += %10 διαίρεση //=10 prit('the sum of the umber\'s digits is', dsum) = it(iput('iput a umber: ')) dsum = 0 s = str() for i i s: dsum+=it(i) prit('the sum of the umber\'s digits is', dsum) 10
Υπολογισμός μέσης τιμής a = [4,5,7,9], μ = (4+5+7+9)/4 µ = - 1 1 N å N i= 0 a[ i] s = iput('iput a set of umbers separated by commas: ') a = [eval(x) for x i s.split(",")] prit('the mea of your umbers is',sum(a)/le(a)) 11
Υπολογισμός τυπικής απόκλισης a = [4,5,7,9], μ = (4+5+7+9)/4, s = - 1 1 N å N i= 0 ( a[ i] - µ ) 2 from math import sqrt s = iput('iput a set of umbers separated by commas: ') a = [eval(x) for x i s.split(",")] prit('the mea of your umbers is',sum(a)/le(a)) sumdiff = 0.0 for um i a: sumdiff += (um-sum(a)/le(a))**2 stdev = sqrt(sumdiff/le(a)) prit('the stadard deviatio is',stdev) 12
Άσκηση: άθροισμα ζυγών Δίνεται μια λίστα αριθμών. Βρές το άθροισμα των ζυγών στη λίστα s = iput('iput a set of umbers separated by commas: ') a = [it(x) for x i s.split(",")] esum = 0 for x i a: if x%2 ==0: esum = esum + x prit("the sum is: ", esum) s = iput('iput a set of umbers separated by commas: ') a = [it(x) for x i s.split(",")] prit("the sum is: ", sum([x for x i a if x%2==0])) 13
Άσκηση Δίνονται δύο λίστες αριθμών. Υπολόγισε μια νέα λίστα που περιέχει τα γινόμενα όλων των ζευγών από τις 2 λίστες Π.χ. L1 = [3,4,5], L2 = [1,2], L3 = [3,6,4,8,5,10] s1 = iput('iput a set of umbers separated by commas: ') L1 = [it(x) for x i s1.split(",")] s2 = iput('iput a set of umbers separated by commas: ') L2 = [it(x) for x i s2.split(",")] L3 = [] for x i L1: for y i L2: L3.apped(x*y) prit(l3) 14
Ορισμός και χρήση πινάκων Μπορούμε να χρησιμοποιήσουμε λίστες ως πίνακες Μπορούμε να χρησιμοποιήσουμε και πλειάδες Ποια η διαφορά; >>> A = [4,5,6,7] >>> A[2] 6 >>> B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> B[2][1] 8 μονοδιάστατος πίνακας: A = [4 5 6 7] προσπέλαση του Α i με A[i] δισδιάστατος πίνακας: B = 2 1 2 3 3 44 5 65 7 8 9 προσπέλαση του B ij με B[i][j] 15
Ορισμός και χρήση πινάκων Η αρχικοποίηση ενός πίνακα συγκεκριμένων διαστάσεων μπορεί να γίνει με την εντολή for >>> A = [0 for i i rage(10)] >>> A [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >>> M=[[0 for i i rage(3)] for i i rage(3)] >>> M [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 16
Ορισμός και χρήση πινάκων Αρχικοποίηση με τυχαίους αριθμούς radom: πακέτο που χειρίζεται δημιουργία τυχαίων αριθμών radom.radit(a,b): τυχαίος ακέραιος στο διάστημα [a,b] radom.radom(): τυχαίος πραγματικός στο διάστημα [0,1) >>> from radom import radit >>> A = [radit(0,10) for i i rage(10)] >>> A [4, 4, 6, 7, 0, 5, 9, 0, 3, 4] >>> M=[[radit(0,10) for i i rage(3)] for i i rage(3)] >>> M [[9, 6, 10], [7, 10, 2], [9, 3, 5]] 17
Πρόσθεση πινάκων Πρόσθεση δύο πικάνων διαστάσεων N x M N=M=2 A = [[0.0 for j i rage(m)] for i i rage(n)] apple apple apple B = [[0.0 for j i rage(m)] for i i rage(n)] 1 2 5 6 6 8 C = [[0.0 for j i rage(m)] for i i rage(n)] 3 4 + 7 8 = 10 12 for i i rage(n): for j i rage(m): A[i][j] = float(iput('a[%d][%d]: ' % (i, j))) for i i rage(n): for j i rage(m): B[i][j] = float(iput('b[%d][%d]: ' % (i, j))) for i i rage(n): for j i rage(m): C[i][j] = A[i][j] + B[i][j] for i i rage(n): for j i rage(m): prit('c[%d][%d]:%7.2f ' % (i, j, C[i][j]), ed = '') prit('') 18
Πολλαπλασιασμός πινάκων N x N N=2 A = [[0.0 for j i rage(n)] for i i rage(n)] B = [[0.0 for j i rage(n)] for i i rage(n)] C = [[0.0 for j i rage(n)] for i i rage(n)] for i i rage(n): for j i rage(n): A[i][j] = float(iput('a[%d][%d]: ' % (i, j))) for i i rage(n): for j i rage(n): B[i][j] = float(iput('b[%d][%d]: ' % (i, j))) for i i rage(n): for j i rage(n): for k i rage(n): C[i][j] += A[i][k]*B[k][j] for i i rage(n): for j i rage(n): prit('c[%d][%d]:%7.2f ' % (i, j, C[i][j]), ed = '') prit('') apple apple apple 1 2 5 6 19 22 3 4 7 8 = 43 50 19
Εύρεση ελάχιστου σε πίνακα N=M=2 A = [[0.0 for j i rage(n)] for i i rage(n)] for i i rage(n): for j i rage(m): A[i][j] = float(iput('a[%d][%d]: ' % (i, j))) mielem = A[0][0] for i i rage(n): for j i rage(m): if mielem > A[i][j]: mielem = A[i][j] prit('the miimum elemet is ',mielem) Ερώτηση: Τι θα επιστρέψει το mi(a); Ερώτηση: πως θα βρούµε το ελάχιστό ΚΑΙ τη θέση του; 20
Εύρεση ελάχιστου σε πίνακα Πώς να το κάνουμε με list comprehesio? N=M=2 A = [[float(iput('a[%d][%d]: ' % (i, j))) for j i rage(n)] for i i rage(n)] mielem = mi([mi(x) for x i A]) prit('the miimum elemet is ', mielem) 21
Υπολογισμός ανάστροφου πίνακα N=2 M=3 A = [[0.0 for j i rage(m)] for i i rage(n)] iva = [[0.0 for j i rage(n)] for i i rage(m)] for i i rage(n): for j i rage(m): A[i][j] = float(iput('a[%d][%d]: ' % (i, j))) iva[j][i] = A[i][j] for i i rage(m): for j i rage(n): apple 1 2 3 A = 4 5 6 2 3 1 4 A T = 42 55 3 6 prit('iva[%d][%d]:%7.2f ' % (i, j, iva[i][j]), ed = '') prit('') 22
Άσκηση Όρισε ένα άνω τριγωνικό πίνακα, με τα στοιχεία από τη διαγώνιο και πάνω =1 M=[[0 for i i rage(3)] for j i rage(3)] for i i rage(3): for j i rage(3): if i<=j: M[i][j]=1 for row i M: prit(row) 23