Compsci 6/101, Spring 2012, Lab 7

You can get help with your APTs and Hangman, or use the hints below and answer some questions toward understanding the APTs and assignment.

Highcard

Some ideas to help with the Highcard APT:

    There is a difference between vals = [7,6,4,3] vals.sort() vals.append(1) tt = vsorted(vals) Note that sorted returns a list that's sorted, but does not modify it's argument. However, vals.sort() does modify its argument, but returns None, i.e., doesn't return a modified version of the list. Similarly, vals.reverse() reverses the list to which its applied, e.g., vals but returns None. However, reversed(vals) returns a reversed version of its argument, but doesn't alter its argument.

    For the Highcard APT, consider this code:

    for val in reversed(sorted(mine)): # what goes here
  1. Explain why the loop goes over a reversed, sorted, hand of cards. For each card, why is the list comprehension below useful? for val in reversed(sorted(mine)): beatables = [x for x in friends if x < val]
  2. Note that mine.remove(dead) remvoves the value dead from mine, but doesn't return anything. Combine these ideas into code that solves the APT. What are your ideas? Code?

    Cryptography

    In the Cryptography APT you must decide which number to increase. Consider two numbers a and b, their product, and incrementing each by 1:
      a*b
      (a+1)*b = ab + b
      a*(b+1) = ab + a
    
  3. This shows that the product can increase by either a or by b depending on which is incremented by 1. If b is larger than a, which value should be increased by 1 to create the largest product?

  4. Using that idea, how can you find the value to increment? How can you change it by adding 1? How can you determine the product? In Python 2.7 using a long may be required, use what's below where value is an int.

    return long(value)

    Hangman with Phrases

    In the code we used in class, we used this initialization: whole_list = WordLoader.get_words(sec_length) secret = random.choice(whole_list) knowledge = ["_"]*len(secret) Consider a list of phrases like "New York Knicks", "Miami Heat", "Los Angeles Lakers". This could lead to this initialization WordLoader.load_lines("bball.txt") whole_list = WordLoader.get_lines() secret = random.choice(whole_list) # could be "New York Knicks" knowledge = [["_"]*len(p) for p in secret.split()]
  5. Explain what knowledge represents in words. How will you display it? Write the code that's the equivalent of the previous/one word version for a list of strings: "_" or "a" or "z" or .. print ' '.join(knowledge)
  6. Now knowledge is a list of lists ,each one could be displayed like the one above, how do you display all of them like this (for New York Knicks), e.g., with extra spaces between the chunks of underscores? Write code to do this.
        _ _ _   _ _ _ _   _ _ _ _ _ _
    
  7. If secret is a string with spaces in it and knowledge is a list of lists (of underscores/letters) then as you loop over secret to find a user's guessed letter you'll need to know which list of lists you're in. Explain how looping over secret.split() is a good idea ifyou keep track of the index of which list returned by .split you're in, e.g., how the index of that .split() list can be used to index knowledge to get the list in which an underscore could be replaced.