In this project, you will write functions that maniuplate lists.Part 0, 3 points: Write a function, swap_ends
, which takes a list and swaps the first and last elements. This function should be void and shouldn't print anything. You can test that it works by looking at what happened to the list:
>>> odds = [1, 3, 5, 7, 9]
>>> swap_ends(odds)
>>> odds
[9, 3, 5, 7, 1]
Expert Practice: It is possible to write this function in one line (in Python). If you figure out how to do this, I want to see your solution! Tester.Part 1, 2 points: Write a function, get_ones
, that takes a non-negative integer parameter and returns a list with that many ones in it:
>>> get_ones(5)
[1, 1, 1, 1, 1]
>>> x = get_ones(0)
>>> x
[]
Expert Practice: It is possible to write this function in one line (in Python). If you make that happen, I want to see it! Tester.Part 2, 3 points: Write a function, has_monkey
, which takes a list of strings and returns whether one of the elements is "monkey"
.Tester.
Part 3, 2 points: Write a function, has_even_size
, which takes a list and returns whether that list has an even number of elements.
>>> numbers = [2, 4, 8]
>>> has_even_size(numbers)
False
Tester.Part 4, 3 points: Write a function, shift_elements
, which takes a list and shifts all elements one cell down, except for the first element, which gets moved to the end:
>>> t = ['banana', 'monkey', 3, [1, 2], None]
>>> shift_elements(t)
>>> t
['monkey', 3, [1, 2], None, 'banana']
Tester.Part 5, 3 points: Write a function, drop_middle
, which takes a list and deletes all the elements except the two ends:
>>> t = ['banana', 'monkey', 3, [1, 2], None]
>>> drop_middle(t)
>>> t
['banana', None]
Tester.Part 6, 0 points: Write a function, get_sum
, which takes a list of numbers and returns the sum of the elements.
Part 7, 0 points: Write a function, get_max
, which takes an list of numbers and returns the biggest element in the list. (You may assume there is at least one element in the list.)
Part 8, 0 points: Write a function, half_list
, which takes a list and returns the first half of that list (leaving the original unchanged). If there are an odd number of elements, you should round down.
>>> half_list([1, 2, 3, 4, 5])
[1, 2]
Part 9, 0 points: Exercise 10.5: is_sorted
.
Part 10, 3 points: Write a function, get_sorted_copy
, which takes a list and returns a sorted copy of that list (leaving the original unchanged).
>>> numbers = [1, 3, 5, 7, 6, 4, 3]
>>> sorted = get_sorted_copy(numbers)
>>> sorted
[1, 3, 3, 4, 5, 6, 7]
>>> numbers
[1, 3, 5, 7, 6, 4, 3]
Tester.Part 11, 3 points: Write a function, reverse_sort
, which takes a list of numbers and sorts it in reverse order (descending order instead of ascending order). Example:
>>> numbers = [1, 3, 5, 7, 6, 4, 3]
>>> reverse_sort(numbers)
>>> numbers
[7, 6, 5, 4, 3, 3, 1]
Tester.Part 12, 3 points: Write a function, double_list
, which takes a list and doubles it, essentially adding another copy of it onto itself. For example,
>>> t = ['hi', 2, 'U']
>>> double_list(t)
>>> t
['hi', 2, 'U', 'hi', 2, 'U']
Tester.Part 13, 3 points: Write a function, combine_and_sort
, which takes two lists of numbers as parameters, and returns a new list containing all elements of the original two lists, but in sorted order. (The original lists should remain unchanged.) Here's an example:
>>> a = [1, 3, 2]
>>> b = [6, 5, 0]
>>> sorted = combine_and_sort(a, b)
>>> sorted
[0, 1, 2, 3, 5, 6]
>>> a
[1, 3, 2]
>>> b
[6, 5, 0]
Tester.Part 14, 3 points: Write a function, all_odd
, which takes an list of integers and determines whether all elements are odd.
>>> all_odd([1, 3, 5])
True
>>> x = all_odd([1, 2, 3])
>>> x
False
Tester.Part 15, 3 points: Write a function, get_longest
, which takes a list of strings and returns the longest one. (If two or more strings are tied for longest, you can return either one.) You can assume that there is at least one element in the list. Tester.
Part 16, 3 points: Write a function strings_list_to_one_string
, which takes a list of strings and turns them all into an English sentence fragment that lists all the individual strings, making correct use of commas and the word "and". The list of strings can have any number of element, but there are three cases to look out for, highlighted in these examples:
>>> strings_list_to_one_string(['snake'])
'snake'
>>> strings_list_to_one_string(['snake', 'werewolf'])
'snake and werewolf'
>>> strings_list_to_one_string(['snake', 'werewolf', 'vampire'])
'snake, werewolf, and vampire'
>>>
My example uses the Oxford comma, but you can choose not to include it instead. Tester.Part 17, 3 points: Write a function, add_five_to_all
, which takes a list of numbers and adds five to all elements, modifying the list:
>>> numbers = [0, 1, .5, 22, -45, 1.21]
>>> add_five_to_all(numbers)
>>> numbers
[5, 6, 5.5, 27, -40, 6.21]
Tester.Part 18, 3 points: Write a function, all_lower_case
, which takes a list of strings and returns a new list with the lower-case version of all those strings. It should not modify the original list! For example:
>>> exclamations = ['MONKEYS!', 'Watch Out!', 'W00T!']
>>> all_lower_case(exclamations)
['monkeys!', 'watch out!', w00t!']
>>> exclamations
['MONKEYS!', 'Watch Out!', 'W00T!']
Tester.Part 19, 3 points: Write no_space_strings
, which takes a list of strings and returns a new list of those same strings, but without any spaces in them. For example:
>>> no_space_strings(["yo yo FTW", "angry beetle"])
['yoyoFTW', 'angrybeetle']
Tester.Part 20, 3 points: Write only_odds
, which takes a list of integers and returns a list containing only the odd elements of the input. For example:
>>> integers = [1, 42, -1, 21, 65536, 127]
>>> only_odds(numbers)
[1, -1, 21, 127]
Tester.Part 21, 3 points: Write a new function, get_palindromes(strings)
, that returns a new list with only the strings that are palindromes. Tester.
Part 22, 3 points: Do Exercise 10.7 from our book: has_duplicates
. Tester.
Part 23, 3 points: Write a function, sum_odds
, which takes an list of integers and returns the sum of all odd elements. This means elements that are odd, not the odd-indexed elements. For example:
>>> sum_odds([1, -7, 5, 12])
-1
>>>
Tester.Part 24, 3 points: Write all_factors(n, numbers)
, which returns True exactly when all elements in numbers are factors of n. (Yes, 1 and n both count as factors.) You can assume that numbers does not contain zero. Here are some examples:
>>> all_factors(16, [1, 2, 4, 8])
True
>>> all_factors(15, [1, 2, 4, 8])
False
This problem comes from exercise 5 of section 12.12 of Allen Downey's Think Java. Tester.Part 25, 3 points: Write a new function, multiples_only(base, integers)
, that returns a new list of numbers that contains only the elements of integers that are multiples of base. For example:
>>> multiples_only(3, [1, 2, 3, 4, -3, -2, -1])
[3, -3]
Tester.Part 26, 3 points: Write a function, get_sums
, which takes a list of lists of integers, and returns an list with the sums of each of the inner lists. For example, the first element in the result will be the sum in the first list in the input. Here's an example:
>>> get_sums([[1, 2], [], [2, 2, 2, -1]])
[3, 0, 5]
>>> get_sums([[1, 1, 1], [1, 1, 1, 1], [1, 1], [1, 1, 1], [1, 1, 1, 1]])
[3, 4, 2, 3, 4]
Tester.Part 27, 3 points: Write a function, get_quadrilateral_types_from_list
, which takes a list of lists of integer pairs, and returns a list of strings categorizing each of the original lists as a rhombus, parallelogram, or quadrilateral. Here's an example:
>>> get_quadrilateral_types_from_list([ [[1, 2], [2,2], [2, 1], [1, 1]], [[3,1], [3,2], [1,2], [1,1]], [[1,1], [1,2], [2,2], [3,1]] ])
['rhombus', 'parallelogram', 'quadrilateral']
You do not need to categorize squares, rectangles, kites, or trapezoids. Tester.Part 28, 0 points: Expert Practice: Write a better version of the previous function better_get_quadrilateral_types_from_list
so that it detects kites, rectangles, squares, and trapezoids. Yes, you're going to need to (do something like) calculate slopes of the line segments.
Part 29, 1 points: Write a new function, without_middle_element
, that returns a copy of a list, but without the middle element.
>>> t = ['hi', 'yo', 'greetings', 'ciao', 'hello']
>>> x = without_middle_element(t)
>>> x
['hi', 'yo', 'ciao', 'hello']
>>> t
['hi', 'yo', 'greetings', 'ciao', 'hello']
Tester.Part 30, 1 points: Write a new function, delete_middle_element
, that deletes the middle element of a list. (If the list has even length, it should round the index down to choose the element to remove.)
>>> t = ['hi', 'yo', 'greetings', 'ciao', 'hello']
>>> x = delete_middle_element(t)
>>> t
['hi', 'yo', 'ciao', 'hello']
>>> print(x)
None
Tester.Part 31, 1 points: Write a new function, remove_evens
, that deletes all even numbers from a list of integers.
>>> t = [0, 1, 1, 2, 3, 5, 8, 13]
>>> x = remove_evens(t)
>>> t
[1, 1, 3, 5, 13]
>>> print(x)
None
Tester.Part 32, 1 points: Write a new function, shrinkify
, that takes a list of numbers and divides all elements by 2, then removes anything with absolute value less than 1.
>>> t =[0, -5, -3, 22, 1337, 42]
>>> shrinkify(t)
>>> t
[-2.5, -1.5, 11.0, 668.5, 21]
>>> shrinkify(t)
>>> t
>>> [-1.25, 5.5, 334.25, 10.5]
>>> shrinkify(t)
>>> t
[2.75, 167.125, 5.25]
Tester.Part 33, 3 points: Write a function, index_of_max_in_range(numbers, low_index, high_index)
, which returns the index of the maximum number between low_index and high_index (inclusive). Here're some examples:
>>> index_of_max_in_range([5, -6, -8, -4, -10, -11], 1, 3)
3
>>> index_of_max_in_range([100, 99, 98, 97, 96, 95, 94], 2, 5)
2
>>> index_of_max_in_range([50, 10, 11, 12, 13, 14, 13, 12, 11, 100], 2, 7)
5
(This and the following two parts are from exercise 10 of section 12.12 of Allen Downey's Think Java.)Tester.Part 34, 3 points: Write swap_elements(array, index_a, index_b)
, which swaps the elements in the list named array. This should be a void function, but it should modify the list. Tester.
Part 35, 4 points: Write selection_sort(numbers)
, which uses the previous two parts to sort the elements in numbers from largest to smallest. This function should not create a new list; it should modify the list that is sent to it. (Thus, it should be void.) Tester.
Part 36, 0 points: Expert Practice: the problems in the Think Java section linked to mostly work for Python as well. Exercises 11 through 13 are excellent.
Part 37, 0 points: Expert Practice: write a function, sort_by_parity
, that takes a list of integers and returns a reordered version of it with the elements sorted so that all the even numbers come before all of the odd numbers. For example:
>>> sort_by_parity([1, 2, 3, 4, 5, 6, 7, 8, 100])
[2, 4, 6, 8, 100, 1, 3, 5, 7]
Submitting your Project:
Make sure all your code is in a file labelled with your user name (everything before the @ in your school email address) followed by _projects.py
all in snake_case. (For example, my file name would be: kburke_projects.py
.) It's very important to name your file correctly in order for me to grade it. Make sure your code runs, then upload it to the project on Canvas. (Don't submit code that doesn't run; you won't earn any points!) Your code should include solutions to all non-zero-point problems from Project 0 onwards. If there is already a file up on Canvas, delete that before uploading the new version or make sure your new file replaces that. (Sometimes Canvas adds a number after the file name. Don't worry about that, because it's something (freaking annoying) you don't have control of. I have a script that automatically deletes that.)