Reference no: EM132400508
Reading and understanding Python code
Consider the following function, in which the names for the function, parameters and local variables (i.e., variables inside the function) are deliberately not made meaningful:
def a(b):
b = b.upper()
c = b.split()
d = " "
for e in c:
d + e[0]
return d
a. What will be printed to the screen when the function is used in the following way?
with open('computer_terms.txt., encoding-'utf8') as infile:
for line in infile:
print(a(line.strip()))
1 # run the above code and show the results
b. Explain step by step each function statement, by referring to the data type involved, and the manipulation performed.
• your
• step
• by
• step
• explanation
• here (double click to edit).
c. Redefine the function by using appriopriate and meaningful names for function, parameter, and local variables. In [ ): I # your code here
d. Is there a way to shorten the code of the function? If so, what do you propose as shorter code?
2. Alliterated names
Write a function alliterated_names ( filename) that reads lines of a text file with personal information (first name, last name, and age, in that order; consult names_ages . txt ), checks the first and last name on alliteration (i.e., first name and last name start with the same letter), and returns a list of alliterated names consisting of last name and first name, in that order, and without age.
3. Loquacious locutions
a. Implement a function longest_worde (filename) that reads a file, tokenizes it, and returns the 10 longest words with their length.
b. Implement a function most_frequent_long_worde (filename) to report the 10 most frequently used long words (15 letters or more) in a file.