Exercise 1

  1. Create a function which takes a list of integers and prints whether the entry is positive, negative or zero.

  2. Modify this function so that instead of printing "positive", "negative" or "zero", it returns the list of corresponding strings. (Can you make this more general? Instead of taking a number to the string "postive", "negative" or "zero", what if I want the squares of the numbers? Or each number incremented by one? How would you allow for this?)

  3. Write a function which takes a list of numbers and returns the sum of the cubes of all the elements.

  4. Write a function which finds the largest item in a list of numbers.

  5. Write a function which returns a list but in reverse order.

Exercise 2

  1. Suppose that I'm playing a card game like poker. What are some possible ways I could represent my current hand in the game?

  2. Create a short list of friends' birthdays like we had above. Print a sorted version of this list. Notice the way Python sorts these? Now, write a function which creates a new birthday list but with the order of the name and the birthday swapped. Print out a sorted version of this list. Notice the difference?

  3. Create a list of the numbers 1 to 10. Create a new list which consists of the consecutive pairs of numbers in the list. For example, [(1, 2), (2, 3), (3, 4), ..., (9, 10)]

  4. Write a function which takes two tuples of (x1, y1) and (x2, y2) pairs and returns the "vector sum" (x1+x2, y1+y2). This suggests that we may try to implement an entire collection of support functions for vector algebra. Indeed, this kind of data abstraction is another crucial approach to building programs!

Exercise 3

  1. Write a dictionary which translates an abbreviation for each day of the week to a corresponding day number. For example:
    Mon -> 1, Tues -> 2, Wed -> 3, Thur -> 4, ...
    See if you can use this to build a "reverse" dictionary taking days numbers to day names.
  2. Suppose you have the list:
    ['dog', 'pencil', 'fence', 'dog', 'apple', 'dog', 'dog', 'dog', 'pear', 'pencil', 'pear', 'pear']
    Using a dictionary, compute the numbers of times each word occurs.
  3. Using this, print out the two most frequently occuring items. (Hint: Try converting the dictionary to a list of key-value pairs (or value-key pairs) and sort it.)

Exercise 4

  1. Write a function which computes the symmetric difference of two sets.
  2. Suppose you have a list of numbers like:
    [1, 4, 5, 3, 6, 7, 5, 3, 1, 9, 3, 3, 4, 2]
    How could you use a set data structure to remove all the duplicates from the list?
  3. Suppose that I'm running a server and I list of "bad" users who I want to make sure can't connect. Write a function which takes a list of users who want to connect and a set of bad users. For example, if I give it:
    users = ['tom', 'pam', 'sasha', 'jj', 'que', 'jeff']
    bad = set(['jeff', 'sasha'])
    
    It should return an "allowed" list:
    ['tom', 'pam', 'jj', 'que']
    
  4. Suppose you roll two fair die. Represent the pairs of die rolls for which at least one entry is either a 2 or a 5 using a set and compute the probability of getting one of these.