f = open("schedule.txt", "r")
text = f.read()
f.close()

#

lines = text.split("\n")

data = []
for line in lines:
    data.append(line.split("\t"))

#

lecData = []
for line in data:
    date = None
    lecture = None
    for token in line:
        if len(token) >= 5:
            if token[:2].isdigit() and token[2] == "/" and token[3:5].isdigit():
                date = token
        if token[:8] == "Lecture:":
            lecture = token
    if date != None and lecture != None:
        lecData.append(date + " " + lecture)

print(lecData)

###

import csv
import matplotlib.pyplot as plt

f = open("all-icecream.csv", "r")
data = list(csv.reader(f))
f.close()

def getIceCreamCounts(data, colName):
    iceCreamDict = { }
    for i in range(1, len(data)): # skip header
        col = data[0].index(colName) # only cleaned flavors

        flavor = data[i][col]
        if flavor not in iceCreamDict:
            iceCreamDict[flavor] = 0
        iceCreamDict[flavor] += 1
    return iceCreamDict

firstPrefs = getIceCreamCounts(data, "#1 cleaned")
secondPrefs = getIceCreamCounts(data, "#2 cleaned")

#

flavors = []
first = []
second = []

for flavor in firstPrefs:
    firstCount = firstPrefs[flavor]
    if flavor in secondPrefs:
        secondCount = secondPrefs[flavor]
    else:
        secondCount = 0

    if firstCount + secondCount >= 10:
        flavors.append(flavor)
        first.append(firstCount)
        second.append(secondCount)

for flavor in secondPrefs:
    if flavor not in firstPrefs:
        secondCount = secondPrefs[flavor]
        if secondCount >= 10:
            flavors.append(flavor)
            first.append(0)
            second.append(secondCount)

print(flavors)
print(first)
print(second)

#


plt.scatter(first, second)

plt.xlabel("#1 Favorites")
plt.ylabel("#2 Favorites")
plt.title("#1 vs #2 Preferences")

for i in range(len(flavors)):
    plt.annotate(flavors[i], [first[i], second[i]])

plt.show()

#

x = [2, 5, 7, 9, 10]

y = [ 5, 2, 9, 12, 3]

err = [ 0.5, 0.1, 0.3, 0.7, 0]

plt.errorbar(x, y, xerr=err)
plt.show()

###

import random

def makeModel(data):
    data["bubbles"] = [ [ 200, 200, "green" ] ]
    data["color"] = None

def makeView(data, canvas):
    for bubble in data["bubbles"]:
        x = bubble[0]
        y = bubble[1]
        color = bubble[2]
        r = 20
        canvas.create_oval(x - r, y - r, x+r, y+r, fill=color)

def runRules(data, call):
    x = random.randint(0, 400)
    y = random.randint(0, 400)
    color = random.choice(["red", "orange", "yellow", "green", "blue", "purple"])
    bubble = [x, y, color]
    data["bubbles"].append(bubble)

def keyPressed(data, event):
    colorMap = { "r" : "red", "o" : "orange", "y" : "yellow", "g" : "green", "b" : "blue", "p" : "purple" }
    if event.char in colorMap:
        color = colorMap[event.char]
        data["color"] = color

def mousePressed(data, event):
    x = event.x
    y = event.y
    data["bubbles"].append([x, y, data["color"]])

# You do not need to be able to write the following functions;
# just modify the five functions above.

from tkinter import *

def timeLoop(data, canvas, call):
    runRules(data, call)

    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()

    canvas.after(data["timeRate"], timeLoop, data, canvas, call + 1)

def keyEventHandler(data, canvas, event):
    keyPressed(data, event)

    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()

def mouseEventHandler(data, canvas, event):
    mousePressed(data, event)

    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()

def runSimulation(w, h, timeRate):
    data = { }
    data["timeRate"] = int(timeRate * 1000) # call will be in ms
    makeModel(data)

    root = Tk()
    canvas = Canvas(root, width=w, height=h)
    canvas.configure(bd=0, highlightthickness=0)
    canvas.pack()
    makeView(data, canvas)

    canvas.after(data["timeRate"], timeLoop, data, canvas, 1)

    root.bind("<Key>", lambda event : keyEventHandler(data, canvas, event))
    root.bind("<Button-1>", lambda event : mouseEventHandler(data, canvas, event))

    root.mainloop()

runSimulation(400, 400, 0.1)