We already know Java and C++. Why learn Python? Using Python to - - PowerPoint PPT Presentation

we already know java and c why learn python using python
SMART_READER_LITE
LIVE PREVIEW

We already know Java and C++. Why learn Python? Using Python to - - PowerPoint PPT Presentation

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,


slide-1
SLIDE 1

Using Python to Implement Algorithms

Tyler Moore

CSE 3353, SMU, Dallas, TX

Lecture 2

Some slides created by or adapted from Dr. Kevin Wayne. For more information see http://www.cs.princeton.edu/~wayne/kleinberg-tardos

We already know Java and C++. Why learn Python?

Python has far less overhead than Java/C++ for the programmer. Python is closer to psuedo-code on the English-pseudocode-code spectrum than Java or C/C++, but it actually executes! Python is handy for data manipulation and transformation, and anything ”quick and dirty.” Python is very powerful, thanks to all those extension modules.

2 / 33

Python Resources

Download from python.org for all major platforms I’ve written an online tutorial: http://lyle.smu.edu/~tylerm/courses/cse3353/python.html Think Python by Allen Downey: http://www.greenteapress.com/thinkpython/ Videos from Khan academy: https://www.khanacademy.org/ science/computer-science-subject/computer-science

3 / 33

Selection Sort (C)

1

s e l e c t i o n s o r t ( int s [ ] , int n)

2

{

3

int i , j ;

4

int min ;

5

for ( i =0; i <n ; i++) {

6

min=i ;

7

for ( j=i +1; j<n ; j++)

8

i f ( s [ j ] < s [ min ] ) min=j ;

9

swap(&s [ i ] ,& s [ min ] ) ;

10

}

4 / 33

slide-2
SLIDE 2

Selection Sort, now in Python

1 def

s e l e c t i o n s o r t ( s ) :

2

”””

3

Input : l i s t s to be s o r t e d

4

Output : s o r te d l i s t

5

”””

6

for i in range ( l e n ( s ) ) :

7

#don ’ t name min s i n c e r e s e r v e d word

8

minidx=i

9

for j in range ( i +1, l e n ( s ) ) :

10

i f s [ j ]<s [ minidx ] :

11

minidx=j

12

s [ i ] , s [ minidx ]= s [ minidx ] , s [ i ]

13

return s

Whitespace matters! Dynamic, implicit typing Iterators make looping easy Has built-in lists, dictionaries Multiple variable assignment per line

5 / 33

Insertion Sort (C)

1

i n s e r t i o n s o r t ( int s [ ] , int n)

2

{

3

int i , j ;

4

int min ;

5

for ( i =1; i <n ; i++) {

6

j = i ;

7

while (( j >0) &&

8

( s [ j ] < s [ j −1])) {

9

swap(&s [ j ] ,& s [ j −1]);

10

j = j −1;

11

}

12

}

13

}

6 / 33

Insertion Sort (Python)

1 def

i n s e r t i o n s o r t ( s ) :

2

”””

3

Input : l i s t s to be s o r t e d

4

Output : s o r te d l i s t

5

”””

6

for i in range ( l e n ( s ) ) :

7

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

8

while j >0 and s [ j ]<s [ j −1]:

9

s [ j ] , s [ j −1]=s [ j −1] , s [ j ]

10

j=j −1

11

return s

7 / 33

Python Built-in Data Types

Python uses implicit, dynamic typing You can use casting functions float(), int() and str() to explicitly convert. In addition to numbers and strings, Python offers built-in support for lists, tuples, sets and dictionaries.

8 / 33

slide-3
SLIDE 3

Working with lists

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 ’ , ’ 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 ’ ] 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

  • f

l i s t s 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 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 ’ ) , ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 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

9 / 33

List indexing

Python offers 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. Indices start at 0. You can get a range of values by including two numbers separated by a colon. If you use one number and a colon, it will give you the rest of the list. You can access the end of the list by using negative numbers

1 > > > s p o r t s [ 2 ] 2 ’ i c e hockey ’ 3 > > > s p o r t s [ 1 : 3 ] 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 ’ ] 10 / 33

Modifying a list

