"""
Learning Goals:
 - Perform basic analyses on data to answer simple questions
 - Adapt matplotlib example code to create visualizations that show the state of a dataset
"""

numbers = [12, 68, 74, 71, 93, 80, 50, 48, 92, 88, 63, 90, 92, 100, 86, 66, 33, 58, 27, 20] # 0-100

print("Mean:", sum(numbers) / len(numbers))
sortedList = sorted(numbers)
print("Median:", sortedList[len(numbers) // 2])

numD = { }
for num in numbers:
    if num not in numD:
        numD[num] = 0
    numD[num] += 1

def mostCommonValue(d):
    bestVal = None
    bestCount = 0
    for k in d:
        if d[k] > bestCount:
            bestVal = k
            bestCount = d[k]
    return bestVal

print("Mode:", mostCommonValue(numD))

def bucket(lst):
    d = { }
    for tensValue in range(10):
        d[tensValue] = []
    for num in lst:
        tensDigit = num // 10
        if tensDigit == 10:
            d[9].append(num)
        else:
            d[tensDigit].append(num)
    return d

print("Buckets:", bucket(numbers))

print("92 prob:", numbers.count(92) / len(numbers))
aList = []
for num in numbers:
    if num >= 90:
        aList.append(num)
print("92 cond prob:", aList.count(92) / len(aList))

####################

import csv

def readFile(filename):
    f = open(filename, "r")
    reader = csv.reader(f)
    data = [ ]
    for row in reader:
        data.append(row)
    f.close()
    return data

def getIceCreamCounts(data):
    d = { }
    for i in range(1, len(data)): # skip header
        for flavor in data[i]:
            if flavor not in d:
                d[flavor] = 0
            d[flavor] += 1
    return d

iceCream = readFile("icecream-f20.csv")
d = getIceCreamCounts(iceCream)
for flavor in d:
    if d[flavor] >= 10:
        print(flavor, ", d[flavor])


###

import matplotlib.pyplot as plt
import random

# Generate a normal distribution
# We'll talk more about random next time!
data = []
for i in range(100000):
    data.append(random.normalvariate(0, 1))

# Set up the plot
fig, ax = plt.subplots()

# Set num of bins with the 'bins' argument
ax.hist(data, bins=20)

plt.show()

###

flavors = [ "vanilla", "chocolate", "strawberry" ]
counts = [ d["vanilla"], d["chocolate"], d["strawberry"] ]

# Set up the plot
fig, ax = plt.subplots()

# Set up the bars
ind = range(len(counts))
# counts holds the height of each bar
rects1 = ax.bar(ind, counts)


# Add labels
ax.set_ylabel('Flavors')
ax.set_title('Counts of Three Flavors')
ax.set_xticks(ind)
ax.set_xticklabels(flavors)

plt.show()

