We already know Java and C++. Why learn Python? Using Python to Implement Algorithms Tyler Moore Python has far less overhead than Java/C++ for the programmer. Python is closer to psuedo-code on the English-pseudocode-code CSE 3353, SMU, Dallas, TX spectrum than Java or C/C++, but it actually executes! Lecture 2 Python is handy for data manipulation and transformation, and anything ”quick and dirty.” Python is very powerful, thanks to all those extension modules. Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos 2 / 33 Python Resources Selection Sort (C) s e l e c t i o n s o r t ( int s [ ] , int n) 1 Download from python.org for all major platforms { 2 i , j ; I’ve written an online tutorial: int 3 min ; int http://lyle.smu.edu/~tylerm/courses/cse3353/python.html 4 ( i =0; i < n ; i++) { for 5 Think Python by Allen Downey: min=i ; 6 http://www.greenteapress.com/thinkpython/ for ( j=i +1; j < n ; j++) 7 Videos from Khan academy: https://www.khanacademy.org/ i f ( s [ j ] < s [ min ] ) min=j ; 8 swap(&s [ i ] ,& s [ min ] ) ; science/computer-science-subject/computer-science 9 } 10 3 / 33 4 / 33
Selection Sort, now in Python Insertion Sort (C) s e l e c t i o n s o r t ( s ) : i n s e r t i o n s o r t ( int s [ ] , int n) 1 def Whitespace 1 { ””” 2 matters! 2 int i , j ; Input : l i s t s to be s o r t e d 3 3 Dynamic, implicit int min ; Output : s o r te d l i s t 4 4 typing ( i =1; i < n ; i++) { ””” for 5 5 j = i ; for i in range ( l e n ( s ) ) : Iterators make 6 6 (( j > 0) && #don ’ t name min s i n c e r e s e r v e d word while 7 7 looping easy ( s [ j ] < s [ j − 1])) { minidx=i 8 8 Has built-in lists, swap(&s [ j ] ,& s [ j − 1]); for j in range ( i +1, l e n ( s ) ) : 9 9 dictionaries j = j − 1; i f s [ j ] < s [ minidx ] : 10 10 minidx=j } Multiple variable 11 11 s [ i ] , s [ minidx ]= s [ minidx ] , s [ i ] } 12 12 assignment per s } return 13 13 line 5 / 33 6 / 33 Insertion Sort (Python) Python Built-in Data Types 1 def i n s e r t i o n s o r t ( s ) : ””” 2 Input : l i s t s to be s o r t e d Python uses implicit, dynamic typing 3 Output : s o r te d l i s t 4 You can use casting functions fl oat(), int() and str() to explicitly ””” 5 convert. for i in range ( l e n ( s ) ) : 6 j=i #don ’ t c a l l v a r i a b l e min s i n c e i t ’ s r e s e r v e d word In addition to numbers and strings, Python o ff ers built-in support for 7 j > 0 and s [ j ] < s [ j − 1]: while lists, tuples, sets and dictionaries. 8 s [ j ] , s [ j − 1]=s [ j − 1] , s [ j ] 9 j=j − 1 10 s return 11 7 / 33 8 / 33
Working with lists List indexing Python o ff ers several ways to extract elements and sublists from a list. Syntactically you use square braces at the end of the list variable name, with the indices you want to access. 1 > > s p o r t s =[ ’ f o o t b a l l ’ , ’ t e n n i s ’ , ’ i c e hockey ’ , ’ l a c r o s s e ’ , > Indices start at 0. You can get a range of values by including two ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 2 > > i n o r o u t =[ ’ out ’ , ’ both ’ , ’ i n ’ , ’ out ’ , ’ out ’ , ’ i n ’ , ’ out ’ , ’ i n ’ ] > numbers separated by a colon. If you use one number and a colon, it 3 > > l e n ( s p o r t s ) #b u i l t − i n f u n c t i o n computes the l e n g t h of l i s t s > will give you the rest of the list. 4 8 5 > > s p o r t s l o c=z i p ( sports , i n o r o u t ) #b u i l t − i n f u n c t i o n combines l i s t element − wise > You can access the end of the list by using negative numbers 6 > > s p o r t s l o c > 7 [ ( ’ f o o t b a l l ’ , ’ out ’ ) , ( ’ t e n n i s ’ , ’ both ’ ) , ( ’ i c e hockey ’ , ’ i n ’ ) , ( ’ l a c r o s s e ’ , ’ out ’ ) , ( ’ f i e l d hockey ’ , ’ out ’ ) , ( ’ b a s k e t b a l l ’ , ’ i n ’ ) , 1 > > s p o r t s [ 2 ] > 2 ’ i c e hockey ’ ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 3 > > s p o r t s [ 1 : 3 ] > 8 > > r a n d o m l i s t =[ ’ f o o t b a l l ’ , 3 , 6 . 7 , ’ham ’ ] #l i s t elements don ’ t have to be the same type > 4 [ ’ t e n n i s ’ , ’ i c e hockey ’ ] 5 > > s p o r t s [ 2 : ] > 6 [ ’ i c e hockey ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ , ’ swimming ’ ] 7 > > s p o r t s [ : 2 ] > 8 [ ’ f o o t b a l l ’ , ’ t e n n i s ’ ] 9 > > s p o r t s [ − 2] > 10 ’ b a s e b a l l ’ 11 > > s p o r t s [ − 2:] > 12 [ ’ b a s e b a l l ’ , ’ swimming ’ ] 9 / 33 10 / 33 Modifying a list Tuples and sets > bar = [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . append ( ’ o ’ ) > > > bar Tuples are immutable lists > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ o ’ ] Sets are unordered lists > bar . pop () > > ’ o ’ > bar > > > t = (4 ,6 ,2 ,3) > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > t [0]=5 > > > bar . extend ( [ ’ y ’ , ’ x ’ ] ) > > Traceback ( most r e c e n t c a l l l a s t ) : > bar > > F i l e ” < s t di n > ” , l i n e 1 , i n < module > [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] TypeError : ’ t u p l e ’ o b j e c t does not support item assignment > bar . i n s e r t ( ’w ’ ,3) > > > s = { 3 ,9 ,6 ,2 } > > Traceback ( most r e c e n t c a l l l a s t ) : > s [ 2 ] > > F i l e ” < st di n > ” , l i n e 1 , i n < module > Traceback ( most r e c e n t c a l l l a s t ) : TypeError : an i n t e g e r i s r e q u i r e d F i l e ” < st di n > ” , l i n e 1 , i n < module > > bar . i n s e r t (3 , ’w ’ ) > > TypeError : ’ s e t ’ o b j e c t does not support i n d e x i n g > bar > > > 6 i n s > > [ ’ b ’ , ’ a ’ , ’ j ’ , ’w ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] True > bar . s o r t () > > > 7 s i n > > > bar > > F a l s e [ ’ a ’ , ’ b ’ , ’ h ’ , ’ j ’ , ’ l ’ , ’w ’ , ’ x ’ , ’ y ’ ] > bar [ : : − 1 ] > > [ ’ y ’ , ’ x ’ , ’w ’ , ’ l ’ , ’ j ’ , ’ h ’ , ’ b ’ , ’ a ’ ] 11 / 33 12 / 33
Recommend
More recommend