> > > bar = [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . append ( ’ o ’ ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ o ’ ] > > > bar . pop () ’ o ’ > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ ] > > > bar . extend ( [ ’ y ’ , ’ x ’ ] ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] > > > bar . i n s e r t ( ’w ’ ,3) Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<st di n >” , l i n e 1 , i n <module> TypeError : an i n t e g e r i s r e q u i r e d > > > bar . i n s e r t (3 , ’w ’ ) > > > bar [ ’ b ’ , ’ a ’ , ’ j ’ , ’w ’ , ’ h ’ , ’ l ’ , ’ y ’ , ’ x ’ ] > > > bar . s o r t () > > > bar [ ’ a ’ , ’ b ’ , ’ h ’ , ’ j ’ , ’ l ’ , ’w ’ , ’ x ’ , ’ y ’ ] > > > bar [ : : − 1 ] [ ’ y ’ , ’ x ’ , ’w ’ , ’ l ’ , ’ j ’ , ’ h ’ , ’ b ’ , ’ a ’ ]

11 / 33

Tuples and sets

Tuples are immutable lists Sets are unordered lists

> > > t = (4 ,6 ,2 ,3) > > > t [0]=5 Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<s t di n >” , l i n e 1 , i n <module> TypeError : ’ t u p l e ’

  • b j e c t

does not support item assignment > > > s ={3 ,9 ,6 ,2} > > > s [ 2 ] Traceback ( most r e c e n t c a l l l a s t ) : F i l e ”<st di n >” , l i n e 1 , i n <module> TypeError : ’ s e t ’

  • b j e c t

does not support i n d e x i n g > > > 6 i n s True > > > 7 i n s F a l s e

12 / 33

slide-4
SLIDE 4

Dictionaries

Dictionaries map keys to values. Both the keys and values can be of any type, from strings to numbers, to other dictionaries.

1 > > > from sampledata import ∗ 2 > > > uni 3 { ’ Jones ’ : ’ Oxford ’ , ’ G i l l i a m ’ : ’ O c c i d e n t a l ’ , ’ C l e e s e ’ : ’ Cambridge ’ , ’Chapman ’ : ’ Cambridge ’ , ’ I d l e ’ : ’ Cambridge ’ , ’ P a l i n ’ : ’ Oxford ’ } 4 > > > uni [ ’ P a l i n ’ ] 5 ’ Oxford ’ 6 > > > uni [ ’ P a l i n ’ ] = ’ Oxford U n i v e r s i t y ’ 7 > > > uni [ ’ P a l i n ’ ] 8 ’ Oxford U n i v e r s i t y ’ 9 > > > uni . keys () 10 [ ’ Jones ’ , ’ G i l l i a m ’ , ’ C l e e s e ’ , ’Chapman ’ , ’ I d l e ’ , ’ P a l i n ’ ]

13 / 33

Dictionaries

You can also start from an empty dictionary and then add values:

s p o r t 2 t y p e ={} s p o r t 2 t y p e [ ’ f o o t b a l l ’ ]= ’ out ’ s p o r t 2 t y p e [ ’ i c e hockey ’ ]= ’ i n ’ > > > s p o r t 2 t y p e { ’ i c e hockey ’ : ’ i n ’ , ’ f o o t b a l l ’ : ’ out ’ }

14 / 33

Python Tips

To see the methods available for a variable s, type dir(s) at the interpreter Python documentation (and Google) will help explain what each method does: http://docs.python.org/2/index.html You can also check the docstring for what methods do

15 / 33

Python Tips

