Compsci 101, Fall 2012, Lab 7

There are two part to this lab: the first is practicing with dictionaries and readin .csv files. The second is looking through some of the APTs for this week and seeing some hints in solving them. The dictionary/.csv stuff must be done in lab. If you've done the APTs here you don't need to do the apt part the lab, but if you haven't yet done the APTs this will help.

    Dictionary and .csv files

    For this part you should snarf lab7, you can see the code and data files here.

  1. First open the top1000.csv file to see the top 1000 rock and roll songs of all time. Select them all, copy them, and paste them into the TagCrowd --- describe in words what you see (handin pages).

  2. If you change the number of of words to show from 50 to 200 what changes do you see?

  3. Run the program DataReader.py and describe the parameters to the function csv.reader in parsing/reading the .csv file.

  4. the artist/group in each row read is row[2], the name of the song is row[1]. In the reading loop of dump remove/comment out the print statement. Use the artist as the key to a dictionary in which the value is a list of songs. Add each song read to the list of songs for the artist. Here's the code you'll need (if variables artist and song have been set appropriately). if artist not in d: d[artist] = [] d[artist].append(song) What's the purpose (in words) of the line d[artist] = []?

  5. After the loop use the code/list comprehensions below -- enter them after the loop and run the code. Then explain on the handin pages answers to the questions there of which five artists have the most songs in the top 1000 songs. info = d.items() tosort = [(len(t[1]),t[0]) for t in info] info = sorted(tosort) print info[-30:]

    SpeedDial

    Read the SpeedDial problem statement (a handout for this lab).

    Put answers on handin page.

  6. First solve one of the problem instances by hand/paper-pencil. What value should be returned by the call assignNumbers(numbers,howMany,slots) for the values below? Show how you determined the answer. numbers = [1111,123456,654321,1234567,7654321,123456789,111222,2222] howMany = [3,4,5,8,7,2,2,3] slots = 4
  7. Write code using the function sum, list comprehensions, and the enumerate loop (e.g., for i,val in enumerate(numbers) that calculates the total number of keypresses needed when there are no speed-dial slots. Ideally you'll write one line as below, but more than one is ok if that helps you think about the problem. It will likely help to convert integer values to string values using str, e.g., str(123) = "123" because you can use the len function with strings. total = sum([ ])
  8. Suppose a six digit number like "123456" is dialed 12 times. If this number is put into a speed-dial slot, how many key-presses are saved? Note that with no speed-dial slots 72 key-presses are needed.

  9. Using the list comprehension/code you wrote for the problem before the last one (sum, str, enumerate) as a model, write code that creates a list named saved of how many key-presses are saved for each phone number in numbers -- you code should result in saved[i] having the value of how many key-presses are saved when numbers[i] is dialed howMany[i] times. saved =
  10. Which of the values in the list saved created in the previous problem represent numbers that should be put in speed-dial slots? Why?
    
    
  11. Using a slice and list functions such as sorted, max, sum, min, etc. write an expression that stores the number of key-presses saved in a variable named saved_presses using the list saved created above. It may help to remember that a negative index in a slice accesses list-elements from the end so that, for example, [1,2,3,4,5][-2:] evaluates to the list [4,5] by "slicing" the last two elements. saved_presses =
  12. What's left to solve this APT -- write the last steps on the handin pages.
    
    
    

    MemberCheck

    You'll look at the APT MemberCheck

    The union of two sets A and B is the elements that are in A together/combined with the elements in B. The union is a set, so there are no duplicates. The vertical bar | is the set-union operator in Python:

      set([1,2,3]) | set([2,3,4,5]) == set([1,2,3,4,5])
    

    To solve this APT consider the list comprehension below:

    cheats1 = [x for x in club1 if x in club2 or x in club3]
  13. In words(on the handin pages) describe the contents of the list referenced by variable cheat1

  14. On the handin pages write two more list comprehensions that interchange the roles of the three clubs so that there are three list variables cheat1, cheat2, and cheat3 that represent similar lists for the three clubs.

  15. A set can be created from a list using x = set(lst), and the union operator | combines sets just as the addition operator + adds integers or concatenates strings, e.g., the expression below stores the union of three sets in a variable w. w = x | y | z

    The sorted function returns a sorted list from any Python sequence, e.g., a list a string or a set. Write one line using sorted, the three lists created in the previous problem, and set operators to return the correct value for the APT.

      return sorted(......)
    

    Customer Statistics

    You'll look at the APT Customer Statistics

  16. Write an expression on one line that creates an alphabetized list of the unique names in parameter names. You can do this by combining the functions set, list, and sorted, e.g., list(set(list(sorted(xx)))) or sorted(list(set(x))), for example. Store the list in a variable unique.

  17. For each of the elements in unique, how can you determine the count of how many times each occurs in name? Once you have this value, how will you concatenate the string form of this count (which is an int), e.g., finish the APT on paper using the function str to convert an int to a string.b