This week we'll be reviewing for the final. The final exam in this course is cummulative; a list of topics that might appear on it are:

- Python syntax
- Strings, Lists, Dictionaries, Tuples, Sets
- Lambda expressions
- Classes, Inheritance
- Iterators, Generators
- Regular Expressions
- Pyplot, NumPy, Image Editing
- Tkinter
- Pandas
- NLTK (*)
- Scikit-Learn (*)

Disclaimer: I've assembled this list myself without seeing the exam. With the exceptions below, everything in this course is fair game for the exam. The above should be everything in the course, but if I happened to have missed something please let me know.

Scikit-Learn you'll learn in lecture tomorrow. The last two topics (NLTK and Scikit-Learn) we don't expect you to know very well since we haven't been (and won't be) covering it much detail. If either of these topics appear at all, expect them to only be a small part of the exam.

Also, we will not be testing you on specific algorithms we've taught you. So, for example, you will not need to know about blurring or edge-detection.

Today, let's cover a broad range of exercises meant to be spread out through the material in this class, but focusing mostly on the material since the midterm. At the end of class I'll give a poll to ask which topics to focus on for Thursday's discussion.

** Exercise 1:** Create a Pandas dataframe with 100 rows and 5 columns, filled with random numbers. Give the columns any name you like. Make the index labels the previous 100 days.

In [1]:

```
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0, 10, [100, 5]))
df.columns = ['Temperature', 'Inches of Rain', 'Hours worked', 'Hours slept', 'Overall Rating']
df.index = pd.date_range('20200831', periods = 100)
df
```

Out[1]:

** Exercise 2:** Do the above, but use regular expressions to get the values from this page on my website.

In [2]:

```
import urllib
import re
import pandas as pd
response = urllib.request.urlopen('http://www.math.ucla.edu/~egeo/classes/f20_pic16a/values.html')
raw = response.read().decode('utf8')
df = pd.DataFrame(re.findall(r'\[(\d) (\d) (\d) (\d) (\d)\]', raw))
df.columns = ['Temperature', 'Inches of Rain', 'Hours worked', 'Hours slept', 'Overall Rating']
df.index = pd.date_range('20200831', periods = 100)
df
```

Out[2]:

** Exercise 3:** Load the cat image we've worked on discussion from before. (This is

`kitty-cat.jpg`

.) Set half the pixels to black in a checkerboard pattern, and keep the rest.In [3]:

```
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread('kitty-cat.jpg')
x,y = np.ogrid[0:img.shape[0], 0:img.shape[1]]
mask = ((x + y) % 2) == 1
new_img = img.copy()
new_img[mask] = 0
plt.imshow(new_img)
plt.show()
```

** Exercise 4:** Write a Tkinter interface that has a square which moves to a random position every time you click on it.

In [4]:

```
import tkinter as Tk
import random
root = Tk.Tk()
c = Tk.Canvas(root, width=500, height=500)
x = random.randint(0,400)
y = random.randint(0,400)
square = c.create_rectangle(x,y,x+100,y+100,fill='red')
def mouse_click(event):
global x
global y
if event.x >= x and event.x <= x + 100 \
and event.y >= y and event.y <= y + 100:
new_x = random.randint(0,400)
new_y = random.randint(0,400)
c.move(square,new_x-x,new_y-y)
x = new_x
y = new_y
# can also use
c.coords(square, new_x, new_y, new_x + 100, new_y + 100)
c.bind('<Button-1>', mouse_click)
c.pack()
root.mainloop()
```

** Exercise 5:** Create a generator that goes through all the prime numbers less than a given number

`n`

. A number of prime if it has exactly two factors: itself and one. (One is not a prime number.)In [5]:

```
def primes(n):
for i in range(2,n):
isPrime = True
for j in range(2,i):
if i % j == 0:
isPrime = False
if isPrime:
yield i
for p in primes(10):
print(p)
# there is an 'else' syntax for a for loop that can be used here, but we didn't learn it
# here it is below
def primes(n):
for i in range(2,n):
for j in range(2,i):
if i % j == 0:
break
else:
# executes if the loop had no break statements
yield i
```