> > > foo = [ ’ cat ’ , ’ dog ’ , ’ hamster ’ , ’ b u f f a l o ’ , ’ cheetah ’ ] > > > d i r ( foo ) #f i n d l i s t a t t r i b u t e s and methods [ ’ a d d ’ , ’ c l a s s ’ , ’ c o n t a i n s ’ , ’ d e l a t t r ’ , ’ d e l i t e m ’ , ’ d e l s l i c e ’ , ’ d o c ’ , ’ e q ’ , ’ f o r m a t ’ , ’ g e ’ , ’ g e t a t t r i b u t e ’ , ’ g e t i t e m ’ , ’ g e t s l i c e ’ , ’ g t ’ , ’ h a s h ’ , ’ i a d d ’ , ’ i m u l ’ , ’ i n i t ’ , ’ i t e r ’ , ’ l e ’ , ’ l e n ’ , ’ l t ’ , ’ m u l ’ , ’ n e ’ , ’ new ’ , ’ r e d u c e ’ , ’ r e d u c e e x ’ , ’ r e p r ’ , ’ r e v e r s e d ’ , ’ r m u l ’ , ’ s e t a t t r ’ , ’ s e t i t e m ’ , ’ s e t s l i c e ’ , ’ s i z e o f ’ , ’ s t r ’ , ’ s u b c l a s s h o o k ’ , ’ append ’ , ’ count ’ , ’ extend ’ , ’ index ’ , ’ i n s e r t ’ , ’ pop ’ , ’ remove ’ , ’ r e v e r s e ’ , ’ s o r t ’ ] > > > p r i n t foo . pop . d o c #check the d o c s t r i n g L . pop ( [ index ] ) − > item − − remove and return item at index ( d e f a u l t l a s t ) . R a i s e s I n d e x E r r o r i f l i s t i s empty

  • r

index i s

  • ut
  • f

range . > > > foo . pop () ’ cheetah ’ > > > foo [ ’ cat ’ , ’ dog ’ , ’ hamster ’ , ’ b u f f a l o ’ ]

16 / 33

slide-5
SLIDE 5

Iteration in python

The python for loop is very powerful, due to its natural integration with iterators. You can iterate lists:

1 > > > from sampledata import ∗ 2 > > > f o r cheese i n c h e e s e s : 3 . . . p r i n t ’%s i s t a s t y ’ % ( cheese ) 4 . . . 5 s w i s s i s t a s t y 6 g r u y e r e i s t a s t y 7 cheddar i s t a s t y 8 s t i l t o n i s t a s t y 9 r o q u e f o r t i s t a s t y 10 b r i e i s t a s t y

You can iterate dictionaries:

1 > > > f o r name i n uni : 2 . . . p r i n t name , ”went to ” , uni [ name ] 3 . . . 4 Jones went to Oxford 5 G i l l i a m went to O c c i d e n t a l 6 C l e e s e went to Cambridge 7 Chapman went to Cambridge 8 I d l e went to Cambridge 9 P a l i n went to Oxford U n i v e r s i t y 17 / 33

List comprehensions

Recall set-builder notation from Discrete Math: S = {3x|x ∈ N, x > 5} We can approximate that in Python

> > > range (1 ,11) [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10] > > > {3∗ x f o r x i n range (1 ,11) i f x>5} s e t ( [ 2 4 , 18 , 27 , 21 , 3 0 ] ) > > > [3∗ x f o r x i n range (1 ,11) i f x >5] [18 , 21 , 24 , 27 , 30] Comprehensions arise in very common coding scenarios

18 / 33

List comprehensions

Here’s a common coding task: iterate over some list, perform some action

  • n each element of that list, and store the results in a new list. Here’s an

example:

> > > c h e e s e s = [ ’ s w i s s ’ , ’ g r u y e r e ’ , ’ cheddar ’ , ’ s t i l t o n ’ , ’ r o q u e f o r t ’ , ’ b r i e ’ ] > > > c h e e s e l e n =[] > > > f o r c i n c h e e s e s : . . . c h e e s e l e n . append ( l e n ( c )) . . . > > > c h e e s e l e n [ 5 , 7 , 7 , 7 , 9 , 4]

We can do this on a single line:

c h e e s e l e n =[ l e n ( c ) f o r c i n c h e e s e s ] > > > c h e e s e l e n [ 5 , 7 , 7 , 7 , 9 , 4]

19 / 33

List comprehensions

But wait, there’s more! Suppose you only want to add items to the list if they meet a certain condition, say if the item begins with the letter s. Well here’s the long way:

