Programming for Engineers in Python Recitation 4 Agenda

  • Slides: 14
Download presentation
Programming for Engineers in Python Recitation 4

Programming for Engineers in Python Recitation 4

Agenda �Sample problems �Hash functions & dictionaries (or next week) �Car simulation 2

Agenda �Sample problems �Hash functions & dictionaries (or next week) �Car simulation 2

A function can be an argument def do_twice(f): f() def print_spam(): print 'spam' >>>

A function can be an argument def do_twice(f): f() def print_spam(): print 'spam' >>> do_twice(print_spam) spam 3

Fermat’s last theorem � Pierre de Fermat 1601 -1665 4

Fermat’s last theorem � Pierre de Fermat 1601 -1665 4

Fermat’s last theorem >>> check_fermat(3, 4, 5, 2) No, that doesn't work >>> check_fermat(3,

Fermat’s last theorem >>> check_fermat(3, 4, 5, 2) No, that doesn't work >>> check_fermat(3, 4, 5, 3) No, that doesn't work Sir Andrew John Wiles �Dirty shortcut since 1995: 1953 def check_fermat(a, b, c, n): print "Wiles proved it doesn’t work" 5

Cumulative sum � For a given list A we will return a list B

Cumulative sum � For a given list A we will return a list B such that B[n] = A[0]+A[1]+…A[n] � Take 1: def cumulative_sum(lst): summ = [ lst[0] ] * len(lst) for i in range(1, len(lst)): summ[i] = summ[i-1] + lst[i] return summ � Take 2: def cumulative_sum(lst): return [sum(lst[0: n]) for n in range(1, len(lst)+1)] 6

Estimating e by it’s Taylor expansion from math import factorial, e term = 1

Estimating e by it’s Taylor expansion from math import factorial, e term = 1 summ = 0 k = 0 while term > 1 e-15: term = 1. 0/factorial(k) summ += term k += 1 print "Python e: ", e print “Taylor’s e: ", summ print “Iterations: ”, k 7 Brook Taylor, 1685 -1731

Estimating π by the Basel problem from math import factorial, pi, sqrt term =

Estimating π by the Basel problem from math import factorial, pi, sqrt term = 1 summ = 0 k = 1 while term > 1 e-15: term = 1. 0/k**2 summ += term k += 1 summ = sqrt(summ*6. 0) 8 print "Python pi: ", pi print “Euler’s pi: ", summ print “Iterations: ”, k Leonard Euler, 1707 -1783

Ramanujan’s π estimation (optional) from math import factorial, pi term = 1 summ =

Ramanujan’s π estimation (optional) from math import factorial, pi term = 1 summ = 0 k = 0 while term > 1 e-15: term = factorial(4. 0*k) / factorial(k)**4. 0 term *= (1103. 0+26390. 0*k) / 396. 0**(4. 0*k) summ += term k += 1 summ = 1. 0/(summ * 2. 0**0. 5 / 9801. 0) 9 print "Python Pi: ", pi print "Ramanujan Pi: ", summ print “Iterations: ”, k Srinivasa Ramanujan, 1887 -1920

Triple Double Word � We want to find a word that has three double

Triple Double Word � We want to find a word that has three double letters in it, like aabbcc (which is not a word!) � Almost qualifiers: �Committee �Mississippi � Write a function to check if a word qualifies � Write a function that reads a text file and checks all 10 the words � Code: http: //www. greenteapress. com/thinkpython/code/carta lk. py � Corpus: http: //www. csie. ntu. edu. tw/~pangfeng/Fortran%20 exa mples/words. txt

Py. Game �A set of Python modules designed for writing computer games �Download &

Py. Game �A set of Python modules designed for writing computer games �Download & install: http: //pygame. org/ftp/pygame-1. 9. 2 a 0. win 32 py 2. 7. msi 11

Car game �Control a car moving on the screen �You. Tube demo: http: //www.

Car game �Control a car moving on the screen �You. Tube demo: http: //www. youtube. com/watch? v=DMOj 3 Hpjem. E �Code: https: //gist. github. com/1372753 or in car. py �Car controlled by arrows �Honk with Enter �Exit with ESC 12

To. Do List: �Fix stirring problem �Honk by pressing space �Car will go from

To. Do List: �Fix stirring problem �Honk by pressing space �Car will go from the bottom to top and from one side to the other (instead of getting stuck) �Switch to turtle! 13

2 players car game �Collision avoidance simulator: �When the cars are too close one

2 players car game �Collision avoidance simulator: �When the cars are too close one of them honks �Players need to maneuver the cars to avoid honks �Code: https: //gist. github. com/1380291 or cars. py �Red car controlled by arrows �Blue car controlled by z, x, c, s 14