Question: how do u solve this problem? 1 Hrite a function called first_to last. first_to_last should 2 #take as input a string representing a filename. Inside
how do u solve this problem?


1 Hrite a function called first_to last. first_to_last should 2 \#take as input a string representing a filename. Inside the 3 \#file will be some text on each line; some lines will contain 4. Hintegers, while others will contain strings of other 5 \#characters. 6 \# 7 \#first to last should return a tuple containing the first and 8 \#last strings in the file alphabetically. It should ignore any 9 \#lines that contain integers. 10 \# For example, in the dropdown in the top left you' 11 find a 12 \#file named firstlastinput, txt. Ignoring numerals, the first 13 Hstring alphabetically is appsp, and the last string 17 \#vou may assume that every line in the file contains either 19 \# ith both numerals and letters, nor any capital letters. def first_to last(filename): first = None last = None file = open ( filename, r ) lines = file.readlines() for line in lines: FindFirstL ast.py * Read all lines of tile to lines (None, None) lines = file.readlines() \# For each line in file for line in lines: \# Remove all the ending newline character line = line. strip ( 'ln') \# If the line is an alphabet if line. isalph(1): \# If the value of first is not None then check if line is alphabetically \# less than first. Else, assign line to first if first l= None: if line = line \# If the value of last is not None and last is alphabetically less than * line. Then, last is set to line if last != None: if last line: last = line else: last = line \# Return the first and last as tuple return first, last \#Below are some lines of code that will test your function. 57 \#you can change the value of the variable(s) to test your 58 \#function with different inputs. 59 60 \#If your function works correctly, this will originally 61 \#print: ("appsp", "zZffs") 62 print(first_to_last("FirstlastInput.txt"))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