> > > s c h e e s e l e n =[] > > > f o r c i n c h e e s e s : . . . i f c[0]== ’ s ’ : . . . s c h e e s e l e n . append ( l e n ( c ) ) . . . > > > s c h e e s e l e n [ 5 , 7]

You can add a condition at the end of the list comprehension:

c h e e s e l e n =[ l e n ( c ) f o r c i n c h e e s e s i f c[0]==” s ” ] > > > s c h e e s e l e n [ 5 , 7]

20 / 33

slide-6
SLIDE 6

More list comprehension examples

1 > > > s p o r t s 2 [ ’ 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 ’ , ’ 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 ’ ] 3 > > > [ s f o r s i n s p o r t s i f l e n ( s ) >8] 4 [ ’ i c e hockey ’ , ’ f i e l d hockey ’ , ’ b a s k e t b a l l ’ ] 5 > > > [ s f o r s i n s p o r t s i f ’ b a l l ’ i n s ] 6 [ ’ f o o t b a l l ’ , ’ b a s k e t b a l l ’ , ’ b a s e b a l l ’ ] 7 8 > > > s p o r t s l o c 9 [ ( ’ 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 ’ ) , ( ’ b a s e b a l l ’ , ’ out ’ ) , ( ’ swimming ’ , ’ i n ’ ) ] 10 > > > [ s [ 0 ] f o r s i n s p o r t s l o c ] 11 [ ’ 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 ’ , ’ 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 ’ ] 12 > > > o u t d o o r s p o r t s =[ s [ 0 ] f o r s i n s p o r t s l o c i f s [1]== ’ out ’ ] 13 > > > o u t d o o r s p o r t s 14 [ ’ f o o t b a l l ’ , ’ l a c r o s s e ’ , ’ f i e l d hockey ’ , ’ b a s e b a l l ’ ]

21 / 33

List comprehension exercise

Write a function that squares each element of a list so long as the elements are positive using a list comprehension. Complete this code:

def s q u a r e L i s t ( l ) : return # f i l l i n l i s t comprehension

22 / 33

Stacks and Queues

Sometimes, the order in which we retrieve data is independent of its content, being only a function of when it arrived. A stack supports last-in, first-out operations: push and pop. A queue supports first-in, first-out operations: enqueue and dequeue. Lines in banks are based on queues, while food in my refrigerator is treated as a stack.

23 / 33

Python lists can be treated like stacks

Push: l.append() Pop: l.pop() What’s missing from list’s built-in methods to make queues possible?

List’s methods are ‘append’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ’sort’ enqueue(): dequeue():

24 / 33

slide-7
SLIDE 7

Queues in Python: deque

> > > from c o l l e c t i o n s import deque > > > q = deque ( [ 2 , 5 , 1 , 7 ] ) > > > q . append (6) > > > q deque ( [ 2 , 5 , 1 , 7 , 6 ] ) > > > q . p o p l e f t () 2 > > > Enqueue: q.append() Dequeue: q.popleft()

25 / 33

An intuitive method that guarantees to find a stable matching.

9

  • Python data structures to represent preferences

1 men = [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ] 2 women = [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] 3 4 p r e f = { ’ x a v i e r ’ :

[ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,

5

’ yancey ’ : [ ’ bertha ’ , ’amy ’ , ’ c l a r e ’ ] ,

6

’ zeus ’ : [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,

7

’amy ’ : [ ’ yancey ’ , ’ x a v i e r ’ , ’ zeus ’ ] ,

8

’ bertha ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ] ,

9

’ c l a r e ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ]

10

}

11 #turn

the p r e f e r e n c e s i n t o a usable stack by r e v e r s i n g

  • rder

12 for n in

p r e f :

13

p r e f [ n ] . r e v e r s e ()

26 / 33

Python data structures to represent preferences

1 rank = { ’amy ’ :{ ’ x a v i e r ’ :2 , ’ yancey ’ :1 , ’ zeus ’ :3} , 2

’ bertha ’ :{ ’ x a v i e r ’ :1 , ’ yancey ’ :2 , ’ zeus ’ :3} ,

3

’ c l a r e ’ :{ ’ x a v i e r ’ :1 , ’ yancey ’ :2 , ’ zeus ’ :3}

4

}

