Problem 1

Using the following dictionary:

my_dict = {
    'a': 3, 
    'b': 2, 
    'c': 10, 
    'd': 7, 
    'e': 9, 
    'f' : 12, 
    'g' : 13
    }

Print out:

- the keys of all values that are even.
- the key with the maximum value.
- the sum of all the values.

In [1]:
my_dict = {
    'a': 3, 
    'b': 2, 
    'c': 10, 
    'd': 7, 
    'e': 9, 
    'f' : 12, 
    'g' : 13
    }

In [3]:
# print the keys with even values
print('keys with even values:')
for key, value in my_dict.items():
    # modulo 2 == 0 implies the number is even
    if value % 2 == 0:
        print(key)

# print the key corresponding to the largest value
print('\nkey with max value:')
max_val = 0    # assume the values are positive numbers!
for key, value in my_dict.items():
    if value > max_val:
        max_val = value
        max_key = key
print(max_key)

# print the sum of all of the values
print('\nsum of values:')
val_sum = 0
for value in my_dict.values():
    val_sum += value
print(val_sum)

# or, using Python built-ins
print('\nsum of values (again):')
print(sum(my_dict.values()))


keys with even values:
c
f
b

key with max value:
g

sum of values:
56

sum of values (again):
56

Problem 2

Write a function that finds the number of elements in a list (without using the built-in len function). Now, use %%timeit to compare the speed to len for a list of 100,000 elements.


In [5]:
def my_len(my_list):
    n = 0
    for element in my_list:
        n += 1
    
    return n

In [6]:
my_list = list(range(100000))

In [7]:
%%timeit
my_len(my_list)


100 loops, best of 3: 4.78 ms per loop

In [9]:
%%timeit
len(my_list)


10000000 loops, best of 3: 78.8 ns per loop

It's not a coincidence that the custom function is roughly 100,000x slower than the built-in. Python lists actually have an attribute that stores their length - so using the len built-in means just looking up that number (so basically one step). Iterating over the entire list takes 100,000 steps (one for each element in the list). Try changing the length of the list to 1,000 elements - the custom length function now should be approximately 1,000x slower than the built-in.

Problem 3

Write a function that calculates the median of a list of numbers (without using statistics). Use the randint function the the random module to create a list of integers to test your function.


In [13]:
def median(x):
    # sort the input list first
    x.sort()
    mid = int(len(x) / 2)
    if len(x) % 2 == 0:
        # for even length, median is average of middle 2 vals
        return (x[mid] + x[mid-1]) / 2
    else:
        # for odd length, median is just the middle value
        return x[mid]

In [17]:
my_list = [1, 2, 3, 4, 5]
median(my_list)


Out[17]:
3

In [19]:
import random
my_random_list = []
# make a list of 1000 random integers between 0 and 99,999
for _ in range(1000):
    my_random_list.append(random.randint(0, 100000))
median(my_random_list)


Out[19]:
52211.0

In [20]:
# double check with median function from `statistics` module
import statistics
statistics.median(my_random_list)


Out[20]:
52211.0

In [ ]: