/c/cs470/www/lectures/0117.ipynb: Python Programming examples

recursion, list comprehensions, object oriented programming, stack data structure

In [2]:
from recursion import *
In [3]:
x = range(20)
In [4]:
x
Out[4]:
range(0, 20)
In [5]:
x = list(x)
In [6]:
x
Out[6]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
In [7]:
rtotal(x)
Out[7]:
190
In [8]:
rlength(x)
Out[8]:
20
In [9]:
rmax(x)
Out[9]:
19

tail recursive versions of the above

In [10]:
trtotal(x)
Out[10]:
190
In [11]:
trlength(x)
Out[11]:
20
In [12]:
trmax(x)
Out[12]:
19
In [14]:
toord('abcdefg')
a  =>  97
b  =>  98
c  =>  99
d  =>  100
e  =>  101
f  =>  102
g  =>  103
In [15]:
rtoord('abceefg')
a  =>  97
b  =>  98
c  =>  99
e  =>  101
e  =>  101
f  =>  102
g  =>  103
In [17]:
rtochar(60,120)
60  =>  <
61  =>  =
62  =>  >
63  =>  ?
64  =>  @
65  =>  A
66  =>  B
67  =>  C
68  =>  D
69  =>  E
70  =>  F
71  =>  G
72  =>  H
73  =>  I
74  =>  J
75  =>  K
76  =>  L
77  =>  M
78  =>  N
79  =>  O
80  =>  P
81  =>  Q
82  =>  R
83  =>  S
84  =>  T
85  =>  U
86  =>  V
87  =>  W
88  =>  X
89  =>  Y
90  =>  Z
91  =>  [
92  =>  \
93  =>  ]
94  =>  ^
95  =>  _
96  =>  `
97  =>  a
98  =>  b
99  =>  c
100  =>  d
101  =>  e
102  =>  f
103  =>  g
104  =>  h
105  =>  i
106  =>  j
107  =>  k
108  =>  l
109  =>  m
110  =>  n
111  =>  o
112  =>  p
113  =>  q
114  =>  r
115  =>  s
116  =>  t
117  =>  u
118  =>  v
119  =>  w
In [18]:
tochar(60,80)
60  =>  <
61  =>  =
62  =>  >
63  =>  ?
64  =>  @
65  =>  A
66  =>  B
67  =>  C
68  =>  D
69  =>  E
70  =>  F
71  =>  G
72  =>  H
73  =>  I
74  =>  J
75  =>  K
76  =>  L
77  =>  M
78  =>  N
79  =>  O
In [19]:
xx
Out[19]:
[1, [2, [3], [[[4]]]]]
In [20]:
maptree(lambda x: x+1, xx)
Out[20]:
[2, [3, [4], [[[5]]]]]

list comprehension version of maptree

In [21]:
lcmaptree(lambda x: x+1, xx)
Out[21]:
[2, [3, [4], [[[5]]]]]
In [22]:
mapmaptree(lambda x: x+1,xx)
Out[22]:
[2, [3, [4], [[[5]]]]]
In [23]:
rmaptree(lambda x: x+1,xx)
Out[23]:
[2, [3, [4], [[[5]]]]]

end of recursion examples. start of list comprension examples

In [1]:
from listcomp import *
In [25]:
a1
Out[25]:
['1', '2', '3', '4', '5']
In [26]:
a2
Out[26]:
['1', '2', '3', '4', '5']

Create a list with [...] syntax

In [27]:
a2 = [x for x in str(12345)]
In [28]:
a2
Out[28]:
['1', '2', '3', '4', '5']
In [29]:
[int(x) for x in str(12345)]
Out[29]:
[1, 2, 3, 4, 5]
In [30]:
[int(x) * int(x) for x in str(12345)]
Out[30]:
[1, 4, 9, 16, 25]
In [31]:
[(n, n*n) for n in range(5)]
Out[31]:
[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]
In [32]:
[(x1, x2) for (x1,x2) in enumerate('abcde')]
Out[32]:
[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]
In [33]:
[x for x in range(10) if x % 2 == 0]
Out[33]:
[0, 2, 4, 6, 8]
In [34]:
[x for x in range(10) if x > 4]
Out[34]:
[5, 6, 7, 8, 9]
In [35]:
reduce(lambda x,y: x+y, range(101))
Out[35]:
5050
In [37]:
[(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]
Out[37]:
[(3, 4, 5),
 (5, 12, 13),
 (6, 8, 10),
 (7, 24, 25),
 (8, 15, 17),
 (9, 12, 15),
 (10, 24, 26),
 (12, 16, 20),
 (15, 20, 25),
 (20, 21, 29)]
In [38]:
[x if x % 2 else x + 1 for x in range(10)]
Out[38]:
[1, 1, 3, 3, 5, 5, 7, 7, 9, 9]

Create a set with {...} syntax

In [39]:
{x if x % 2 else x + 1 for x in range(10)}
Out[39]:
{1, 3, 5, 7, 9}
In [40]:
s3 = {1,2,3,1,2,3}
In [41]:
s3
Out[41]:
{1, 2, 3}

Create a generator object with (...) syntax.

In [5]:
gen = (x*x for x in range(200))
In [6]:
next(gen)
Out[6]:
0
In [7]:
for n in range(5):
    print(next(gen))
1
4
9
16
25

end of list comprehension. start of object oriented. Create course class. (pretty confusing, yes?)

In [42]:
from oop import *
<course title:Math 120, (0)>
<course title:CS100, (1)>
<course title:CS200, (2)>
<course title:CS201, (3)>
<course title:CS223, (4)>
<course title:CS323, (5)>
<course title:CS690, (6)>
<course title:Math 120, (0)>
In [43]:
m120
Out[43]:
"course('Math 120', 'Math')"
In [44]:
c100
Out[44]:
"course('CS100', 'Computer Science')"
In [45]:
course.all()
<course title:Math 120, (0)>
<course title:CS100, (1)>
<course title:CS200, (2)>
<course title:CS201, (3)>
<course title:CS223, (4)>
<course title:CS323, (5)>
<course title:CS690, (6)>

all is a class method, not an instance method.

In [46]:
course.all("Math")
<course title:Math 120, (0)>
In [47]:
test()
Course Title Math 120
	Department: Math
Course Title CS100
	Department: Computer Science
	Room: Law School Auditorium
Course Title CS200
	Department: Computer Science
	Room: LC 101
	Students: ['Joe', 'Mary', 'Joe', 'John', 'Jane']
	Prereqs: ["course('CS100', 'Computer Science')"]
Course Title CS201
	Department: Computer Science
	Room: LC 101
	Prereqs: ["course('CS100', 'Computer Science')"]
Course Title CS223
	Department: Computer Science
	Room: DL 220
	Prereqs: ["course('CS201', 'Computer Science')"]
Course Title CS323
	Department: Computer Science
	Room: DL 220
	Prereqs: ["course('CS223', 'Computer Science')"]
Course Title CS690
	Department: Computer Science
Prereq for <course title:CS323, (5)>: <course title:CS223, (4)>
Prereq for <course title:CS223, (4)>: <course title:CS201, (3)>
Prereq for <course title:CS201, (3)>: <course title:CS100, (1)>
None

end of oop. start of stack.

In [48]:
from stack import *

note that stack has an __iter__ method for iteration, as in for or list

In [49]:
s
Out[49]:
stack([1, 2, 3, 4])
In [50]:
test(s)
4
3
2
1
Out[50]:
[4, 3, 2, 1]
In [51]:
revstr('hello world')
Out[51]:
'dlrow olleh'

end of stack demo

In [ ]: