python project2

Instructions

Complete the questions.

Program Output Example:

Program Text:

print "X",
print "X",
Output:

XX
Problem 1: Common Errors
Each of the following snippets represents a common error made by an introductory
 programming student. Identify the mistake and provide a corrected version.

1. Convert my_string to lowercase
Program Text:

python
my_string.lower()
Error: The function does not modify my_string in place.

Corrected Version:

python
my_string = my_string.lower()
2. Print every element in my_list in reverse order
Program Text:

python
for i in my_list.reverse():
    print i,
Error: .reverse() modifies the list in place and returns None.

Corrected Version:

python
for i in reversed(my_list):
    print i,
or

python
my_list.reverse()
for i in my_list:
    print i,
my_list.reverse()  # Restore original order
3. Reverse the order of elements in my_list
Program Text:

python
my_list.reverse
Error: .reverse is a method but is not called.

Corrected Version:

python
my_list.reverse()
4. Multiply user_input by 10 and add it to my_list as a string
Program Text:

python
bigger_input = user_input + 0
my_list.append(user_input)
Errors:

user_input + 0 is incorrect because user_input is a string.

The multiplication step is missing.

Corrected Version:

python
bigger_input = str(int(user_input) * 10)
my_list.append(bigger_input)
5. Backup my_list and remove the largest element
Program Text:

python
new_list = my_list
my_list.remove(max(my_list))
Error: Both new_list and my_list refer to the same object.

Corrected Version:

python
new_list = my_list[:]  # Creates a copy
my_list.remove(max(my_list))
or

python
import copy
new_list = copy.deepcopy(my_list)
my_list.remove(max(my_list))
6. Finding the last occurrence of e in my_list
Program Text:

python
def rindex(my_list, e):
    """Finds the position of the last occurrence of e in my_list."""
    if e not in my_list:
        return -1
    my_list.reverse()
    for i in range(len(my_list)):
        if my_list[i] == e:
            return len(my_list) - 1 - i
Error: my_list.reverse() modifies the original list but does not restore its order.

Corrected Version:

python
def rindex(my_list, e):
    if e not in my_list:
        return -1
    return len(my_list) - 1 - my_list[::-1].index(e)
7. Print all elements in my_list
Program Text:

python
for i in my_list:
    print i
    i = i + 1
Error: i = i + 1 has no effect since i is a loop variable.

Corrected Version:

python
for i in my_list:
    print i
8. Find the largest element in my_list
Program Text:

python
def find_max(list):
    """Finds the largest integer in list."""
    max = 0
    for i in my_list:
        if i > max:
            return i
    return max
Errors:

The loop returns on the first value greater than max, breaking too early.

max is used as a variable name, which overrides the built-in function max().

Corrected Version:

python
def find_max(my_list):
    """Finds the largest integer in my_list."""
    max_val = my_list[0]  # Start with first element
    for i in my_list:
        if i > max_val:
            max_val = i
    return max_val
or simply:

python
def find_max(my_list):
    return max(my_list)
Problem 2: Meaningful Names
Question:
The function should swap the maximum and minimum elements in a list.

Original Code:

python
def f3(ll):
    j = 0
    k = 0
    for i in range(len(ll)):
        if ll[i] > ll[j]:
            j = i
        elif ll[i] < ll[k]:
            k = i
    l = ll[j]
    ll[k] = ll[l]
    ll[j] = ll[k]
Errors:

ll[k] = ll[l] should be ll[k] = l.

ll[j] = ll[k] should be corrected to swap the values.

Corrected Version:

python
def swap_max_min(lst):
    max_index = 0
    min_index = 0
    for i in range(len(lst)):
        if lst[i] > lst[max_index]:
            max_index = i
        elif lst[i] < lst[min_index]:
            min_index = i
    temp = lst[max_index]
    lst[max_index] = lst[min_index]
    lst[min_index] = temp
Problem 3: Test Cases
Question:
We need meaningful test cases for a custom square root function.

Given Code:

python
SQ_3 = ...  # Square root of 3
test_cases = [_____, _____, _____, _____, _____]
test_case_answers = [_____, _____, _____, _____, _____]

def custom_sqrt(num):
    """Returns the square root of num, or 0 if num < 0"""
    ...  # (Code snipped)

for i in range(len(test_cases)):
    if custom_sqrt(test_cases[i]) != test_case_answers[i]:
        print "Test Case #", i, "failed!"
Possible Test Cases:
python
test_cases = [0, 1, 4, 9, 16, -5]
test_case_answers = [0, 1, 2, 3, 4, 0]  # Expected square roots

Quiz

To mark this module as complete, you must finish this quiz. Once submitted, you'll need to wait 2 hours before attempting it again.