5 6 freemen = l i s t (men)

#i n i t i a l l y a l l men and women are f r e e

7 numpartners = l e n (men) 8 #c r e a t e

l i s t

  • f

matches

9 S = {} 27 / 33

slide-8
SLIDE 8

Gale-Shapley algorithm in Python

1 while

freemen :

2

m = freemen . pop ()

3

i f l e n ( p r e f [m])==0: continue

4

#get the h i g h e s t ranked woman that has not yet been proposed to

5

w = p r e f [m] . pop ()

6

i f w not in S : S [w] = m

7

else :

8

mprime = S [w]

9

i f rank [w ] [m] < rank [w ] [ mprime ] :

10

S [w] = m

11

freemen . append ( mprime )

12

else :

13

freemen . append (m)

14 15 print S 16 #{ ’amy ’ :

’ x a v i e r ’ , ’ c l a r e ’ : ’ zeus ’ , ’ bertha ’ : ’ yancey ’}

28 / 33

Alternative implementation: “pointers” instead of a stack

1 p r e f p t r = {} 2 for m in men : 3

p r e f p t r [m] = 0

4 5 while

freemen :

6

m = freemen . pop ()

7

i f p r e f p t r [m]>numpartners : continue

8

#get the h i g h e s t ranked woman that has not yet been proposed to

9

w = p r e f [m] [ p r e f p t r [m] ]

10

p r e f p t r [m]+=1

11

i f w not in S : S [w] = m

12

else :

13

mprime = S [w]

14

i f rank [w ] [m] < rank [w ] [ mprime ] :

15

S [w] = m

16

freemen . append ( mprime )

17

else :

18

freemen . append (m)

29 / 33

Improving the code

An advantage of Python is quick development: you can get instant feedback from code by running it in the interpreter (demo) Once the code is working, though, we must make it modular

30 / 33

Step 1 to modularity: Create a function

1 def

GaleShapley (men , women , p r e f ) :

2

# p r e p r o c e s s i n g

3

# # b u i l d the rank d i c t i o n a r y

4

rank={}

5

for w in women :

6

rank [w] = {}

7

i = 1

8

for m in p r e f [w ] :

9

rank [w ] [m]= i

10

i+=1

11

# # c r e a t e a ” p o i n t e r ” to the next woman to propose

12

p r e f p t r = {}

13

for m in men :

14

p r e f p t r [m] = 0

15 16

freemen = l i s t (men) #i n i t i a l l y a l l men and women are f r e e

17

numpartners = l e n (men)

18

S = {} #b u i l d d i c t i o n a r y to s t o r e engagements

31 / 33

slide-9
SLIDE 9

Step 1 to modularity: Create a function

1

#run the algorithm

2

while freemen :

3

m = freemen . pop ()

4

i f p r e f p t r [m]>numpartners : continue

5

#get the h i g h e s t ranked woman that has not yet been proposed to

6

w = p r e f [m] [ p r e f p t r [m] ]

7

p r e f p t r [m]+=1

8

i f w not in S : S [w] = m

9

else :

10

mprime = S [w]

11

i f rank [w ] [m] < rank [w ] [ mprime ] :

12

S [w] = m

13

freemen . append ( mprime )

14

else :

15

freemen . append (m)

16

return S

32 / 33

Invoking the function

1 i f

name ==” main ” :

2

#input data

3

themen = [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ]

4

thewomen = [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ]

5 6

t h e p r e f = { ’ x a v i e r ’ : [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,

7

’ yancey ’ : [ ’ bertha ’ , ’amy ’ , ’ c l a r e ’ ] ,

8

’ zeus ’ : [ ’amy ’ , ’ bertha ’ , ’ c l a r e ’ ] ,

9

’amy ’ : [ ’ yancey ’ , ’ x a v i e r ’ , ’ zeus ’ ] ,

10

’ bertha ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ] ,

11

’ c l a r e ’ : [ ’ x a v i e r ’ , ’ yancey ’ , ’ zeus ’ ]

12

}

13 14

eng = GaleShapley ( themen , thewomen , t h e p r e f )

15

print eng

33 / 33