question archive I am not sure how to complete the following code? def reverse_file(f): """Returns a reverse of the file in a new list called reverse_list

I am not sure how to complete the following code? def reverse_file(f): """Returns a reverse of the file in a new list called reverse_list

Subject:Computer SciencePrice:3.86 Bought11

I am not sure how to complete the following code?

def reverse_file(f):

"""Returns a reverse of the file in a new list called reverse_list.

Args:

f: a freshly opened file in the format of the file alkaline.txt

Return:

list in reverse order

Hint:

Use split() to create list of each line

Create another list to store the list in reverse order.

Traverse the first list using for loop from end of this list

Append each list item into new list

"""

# create empty list

 

# Use for loop and .split() to append each line of .txt file into

# the list

 

# create another empty list to store reverse of this list

 

# create variables to store the variables such as length of

# your list using function len()

 

# traverse the list using for loop in reverse order using length

# of the listappend each element of the list to new list.

# Decrement variable

 

# at the end, return the list

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

 

def reverse_file(f): # created an empty list to store lines in the original file. my_list = [] reverse_list = [] # created an empty list for final answer. file = open(f, 'r') # opened the file using open function. # reading the contents of the file using .read() function. Storing it as a string. content = file.read() # spliting the contents of the file on the basis of "\n" to create list of each line my_list = content.split('\n') #variable to store list length length=len(my_list) # running a for loop and reading the contents of the splitted list in reverse order. for i in range(length-1,-1,-1): reverse_list.append(my_list[i]) # closing the opened file, to flush any unwritten information and close the file object. file.close() return reverse_list # returning the reversed list, as expected. # calling the defined function and storing it into a list called "result_list". result_list = reverse_file("alkaline.txt") # printing the elements of the returned list. for i in result_list: print(i)

Please see the attached file for the complete solution