Python代写:COMPSCI101 Python Lists

代写关于List用法的Python基础作业,一共编写八个问题。

The Assignment

In this assignment, 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
3
print("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. ['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
4
print("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. ['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
2
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']
letter_values = [1, 4, 2, 3, 1, 4, 3, 4, 1, 7, 7, 4, 6, 6, 1, 3, 9, 2, 2, 2, 1, 8, 5, 9, 9, 9]

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
4
print("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. 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
3
code-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
5
print("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 '))

prints:

1. 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
5
print("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([]))

prints:

1. (33, 64)
2. (-1, 81)
3. (26, -1)
4. (0, 67)
5. (-1, -1)

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. For example, the following code:

1
2
3
4
5
print("1.", get_triple_sums_list([1, 2, 3]))
print("2.", get_triple_sums_list([1, 5, 3, 4, 5, 2]))
print("3.", get_triple_sums_list([1, 6, 2, 4]))
print("4.", get_triple_sums_list([1, 6, 2, 4, 3]))
print("5.", get_triple_sums_list([]))

prints:

1. [6]
2. [9, 11]
3. [9, 4]
4. [9, 7]
5. []

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. For example, the following code:

1
2
3
4
5
6
7
8
9
10
11
a_list = [1, 1, 3, 4, 4, 1]
remove_doubles(a_list)
print("1.", a_list)

a_list = [1, 1, 1, 4, 4, 4, 1, 1, 1]
remove_doubles(a_list)
print("2.", a_list)

a_list = [1, 1, 1, 1]
remove_doubles(a_list)
print("3.", a_list)

prints:

1. [1, 3, 4, 1]
2. [1, 4, 1]
3. [1]

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. Make sure you understand how the hands are valued:

[5, 3, 2, 5, 5, 6, 4, 3] hasvalue 0
[3, 4, 1, 5, 3, 1, 4, 6] has value 6 (contains one run with just the dice [1] and a second run with just [1])
[5, 3, 2, 6, 4, 5, 1, 4] has value 18 (contains one run with the dice [1, 2, 3, 4, 5, 61)
[2, 1, 1, 1, 2, 3, 3, 2] has value 24 (contains one run with the dice [1, 2, 3], a second run with the dice [1, 2, 3], and another run with the dice [1, 2])
[3, 4, 1, 5, 2, 1, 4, 6] has value 21 (contains one run with the dice [1, 2, 3, 4, 5, 6], another run with the dice [1])

Complete the get_dice_score() function which is passed a list of eight dice throws and returns the value of the hand according to the rules described above.