Reference no: EM132356271
Question
Problem 1: Message
Coder
For parts 1 and 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions 2 you will need to use the functions ord and chr.
ord takes a character as an argument and returns its ASCII integer equivalent. For example:
ord('a') returns 97
ord('A') returns 65
chr takes an integer as an argument and returns the ASCII character equivalent to it. For example:
chr(97)(97) returns 'a'
chr(65)(65) returns 'A'
Note also that the entire character set has an ordering defined on it. So, the characters a to z are in increasing order (a < b < ... <y < z) and so are the characters A to Z (A < B < ... < Y < Z).
Part 1:
Write function preceding that takes a single character as an input and returns a code for that character. The coding scheme is as follows:
if the character is 'a', then the code for the character is 'z'
if the character is 'A', then the code for the character is 'Z'
if the character is between 'b' and 'z', then the code for the character is the letter that precedes it. For example, the code for 'b' is 'a', the code for 'c' is 'b' and the code for 'z' is 'y'
if the character is between 'B' and 'Z', then the code for the character is the letter that precedes it. For example, the code for 'B' is 'A', the code for 'C' is 'B' and the code for 'Z' is 'Y'
if the character is anything else, then the code for the character is the character itself. For example, the code for '!' is '!', the code for ' ' is ' ' (space is space), etc.
preceding('a') returns 'z'
x='!'
preceding(x) returns '!'
part 2:
Write function succeeding that takes a single character as an input and returns a code for that character. The coding scheme is as follows:
if the character is 'z', then the code for the character is 'a'
if the character is 'Z', then the code for the character is 'A'
if the character is between 'A' and 'Y', then the code for the character is the letter that succeeds it. For example, the code for 'A' is 'B', the code for 'B' is 'C' and the code for 'Y' is 'Z'A' and 'Y', then the code for the character is the letter that succeeds it. For example, the code for 'A' is 'B', the code for 'B' is 'C' and the code for 'Y' is 'Z'
if the character is between 'a' and 'y', then the code for the character is the letter that succeeds it. For example, the code for 'a' is 'b', the code for 'b' is 'c' and the code for 'y' is 'z'a' and 'y', then the code for the character is the letter that succeeds it. For example, the code for 'a' is 'b', the code for 'b' is 'c' and the code for 'y' is 'z'
if the character is anything else, then the code for the character is the character itself. For example, the code for '!' is '!', the code for ' ' is ' ' (space is space), etc.
succeeding('a') returns 'b'
x='!'
succeeding(x) returns '!'
part 3:
Write function message_coder that takes two arguments - a string and a function - and returns a coded message that uses the function for coding each character in the string.
For example:
message_coder('hello dolly zebra!',preceding) returns 'gdkkn cnkkx ydaqz!'
message_coder('hello dolly zebra!',succeeding) returns 'ifmmp epmmz afcsb!'
Problem 2
Write function word_distribution that takes as inputs a string and an optional word_list as arguments and returns a dictionary containing the frequency of each word in the text, or of each word in the word_list if it is included in the function call. For example if the argument to the function is: example if the argument to the function is:
text_string = "Hello. How are you? Please say hello if you don't love me!"
print(word_distribution(text_string)) should print
{'hello': 2, 'how':1, 'are':1, 'you':2,'please':1, "don't": 1 ...}
If the word_list is provided then the function should return the count of words in the word_list and ignore all other words in the text. word_list is the set of words that should be counted. (The value for word_list should default to None.)
text_string = 'I came, I saw, I conquered!'
word_distribution(text_string,word_list=['I','saw','Britain'])
returns
{'i':3,'saw':1,'britain':0}
Make sure that you exclude all punctuation from your count and that your count ignores case. Thus, Hello and hello are the same word in the above example. The . ? and ! are all ignored. But the apostrophe in don't doesn't count and don't is treated as a single word.
Notes:
word.split() splits a string on spaces and returns a list. Note that this will concatenate punctuation with words. For the above example, you will get:
['Hello.','How','are','you?',...]