Python代写:COMPSCI101 Python Dictionaries

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

The Assignment

In this assignment, you will develop seven functions. Each function needs to be submitted into CodeRunner. When you press the check button, CodeRunner will give you feedback and a mark for the function.

Question 1 - draw_histogram() function

Define the draw_histogram() function which is passed one parameter, a dictionary. The keys of the dictionary are single letters and the corresponding values are integers, e.g., {'b': 5 , 'a': 6 , 'c': 3}. For each keyzvalue pair in the dictionary the function prints the key, followed by “ : “, followed by a series of dashes ('-'). The number of dashes printed is given by the value corresponding to the key. The keys are printed in alphabetical order, each on a separate line. For example, the following code:

1
draw_histogram({'a': 2, 'c': 5, 'b': 7})

prints the following:

a: --
b: -------
c: -----

Question 2 - print_most_frequent() function

Define the print_most_frequent() function which is passed two parameters, a dictionary containing words and their corresponding frequencies (how many times they occurred in a string of text), e.g.,

1
{"fish":9, "parrot":8, "frog":9, "cat":9, "stork":1, "dog":4, "bat":9, "rat":4}

and, an integer, the length of the keywords in the dictionary which are to be considered.

The function prints the keyword length, followed by “ letter keywords: “, then prints a sorted list of all the dictionary keywords of the required length which have the highest frequency, followed by the frequency. For example, the following code:

1
2
3
4
5
6
7
word_frequencies = {"fish":9, "parrot":8, "frog":9, "cat":9, "stork":1, "dog":4, "bat":9, "rat":3}

print_most_frequent(word_frequencies, 3)
print_most_frequent(word_frequencies, 4)
print_most_frequent(word_frequencies, 5)
print_most_frequent(word_frequencies, 6)
print_most_frequent(word_frequencies, 7)

prints the following:

3 letter keywords: ['bat', 'cat'] 9
4 letter keywords: ['fish', 'frog'] 9
5 letter keywords: ['stork'] 1
6 letter keywords: ['parrot'] 8
7 letter keywords: [] 0

Question 3 - get_text_valuation() function

Define the get_text_valuation() function which is passed two parameters, a dictionary and a string of text. The keys of the dictionary are single letters and the corresponding values are integers, the value of the key letter, e.g., {'b': 5, 'a': 6, 'c': 3}. The function returns the total valuation (an integer) of the string of text where:

  • if the letter from the text is a keyword of the dictionary then its value is the integer corresponding to the letter in the dictionary.
  • any alphabetic characters from the text which are not in the dictionary are worth 1, and,
  • all non alphabetic characters are worth 0 (use the isalpha( ) method to check if a character is alphabetic).

You can assume that all the keys in the dictionary are lowercase characters.
You will need to change the text to lowercase before you work out the total value of the
text. For example, the following code:

1
2
3
4
5
6
7
letter_value_dict = {"r":2, "s":2, "z":14, "t":3, "m":7, "g":3, "q":8}
word = "gst"
print(word, "-", get_text_valuation(letter_value_dict, word))
word = "exhaustipated"
print(word, "-", get_text_valuation(letter_value_dict, word))
word = "too much month at the end of the money"
print(word, "-", get_text_valuation(letter_value_dict, word))

prints:

gst - 8
exhaustipated - 18
too much month at the end of the money - 58

Question 4 - get_dictionary_from_file() function

Define the get_dictionary_from_file() function which is passed a filename as a parameter. The file contains words and their meaning, each word with its meaning is on a new line and “:“ separates the word from its meaning. An example file is:

allegator : some who alleges.
ecdysiast : an exotic dancer, a stripper.
eructation : a burp, belch.
lickety-split : as fast as possible.
lickspittle : a servile person, a today.

The get_dictionary_from_file() function returns a dictionary of words and their corresponding meanings. Note that the “:“ is not part of the meaning but the full stop at the end of the meaning is included. You can assume that there is only one “:“ in each line in the dictionary.

For example, the following code:

1
2
3
the_dict = get_dictionary_from_file("FiveWordMeanings.txt")
for word in ["lickspittle", "ecdysiast", "allegator", lickety-split", "eructation"]:
print(word, "=", the_dict[word])

prints:

lickspittle = a servile person, a today.
ecdysiast = an exotic dancer, a stripper.
allegator = someone who alleges.
lickety-split = as fast as possible.
eructation = a burp, belch.

Question 5 - get_word_len_dict() function

Define the get_word_len_dict() function which is passed a string of text as a parameter. The function returns a dictionary with keys which are integers and corresponding values which are sorted lists of unique words. The sorted list of words corresponding to a key contains all the unique words from the text that have a length equal to the key value.
For example, the following code:

1
2
3
4
5
6
7
text = "the faith thatimahad had had had an affect on his life"
the_dict = get_word_len_dict(text)
keys_list = list(the_dict.keys())
keys_list.sort()
for number in keys_list:
words = the_dict[number]
print(number, "-", words)

prints:

2 - ['an', 'he', 'on']
3 - ['had', 'his', 'the']
4 - ['life', 'that']
5 - ['faith']
6 - ['affect']

Question 6 - get_previous_words_dict() function

Define the get_previous_words_dict() function which is passed a string of text as a parameter. The function returns a dictionary with keys which are all the unique words from the text and the corresponding values are lists of all the words in the text which come before the key word. Note that in the list of corresponding words, the same word can appearrnoretharionce.

The first word in the sentence will initially have the empty string (' ') as its previous word.

You can assume that the text is all in lower case and contains no punctuation.

For example, the following code:

1
2
3
4
5
6
7
8
9
sentence = "we must all hang together or assuredly we shall all hang separately"

previous_words_dict = get_previous_words_dict(sentence)
keys_list = list(previous_words_dict.keys())
keys_list.sort()
for word in keys_list:
word_list = previous_words_dict[word]
word_list.sort()
print(word, "-", word_list)

prints:

all - ['must', 'shall']
assuredly - ['or']
hang - ['all', 'all']
must - ['we']
or - ['together']
separately - ['hang']
shall - ['we']
together - ['hang']
we - [", 'assuredly']

Question 7 - get_names_num_tuple_dict() function

Define the get_names_num_tuple_dict() function which is passed a filename as a parameter. The file contains lines of text where each line is made up of a name followed by a series of numbers. An example input file is:

Brian 4 5 5 3 2 3 4 1
Sec 3 5 8 33
John 2 33 61 3 1 1
Elia 4 5 8 3 7
Ali 5 17 6 32 2 35 71 19

The get_names_num_tuple_dict() function returns a dictionary with the names as keywords and corresponding values which are tupIes of numbers. The first number after the name controls how many numbers are to be included in the corresponding tuple (the numbers included in the tuple are taken from the left to right). For example, the line of text “Ali 5 17 6 32 2 35 71 19” has 5 as the first number after the name and this line of text becomes the dictionary entry with the keyword “Ali “ and with the corresponding
value tuple made up of the five numbers "Ali":(17, 6, 32, 2, 35).

You can assume that there are always enough numbers in each line of the input file.

For example, the following code:

1
2
3
4
5
6
7
names_and_nums_dict = get_names_num_tuple_dict("ShortNamesAndNums.txt")
keys_list = list(names_and_nums_dict.keys())
keys_list.sort()

for keyword in keys_list:
names_nums_tuple = names_and_nums_dict[keyword]
print(keyword, "-", names_nums_tuple)

prints:

Ali - (17, 6, 32, 2, 35)
Brian - (5, 5, 3, 2)
Elia - (5, 8, 3, 7)
John - (33, 61)
Seo - (5, 8, 33)