Lab: Loop Algorithms#

This lab will give you practice building algorithms with loops.

Part 1: Algorithms With for Loops#

Now let’s do some harder things with for loops. If you’re having trouble with these problems remember to use the debugger.

[ ]:

my_list = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']

# Put your code below this comment.


1. Compute the Sum#

Write a for loop that prints the sum of all the elements in my_list

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


2. Find the Largest Value#

Write a for loop that prints the largest element in my_list

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


3. Find the Smallest Value#

Write a for loop that prints the smallest element in my_list.

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


4. Bold Face#

Copy and paste this example list into the next cell:

items = ['Eat', 'Sleep', 'Program', 'Repeat']


Write a for loop that makes a list named bolded with the elements from items surrounded by bold tags. For example your code should turn "Eat" into "<b>Eat</b>".

[ ]:




5. Build HTML#

Use the items in bolded from the last question to create an unorderd list in HTML. Display the list.

The output should look approximately like:

• Eat

• Sleep

• Program

• Repeat

[ ]:




5. A List as a Function Argument#

Write a function called add_me that takes one argument the_list. The function adds your name to the end of the list making your name the last element in the list.

• Function: add_me - Add your name to a list.

• Arguments: the_list

• Returns: None

[ ]:




Copy this list definition in to the cell below and call your add_me function with a_list as its argument.

a_list = [ 'one', 'two', 'apple', 'pear' ]


What happens to a_list after you call add_me?

[ ]:




6. Remove the Smallest Value#

Write a function called remove_min that takes one argument the_list and removes the smallest value in it. The function returns the smallest value.

• Function: remove_min - Removes and returns the smallest value.

• Arguments: the_list - A list of values (they should all be the same type)

• Returns: The smallest value in the list.

[ ]:




Part 2: Dictionary Functions#

Here are some more challenging problems using dictionaries.

3.1. Reverse the Dictionary#

Write a function caled reverse_get that takes two arguments, data and val. The function searches data for the value val and returns the corresponding key.

• Name: reverse_get

• Arguments:

• data - A dictionary

• val - A value in the dictionary

• Returns: The key that matches the value or None if the value is not found in the dictionary.

[ ]:




3.2. Frequency Counter#

Write a function that takes a single string argument and counts how many times each letter appears in the string. The function returns a dictionary with letters for keys and the count of the letters as values. For example if the function is given the sentence:

count_letters('Hello World')


It should return the dictionary:

{
'h' : 1,
'e' : 1,
'l' : 3,
' ' : 1,
'o' : 2,
'w' : 1,
'd' : 1,
}

• Name: count_letters

• Arguments:

• text (string) - The text to count.

• Returns: A dictionary with letters for keys and counts for values.

The nex cell has some example text to try:

[ ]:

example_text = """
It was a dark and stormy night; the rain fell in torrents — except
at occasional intervals, when it was checked by a violent gust of
wind which swept up the streets (for it is in London that our scene
lies), rattling along the housetops, and fiercely agitating the
scanty flame of the lamps that struggled against the darkness.
"""


[ ]: