Monday 30 September 2013

PYTHON WRITTEN ANSWERS

                                  BOX ANSWERS


Question 2: Consider this code: 
def mystery(s):
    """ (str) -> bool
    """
    matches = 0
    for i in range(len(s) // 2):
        if s[i] == s[len(s) - 1 - i]: # <-- How many times is this line reached?
            matches = matches + 1
    
    return matches == (len(s) // 2)

mystery('civil')
Trace the function call mystery('civil') using the Python Visualizer. How many times is the line marked above reached?
 
Answer= 2

Question 6

Consider this code:
values = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Using values and indexing with non-negative indices, write an expression that evaluates to 5. Do not use addition, subtraction, or parentheses ( ) (brackets [ ] are required).
 
Answer: values[1][1]

Question 7

Consider this code:
breakfast = [['French', 'toast'], ['blueberry', 'pancakes'], ['scrambled', 'eggs']]
Using breakfast and indexing with only negative indices, write an expression that evaluates to 'blueberry'. Do not use addition, subtraction, or parentheses ( ) (brackets[ ] are required).
 
Answer: breakfast [-2][-2]

No comments:

Post a Comment