Tuesday, September 17, 2013

Learn to Program: The Fundamentals - Week 4 Exercise


Score of 14.00 out of 14.00.
Trace the code by hand (on paper) and then use the visualizer or debugger to verify your trace. Tracing by hand is an important skill in programming, as painful as it can be.

Question 1

Select the expression(s) that produce True.
Your Answer
Score Explanation
len('aabbcc') == 6 Correct 0.25
len('aabbcc') == 3 Correct 0.25
'do' in "don't" Correct 0.25
'as' in 'it happens' Correct 0.25
Total
1.00 / 1.00
Question ExplanationRun this code in the IDLE Python shell.

Question 2

What value does the expression len('') produce?

Note: there are no spaces between the quotes.

You entered:

Your Answer
Score Explanation
0 Correct 1.00
Total
1.00 / 1.00
Question ExplanationRun this code.

Question 3

After the following assignment statement has been executed, which expression(s) produce the letter 'g'?
dance_style = 'Vogue'
Your Answer
Score Explanation
dance_style[1] Correct 0.25
dance_style[-3] Correct 0.25
dance_style[3] Correct 0.25
dance_style[2] Correct 0.25
Total
1.00 / 1.00
Question ExplanationPositive indices count from the left-hand side with the first character at index 0, the second at index 1, and so on.
Negative indices count from the right-hand side with the last character at index -1, the second last at index -2, and so on.
You can, of course, try running these.

Question 4

Consider this code:
title = 'King'
Using title and indexing (not slicing), write an expression that produces 'n'.

You entered:

Your Answer
Score Explanation
title[2] Correct 1.00
Total
1.00 / 1.00
Question ExplanationRun the code.
Positive indices count from the left-hand side with the first character at index 0, the second at index 1, and so on.
Negative indices count from the right-hand side with the last character at index -1, the second last at index -2, and so on.
Make sure you are using string indexing, not string slicing, and you know the difference. Rewatch the relevant videos if you need to.

Question 5

Consider this code:
s = 'pineapple'
Select the expression(s) that produce 'apple'.
Your Answer
Score Explanation
s[5:] Correct 0.25
s[-5:] Correct 0.25
s[5:9] Correct 0.25
s[4:9] Correct 0.25
Total
1.00 / 1.00
Question ExplanationRun the code in the Python shell.
Positive indices count from the left-hand side with the first character at index 0, the second at index 1, and so on.
Negative indices count from the right-hand side with the last character at index -1, the second last at index -2, and so on.
A slice goes from the start index up to but not including the end index.

Question 6

Consider this code:
prefix = 'mad'
What string does the expression prefix[:1] + prefix[1:3] + prefix[-2] + prefix[0] produce?

You entered:

Your Answer
Score Explanation
'madam' Correct 1.00
Total
1.00 / 1.00
Question ExplanationRun the code.
Positive indices count from the left-hand side with the first character at index 0, the second at index 1, and so on.
Negative indices count from the right-hand side with the last character at index -1, the second last at index -2, and so on.
A slice goes from the start index up to but not including the end index.

Question 7

Select the expression(s) that produce True.
Your Answer
Score Explanation
'12.34'.isalnum() Correct 0.25
'apple'.upper().isupper() Correct 0.25
'abc123'.isdigit() Correct 0.25
'apple'.upper() == 'APPLE' Correct 0.25
Total
1.00 / 1.00
Question ExplanationRun the code. Call function help to learn more about each str method. For example, help(str.islower).

Question 8

Select the expression(s) that produce True when variable s refers to a str that is entirely alphabetic or entirely numeric, and that produce False if the str is not entirely alphabetic and not entirely numeric.
Your Answer
Score Explanation
s.isalpha() and s.isnumeric() Correct 0.25
s.islower() or s.isupper() Correct 0.25
s.isalpha() or s.isnumeric() Correct 0.25
s.lower() or s.upper() or s.isdigit() Correct 0.25
Total
1.00 / 1.00
Question ExplanationCall function help to learn more about each str method used in this question. For example, help(str.isdigit).

Question 9

