Question

In: Computer Science

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

Exercise 9 – Writing values from a list into a file Complete the function design for the write_most_frequent() function, which takes 4 parameters, a string, a list of tuples, an integer, and another string: • The first string represents the name of the file to write to (with the append usage mode) • The list of tuples contains the information to write. Assume the list has already been sorted. • The integer represents the number of elements to read from the list and write to the file • The title should be written to the file before 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 the following:
   # 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 the following:
   # 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 is sorted;
# the title given should be written on its own line first
def write_most_frequent(filename, list, n, title):
   print("Fix me")

Solutions

Expert Solution

If you have any queries write a comment. If you have understood upvote thank you.

SOLUTION:

def write_most_frequent(filename, list1, n, title):
file=open(filename,"a+")
##open file in append and write mode so that the data is appended to the file
file.write("Result for "+title+"\n")
##append title to the file start
for i in range(n):##append the data to the file
file.write(list1[i][0]+" : "+str(list1[i][1])+"\n")
file.close()
##close the file
def test_write_most_frequent():
print("testing write_most_frequent")
##list of data
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)]
##call function
write_most_frequent("test_output.txt", list1, 5, "Alphabet Statistics")


##call the test write most frequent
test_write_most_frequent()

CODE IMAGE:

OUTPUT TEXT FILE:


Related Solutions

Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the...
In python Complete the function get_Astring(filename) to read the file contents from filename (note that the test will use the file data.txt and data2.txt provided in the second and third tabs), strip off the newline character at the end of each line and return the contents as a single string.
Complete exercise 9.20 from the text. (a) Create a function called polygon that draws a polygon...
Complete exercise 9.20 from the text. (a) Create a function called polygon that draws a polygon in a polar plot. Your function should have a single input parameter – the number of sides. (b) Use a for loop to create a figure with four subplots…You should use the function you created in part (a) to draw each polygon. Use the index parameter from the for loop to specify the subplot in which each polygon is drawn, and in an expression...
List all of the values of the sine function that you know. Remember that values of...
List all of the values of the sine function that you know. Remember that values of sin(x) repeat every 2π radians, so your answer should include infinitely many values.
Complete the function main in file median.c to implement the computation of the median of a...
Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program should use realloc to allow an arbitrary number of integers to be read into a dynamically allocated array. Every time realloc is called, let the new size of the array be twice the old size plus 1. Look at function readLongLine in the slides for Chapter 6 for an example of how to use realloc. The...
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT