Exercise 9 – Writing values from a list into a file Complete the function design for...

90.2K

Verified Solution

Question

Programming

Exercise 9 – Writing values from a list into a file Complete thefunction design for the write_most_frequent() function, which takes4 parameters, a string, a list of tuples, an integer, and anotherstring: • The first string represents the name of the file to writeto (with the append usage mode) • The list of tuples contains theinformation to write. Assume the list has already been sorted. •The integer represents the number of elements to read from the listand write to the file • The title should be written to the filebefore writing data from the list.

def test_write_most_frequent():
   print(\"testing write_most_frequent\")
   list1 = [(\"a\",27), (\"bc\",25), (\"defg\",21), (\"hi\",21),(\"jk\",18),
           (\"l\",17),(\"m\",16), (\"nop\", 15), (\"qr\", 14), (\"s\", 13),
           (\"t\",10),(\"uv\",9), (\"x\",5), (\"yz\",2)]
   write_most_frequent(\"test_output.txt\", list1, 5,\"Alphabet Statistics\")
   # Should append to a file called test_output.txt thefollowing:
   # Results for Alphabet Statistics:
   # a: 27
   # bc: 25
   # defg: 21
   # hi: 21
   # jk: 18

   write_most_frequent(\"test_output.txt\", list1, 12,\"Large Alphabet Statistics\")
   # Should append to a file called test_output.txt thefollowing:
   # Results for Large Alphabet Statistics:
   # a: 27
   # bc: 25
   # defg: 21
   # hi: 21
   # jk: 18
   # l: 17
   # m: 16
   # nop: 15
   # qr: 14
   # s: 13
   # t: 10
   # uv: 9

-------------------------------------
# (str, (list of tuple), int, str -> None)
# appends to the file named filename data from the first
# n elements found in the given list; assumes the list issorted;
# the title given should be written on its own line first
def write_most_frequent(filename, list, n, title):
   print(\"Fix me\")

Answer & Explanation Solved by verified expert
4.1 Ratings (619 Votes)
If you have any queries write a comment If you have understoodupvote thank youSOLUTIONdef writemostfrequentfilename list1 n    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students