def f(x):
    print(x**2)

a = f(2)

print(a)

"""
Explanation: Day 11

Computational Physics (PHYS 202)
Cal Poly, Spring 2015
Brian E. Granger

In class

Go over midterm
Discuss solutions

Grade distribution


Coding tips and help

Difference between return and print.
Transforming data between different container types.
How to count things.

Comments on performance.


Fetch today's material:
nbgrader fetch phys202-2015 day11
nbgrader fetch phys202-2015 assignment08



Coding tips and help
Different between return and print
A function that has no return statement will always return None:
End of explanation
"""


def g(x):
    return x**2

b = g(2)

print(b)

"""
Explanation: If you want a function to do anything useful, you have to return something:
End of explanation
"""


import random
alpha = 'abcdefghijklmnopqrstuvwxyz'

l = [random.choice(alpha) for i in range(10)]
l

s = ''.join(l)
s

for c in s:
    print(c)

[c.upper() for c in s]

list(s)

tuple(s)

digits = str(1001023039)

list(digits)

[int(d) for d in digits]

"""
Explanation: Transforming container types
String can be turned into lists and tuples:
End of explanation
"""


def random_string(n):
    return ''.join([random.choice(alpha) for i in range(n)])

random_string(100)

def count0(seq):
    counts = {}
    for s in seq:
        counts[s] = seq.count(s)
    return counts

rs = random_string(10)
count0(rs), rs

%timeit count0(random_string(10000))

def count1(seq):
    counts = {}
    for s in seq:
        if s in counts:
            counts[s] += 1
        else:
            counts[s] = 1
    return counts

rs = random_string(10)
count1(rs), rs

%timeit count1(random_string(10000))

from collections import defaultdict

def count2(seq):
    counts = defaultdict(int)
    for s in seq:
        counts[s] += 1
    return counts

rs = random_string(10)
count2(rs), rs

%timeit count2(random_string(10000))

from collections import Counter

def count3(seq):
    return dict(Counter(seq))

rs = random_string(10)
count3(rs), rs

%timeit count3(random_string(10000))

"""
Explanation: How to count things
End of explanation
"""


