## Practice Problems for Module 04

# Part 1 - Warm up questions

intro_list = [1,2,3,-4,-5,"six", "7", True]

# Ensure that you understand the lists created by the following list slices.
#intro_list[2:4]
#intro_list[4:10]
#intro_list[3:4]
#intro_list[3:3]
#intro_list[10:4]
#intro_list[:3]
#intro_list[4:]
#intro_list[1:10:2]
#intro_list[2:6:-1]
#intro_list[6:2:-1]
#intro_list[::-1]

# Ensure you understand the following list subscripting
#intro_list[0]
#intro_list[5]
#intro_list[10]  
#intro_list[-1]
#intro_list[-4]
#intro_list[-20]




# Write a Pyhton function find_multiples that consumes values, a list of integers,
# and m, a positive integer, and returns a list containing all multiples of m in values.
# For example, 
# find_multiples([1,2,3,4,5,6], 7) => []
# find_multiples([1,2,3,4,5,6], 2) => [2,4,6]

def find_multiples(values, m):
    pass

## Mutating list: Answer the questions posed in the code below. 
L = [1,2,3]
L.append('a')             # What is value of L?
M = L                     # M & L are aliases. Why?
M.extend([True, False])   # What is the value of L? M? 
L.insert(3,"new")         # What is the value of L? M?
L = []                    # What is the value of L? M? Are they aliases?
M.append(1)               # What is the value of M?
M.remove(1)               # What is the value of M?
x = M.pop(1)              # What is value of x? M?

## Solve the following using built-in list operations (no recursion needed).
P = [1,2,1,3,1,4,2,3,4]
# Verify that P contains 3 occurrences of 1. 
# Write three statements to find the indices of these three occurrences of 1 in P
#   using the index function. 
# How could you check if there is a fourth occurrence of 1 in P using the results
#   of the step above, slicing, and in? Don't use count here.



# Use abstract list functions and lambda to write the following two function:
# 
# Write a Python function find_As that consumes grades, a list of natural numbers
#   between 0 and 100, and returns the list of values in grades greater or
#   equal to 80.
# e.g. find_As([87,45,90,80,67,75,100]) => [87.90,80,100]

def find_As(grades):
    pass


# Use abstract list functions and lambda to write the following function:

# Write a Python function keep_last that consumes a list of nonempty strings,
#   and returns the list containing only the last characters in each string.
# e.g. keep_last(["abc", "abba", "hit"]) => ['c','a','t']

def keep_last(words):
    pass


# Write a Python function, clear_all, that consumes a vals, a list of integers,
# and uses recursion to mutate the list to contain all zeroes. 
# The function returns None.
# Be sure to include the full design recipe for clear_all. 

def clear_all(vals):
    pass



# Part 2 - Extra Practice

# Problem 1
## Part a
## Builds on a quesion in Module 03 practice
## Write a function new_names that consumes two lists - one of people's 
## current full names, and a second, parallel list, of their new last names. The 
## function returns a new list containing all the updated names.  
## For example, update_names(["Abigail Melissa Smith", "Hao Chan", 
## "Timothy Howe"], ["Jones", "Chan", "Howe-Williams"]) => 
##     ["Abigail Melissa Jones", "Hao Chan", "Timothy Howe-Williams"]
##
## Part b
## Rewrite new_names using abstract list functions.

# Problem 2
## Part a
## Write a recursive function count_A_grades which consumes a 
## list of numbers in the range [0:100] and returns the number
## of values from the list in the range [80:100]. 
##
## Part b
## Rewrite count_A_grades using abstract list functions. 

# Problem 3
## Write the function strange_change, which consumes vals, a non-empty list of numbers, and
## sets a value in the list to 100, based on the value of the first
## element in the list. If vals[0] a valid index (i.e. between 0 and len(vals)-1), the value
## stored at pos vals[0] is set to 100. If vals[0] < 0, then the first value
## in the list is set to 100. If vals[0) >= len(lst), then the value
## at the last list position is set to 100. The function returns the
## index of the changed value.
## For example,  
## L = [3,2,3,5,8,0]
## strange_change(L) => 3, and modifies L to [3,2,3,100,8,0]
## L = [-2,2,4,9]
## strange_change(L) => 0, and modifies L to [100,2,4,9]
## L = [90,3]
## strange_change(L) => 1, and modifies L to [90,100].

# Problem 4
## Builds on a quesion in Module 03 practice
## Write a function change_titles which consumes a list of names, 
## and modifies that list so that all names with titles "Miss" and "Mrs." 
## are changed to include the title "Ms." instead.

# Problem 5
## Part a
## Write a recursive function count_in_range  which consumes a 
## list of integers, numbers, and two other integers, lo and hi,
## and returns the number of values v in numbers
## that satisfy lo <= v <= hi. 
##
## Part b
## Rewrite using abstract list functions

# Problem 6:
## Write a Python function get_positives that consumes nothing, and returns
## a list of positive integers entered by the user at the keyboard while the 
## function is running. The functon asks the user to enter an integer. If a positive
## value is entered, it is appended to the list created so far. If 0 is entered,
## the program continues but 0 is not added to the list. If a negative value
## is entered, the function terminates. 

# Problem 7:
## Part a
## Write a  function add_numbers that consumes a list containing any number of
## values of any type, and returns the sum of the numeric values (int or float)
## in the list. The result should be produced as a floating point value.
## For example, 
## * add_numbers([1,2,3.4,4,5]) => 15.4
## * add_numbers(['a', True]) => 0
##
## Part b
## Rewrite using abstract list functions

# Problem 8
## Write a function list_mod that consumes a list of natural numbers, counts, and a positive 
## natural number, n, and mutates the list so that each entry in counts, say k, is 
## changed to k mod n (remainder). The function returns None.

# Problem 9
## Write a function modify that consumes a list of values, info, and 
## changes all entries in the list containing strings to contain 0 instead.
## The function returns the number of changed values.