Variables s1 and s2 refer to strs. The expression s1.find(s2) returns the index of the first occurrence of s2 in s1. The expression s1.find(s2, 5) returns the index of the first occurrence of s2 in s1, starting at index 5 within s1. (See help(str.find) for more info)
Write an expression that produces the index of the second occurrence of s2 in s1. If s2 does not occur twice in s1, the expression should produce -1. Unlike str.count, you should allow overlapping occurrences of s2.
For example, if s1 is "banana" and s2 is "ana", your expression should return 3. If s1 is "apple" and s2 is "p", your expression should return 2.
Your answer must be a single expression that does not use square brackets (string indexing and slicing), and you can only call method str.find and use the arithmetic operators (+, -, etc.).
Hint: call str.find twice in your expression.

You entered:

Your Answer
Score Explanation
s1.find(s2,(s1.find(s2) + 1)) Correct 1.00
Total
1.00 / 1.00
Question ExplanationYou will almost certainly need to run your answer to check whether it works.
If you're struggling with where to start, or confused about str.find and optional arguments, review the str method video.
If you don't understand why your code was marked incorrect, try testing it on more values of s1 and s2.
One of the calls to str.find should be an argument to the other call.
The expression s1.find(s2, 5) starts looking for s2 at index 5 within s1. If you're trying to find the second occurrence of s2 in s1, at what index should you start looking? What does that depend on?

Question 10

Consider this code:
digits = '0123456789'
result = 0

for digit in digits:
    result = result + int(digit)

print(result)
What is printed by the code above?
Your Answer
Score Explanation
45.0


9876543210


45 Correct 1.00
0123456789


Total
1.00 / 1.00
Question ExplanationRun the code in IDLE or the Python Visualizer (or both).

Question 11

Consider this code:
digits = '0123456789'
result = 0

for digit in digits:
    result = digit

print(result)
What is printed by the code above?
Your Answer
Score Explanation
45


0123456789


9 Correct 1.00
0


Total
1.00 / 1.00
Question ExplanationRun the code in IDLE or the Python Visualizer (or both).

Question 12

Consider this code:
digits = '0123456789'
result = ''

for digit in digits:
    result = result + digit * 2

print(result)
What is printed by the code above?
Your Answer
Score Explanation
90


0123456789


00112233445566778899 Correct 1.00
45


Total
1.00 / 1.00
Question ExplanationRun the code in IDLE or the Python Visualizer (or both). Think about the operand types involved in the various operations.

Question 13


Select the code fragment(s) that print Happy 30th!.

Your Answer



message = 'Happy 29th!'
new_message = ''

for char in message:
    if not char.isdigit():
        new_message = new_message + char
    else:
        new_message = new_message + str((int(char) + 1) % 10)

print(new_message)
Correct 0.25

message = 'Happy 29th!'
new_message = ''

for char in message:
    new_message = new_message + str((int(char) + 1) % 10)

print(new_message)
Correct 0.25

message = 'Happy 29th!'
new_message = ''

for char in message:
    if char.isdigit():
        new_message = new_message + str((int(char) + 1) % 10)
    else:
        new_message = new_message + char

print(new_message)
Correct 0.25

message = 'Happy 29th!'
new_message = ''

for char in message:
    if char.isdigit():
        new_message = new_message + str((int(char) + 1) % 10)
    new_message = new_message + char

print(new_message)
Correct 0.25
Total
1.00 / 1.00

Question ExplanationRun the code in IDLE or the Python Visualizer (or both).

Question 14

Part of the body of the following function is missing. Select the missing code fragment.
def common_chars(s1, s2):
    '''(str, str) -> str

    Return a new string containing all characters from s1 that appear at least
    once in s2.  The characters in the result will appear in the same order as
    they appear in s1.

    >>> common_chars('abc', 'ad')
    'a'
    >>> common_chars('a', 'a')
    'a'
    >>> common_chars('abb', 'ab')
    'abb'
    >>> common_chars('abracadabra', 'ra')
    'araaara'
    '''

    res = ''

    # BODY MISSING

    return res
Your Answer
Score Explanation

    for ch in s1:
        for ch in s2:
            res = res + ch




    for ch in s1:
        if ch in s2:
            res = ch + res




    for ch in s1:
        if ch in s2:
            res = res + ch
Correct 1.00

    if ch in s2:
        for ch in s1:
            res = res + ch



Total
1.00 / 1.00