The request
In this request, you will develop eight functions. Each function needs to be submitted into CodeRunner. When you press the check button, CodeRunner will give you feedback and a mark for that function.
Question 1 - get_ends_with_a_vowel() function
Define the get_ends_with_a_vowel() function which is passed a list of strings as a parameter. The function returns a list containing all the strings of the parameter list which have a last letter which is a vowel (the letters a, e, i, o, u). You can assume that none of the elements of the parameter list are the empty string. For example, the following code:1
2
3print("1.", get_ends_with_a_vowel(["Cain", "Jessie", "Robert", "Geoffrey", "Li"]))
print("2.", get_ends_with_a_vowel(["Jess", "Cain", "Amity", Raeann"]))
print("3.", get_ends_with_a_vowel([]))
prints:1
2
31. ['Jessie', 'Li']
2. []
3. []
Question 2 - get_common_words_used() function
Define the get_common_words_used() function which is passed a string of text and a list of commonly used words (strings) as parameters. The function returns a list containing all the words in the text string which are in the list of commonly used words. The returned list should contain no duplicates and should be sorted (use the list method, sort()). The string of text should be converted to lower case before you do any checking as the commonly used words are all in lower case. For example, the following code:1
2
3
4print("1.", get_common_words_used("A bus station is where a bus stops A train station is where a train stops On my desk I have a work station", ["a", "is", "i", "on", "the"]))
print("2.", get_common_words_used("Easy come, easy go go go", ["a", "go", "i", "on", "the"]))
print("3.", get_common_words_used("Easy come, easy go go go", ["a" , "is", "i", "on", "the"]))
print("4.", get_common_words_used("", ["a", "is", "i", "the"]))
prints:1
2
3
41. ['a' , 'i' , 'is' , 'on']
2. ['go']
3. []
4. []
Question 3 - get_text_value() function
Define the get_text_value() function which is passed a string of text as a parameter. The function returns an integer which is the total of the value of each word of the parameter string. The words of the parameter string are valued based on the value of the first letter added to the value of the last letter. If the word has only one letter then the value of the word is just the value of the first letter. The first two lines of code in this function are:
1 | letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', k', '1', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'V', 'w', 'x', 'y', 'z'] |
where the letters list contains every lowercase letter of the alphabet and the letter_values list contains the value corresponding to each letter of the alphabet, i.e., the first element is the value of the letter ‘a’, the second element is the value of the letter b’,and so on.
The string of text parameter should be converted to lower case before you do any checking as the letters of the alphabet are all in lower case. You can assume that the first and the last letters of all the words are alphabetic characters. For example, the following code:1
2
3
4print("1. Text value:", get_text_value("abracadabra"))
print("2. Text value:", get_text_value("a b"))
print("3. Text value:", get_text_value("enjoy Today"))
print("4. Text value:", get_text_value(""))
prints:1
2
3
41. Text value: 2
2. Text value: 5
3. Text value: 21
4. Text value: 0
Question 4 - is_legitmate_code() function
Define the is_legitmate_code() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string denotes a legitimate code or not. A legitimate code is a string made up of one letter followed by any
number of digits or spaces (at least one digit). The first three lines of code inside the function should be:1
2
3code-letters = ["A", "B", "Z", "T", "X"]
min_for_each_letter = [2, 2, 1, O, 4] # inclusive
max_for_each_letter = [8, 9, 6, 7, 5] # inclusive
where:
- code_letters is the list of code letters which are legitimate for the first letter of the code string,
- min_for_each_letter is a list which contains the minimum number (inclusive) for each digit following that letter,
- max_for_each_letter is a list which contains the maximum number (inclusive) for each digit following that letter.
For example, the third element of the code_letters list is the letter ‘Z’, the corresponding third element of the min_for_each_letter list is 1 and the corresponding third element of the max_for_each_letter list is 6. This indicates that the code digits which follows the letter ‘Z’ can be any number made up of the digits 1, 2, 3, 4, 5 or 6. The number part of the code string can contain any number of spaces.
Note: The code number part of a parameter string to be tested could contain an alphabetic character thus making the code not legitimate. You will find it useful to use the isdigit() method which returns True if the string is a digit, False otherwise.For example, the following code:1
2
3
4
5print("1.", is_legitmate_code('B747346'))
print("2.", is_legitmate_code('X 444 454'))
print("3.", is_legitmate_code('T 444854'))
print("4.", is_legitmate_code('X 444X454'))
print("5.", is_legitmate_code('X '))
prints1
2
3
4
51. True
2. True
3. False
4. False
5. False
Question 5 - get_fail_pass_average() function
Define the get_fail_pass_average() function which is passed a list of integers as a parameter where each integer represents a mark out of 100. The function returns a tuple made up of the average of all the marks which are less than 50, followed by the average of all the marks which are 50 or more (both averages are rounded to the nearest whole number). If there are no fail marks then the average fail mark should be set to -1 and if there are no pass marks then the average pass mark should be set to -1. For example, the following code:1
2
3
4
5print("1.", get_fail_pass_average([63, 65, 33]))
print("2.", get_fail_pass_average([63, 62, 100, 100]))
print("3.", get_fail_pass_average([33, 42, 20, 10]))
print("4.", get_fail_pass_average([50, 50, 100, 0]))
print("5.", get_fail_pass_average([]))
Question 6 - get_triple_sums_list() function
Define the get_triple_sums_list() function which is passed a list of integers as a parameter. The function returns a new list containing all the sums of the parameter list elements taken three at a time, i.e., the sum of the first three elements, the sum of the next three elements, and so on. If there are elements of the parameter list which are left over, the final element of the returned list is the sum of these leftover elements (it could be that there is one or that there are two leftover elements). If the parameter list is empty, the function returns the empty list.
Question 7 - remove_doubles() function
Define the remove_doubles() function which is passed a list of integers as a parameter. The function removes any element in the list which is the same as the previous element.
Question 8 - get_dice_score( ) function
In a dice rolling game a hand is made up of eight random dice throws and is valued in the following way:
- Each dice which is part of a run of dice starting from a 1 is valued at 3 points.
- If there is no 1 in a hand of eight dice then the score for the whole hand is 0.
- A hand of dice can contain more than one run.
Study the following five example hands of eight dice and their corresponding valuation.