Exercises

Counting Letters

>hello
h 1
e 1
l 2
o 1

Counting Letters Version 1

word = input("Enter a word\n>")
for c in "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz":
	count = word.count(c)
	if count > 0:
		print(c, count)

Counting Letters Version 2

word = input("Enter a word\n>")
d = {}
for c in word:
	try:
		d[c] += 1
	except KeyError:
		d[c] = 1
for k,v in d.items():
	print(k, v)

Counting Letters Version 3

word = input("Enter a word\n>")
d = {}
for c in word:
	d[c] = d.get(c, 0) + 1
for k,v in d.items():
	print(k, v)

Counting Dice Rolls

An Implementation Using Dictionaries

import random
freq_dice_rolls = {}
for i in range(1000):
	roll = random.randint(1,3) + random.randint(1,3)
	freq_dice_rolls[roll] = freq_dice_rolls.get(roll, 0) + 1
print(freq_dice_rolls)

Dice Rolls Solution

Let's compare that to our previous solution without dictionaries!

import random
twos, threes, fours, fives, sixes = 0, 0, 0, 0, 0
for i in range(1000):
	roll = random.randint(1,3) + random.randint(1,3)
	if roll == 2:
		twos += 1
	if roll == 3:
		threes += 1
	if roll == 4:
		fours += 1
	if roll == 5:
		fives += 1
	if roll == 6:
		sixes += 1

print("twos: %s, threes: %s, fours: %s, fives: %s, sixes %s" % (twos, threes, fours, fives, sixes)) 

A List of Contacts

Create a text-based contact list that allows you to store first name, last name and room #

It should support the following functionality:

The primary interface will be a prompt that will continually ask the user for a letter (a, p, f or q)…

Add a Contact Example Interaction

(a)dd contact, (p)rint all contacts, (f)ind contact, (q)uit 
>a
first name plz 
>tim
last name plz 
>test
room # plz 
>200

Find a Contact Example Interaction

Finding a contact:

(a)dd contact, (p)rint all contacts, (f)ind contact, (q)uit 
>f
what's the firs name of the person you'd like to find? 
>tim
last name - test
first name - tim
room - 200
(a)dd contact, (p)rint all contacts, (f)ind contact, (q)uit 
>p
last name - test
first name - tabitha
room - 100

last name - test
first name - tim
room - 200

Before Diving In…

One Possible Solution…

Storage

How about using a list of dictionaries to store contacts?

Here's an example with one contact:

[{'first name': 'tabitha', 'last name': 'test', 'room': 100}]

Some Functions

We can break down the code into smaller chunks of functionality:

def contact_as_string(contact):
	s = ''
	for attribute, value in contact.items():
		s += '%s - %s\n' % (attribute, value)
	return s

def print_all_contacts(contact_list):
	for c in contact_list:
		print(contact_as_string(c))

And More Functions

def find_contact(contact_list, attribute, value):
	for c in contact_list:	
		if c[attribute] == value:
			return c
	return None

def find_contact_by_first(contact_list, first):
	return find_contact(contact_list, 'first name', first)

The Main Loop…

We can use the previous functions in a while loop that drives the interaction with the user:

contacts = [{'first name': 'tabitha', 'last name': 'test', 'room': 100}]
while True:
	command = input('(a)dd contact, (p)rint all contacts, (f)ind contact, (q)uit \n>')
	if command == 'a':
		first = input('first name plz \n>')
		last = input('last name plz \n>')
		room = input('room # plz \n>')
		contacts.append({'first name': first, 'last name': last, 'room': room})
	elif command == 'p':
		print_all_contacts(contacts)
	# continued in next slide...

The Main Loop Continued

	elif command == 'f':
		name_to_find = input('what\'s the firs name of the person you\'d like to find? \n>')
		c = find_contact_by_first(contacts, name_to_find)
		if c != None:
			print(contact_as_string(c))
		else:
			print('Contact not found')
	elif command == 'q':
		break

</section>