Compsci 06/101, Spring 2011, Lab 4

By entering your name/net-id below you indicate you are present for Lab 4 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: _____________

Vocabulary

  1. What do you think the name of the string method is that returns the lower case version of a string?
    
    
    
    
    
  2. The function max returns the maximal value of a string as well as a list. What is max("science") and why?
    
    
    
    
    
    
    
  3. What is the value of reversed(sorted([5,4,1,2,8])) and why?
    
    
    
    
  4. What is the value of sorted(lst).index(min(lst)) for any list lst? Why? What's the value if min is replaced by max?
    
    
    
    
    
  5. Why is the value of str.endswith(str[-3:]) True?
    
    
    
    
  6. When is the value of st.upper().endswith(st) True?
    
    
    
    
    
  7. The in operator determines if its left operand occurs in its right operand and returns a boolean value. This means 'a' in 'stranger' evaluates to True. What is the value of 5 in [1,2,3,4]?
    
    
    
    
  8. Explain when the value of st[0:2] in st is False?
    
    
    
    
    
  9. Explain when the value of st[0:2]*2 in st is True?
    
    
    
    
    
  10. When is the value of lst.count(lst[0]) the same as the value of len(lst) for a list lst?
    
    
    
    
    
    
    

    List Comprehensions

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

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

APT Questions

Bagels

  1. Which solution uses indexes to reference the values in the list orders rather than accessing the values directly? How do you know?
    
    
    
    
    
  2. Which solution uses a helper function? What is the function's name?
    
    
    
    
  3. Which solution alters the values in the list passed to bagelCount?
    
    
    
    
  4. Which solution(s) would no longer work if the maximal value of a bagel order is changed from 60 to 100? Why?
    
    
    
    
  5. How could Solution A be modified so that the body of the for loop consists of one assignment statement to bagels -- i.e., so that one of the three assignment statements in the code is used and no others are used, and no if statments are used?
    
    
    
    
    
  6. Write a list comprehension that creates a new list using the values from orders so that the function bagelCount can be written as below, where you fill in the blank with an expression that uses variable b: def bagelCount(orders): return sum([ for b in orders])

ScoreIt/Yahtzee

  1. [4,3,3,5,2]
    
    
    
    
    
  2. Expression with count?
    
    
    
    
    
  3. Number of occurrences of each of 1,2,3,4,5,6:
    
    
    
    
    
    
  4. Value of yahtzee score for each 1,2,3,4,5,6:
    
    
    
    
    
    
  5. Write the function maxPoints using only list comprehensions and list functions, e.g., using a transformative approach.
    
    
    
    
    
    
    

ClassScores

  1. Create a list comprehension that assigns to a variable occs (occurrences) values that represent the number of occurrences of each value 0,1,2,...,100 -- in that order-- in the list scores. The list created will have 101 values. You should use the expression range(0,101) and the list method count.

    For example, if scores = [2,2,2,1,100] the list comprehension should create occs = [0,1,3,0,0,...,0,1] (why?)

    
    
    
    
    
    
    
  2. Given the list from the previous problem how will you determine how many times the mode(s) occur? Use one of the list functions max, sum, or len and the variable occs --- store the maximal number of occurrences of the mode in a variable named mode.
    
    
    
    
    
  3. Write a list comprehension that creates a new list that will be the return value of findMode. This list should use for i in range(0,101) and an if statement that uses values of occs and the value you stored in mode?