Compsci 101, Fall 2012, Lab 3

By entering your name/net-id below you indicate you are present for Lab 3 to answer these questions and that you were part of the process that resulted in answers being turned in.
Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

Name: ______________    Net id: _____________ || Name: ______________    Net id: _____________

List Comprehensions

  1. print [x**2 for x in range(1,10,2)]
    
    
    
    
  2. print [s[1] for s in ["big", "brown", "bare", "stem", "pea"]]
    
    
    
    
  3. print ['po'*i for i in range(0,5)]
    
    
    
    
  4. print [i*i for i in range(1,10) if i % 2 == 0]
    
    
    
  5. print sum([1 for p in [1,2,3,4,5,6,7,8]])
    
    
    
    
  6. print sum([1 for x in "ostentatiously" if "aeiou".find(x) > -1])
    
    
    

  7. List comprehension for sum of odd numbers in nums:
    
    
    
    
    
  8. List comprehension for average word length of strings in strs:
    
    
    
    
    
    

APT ScoreIt

  1. Value returned by call maxPoints([2,2,4,5,4]) and why?
    
    
    
    
    
  2. Number of 2's in the list toss?
    
    
    
    
    
  3. Why elif won't work with an example:
    
    
    
    
    
    
    
    
    
    
  4. Why does the loop work (in words)
    
    
    
    
    
    
    
    
    
    
  5. Why does list comprehension work (in words)
    
    
    
    
    
    
    
    
    

Dice and Walking

  1. What does the graph look like? Justify.
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  2. Why write code to generate one roll at a time rather than use a list?
    
    
    
    
    
    
  3. Changes to roll three dice instead of two:
    
    
    
    
    
    
    
    

Walking Questions

Questions about OneDWalker.py
  1. Results of five runs:
    
    
    
    
    
  2. What happens when steps is 6?
    
    
    
    
    
    
  3. Why is the while loop in walk correct?
    
    
    
    
    
    
  4. How to find # steps needed to visit all locations -500 to 500.