Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finding Function Names Write a function called function.names that takes as a pa

ID: 3598403 • Letter: F

Question

Finding Function Names Write a function called function.names that takes as a parameter a file handle open for reading, and returns a list of all of the function names in that file. Remember that function definitions have a very specific format: def function name (parameters). You can assume that all functions are exactly correctly formatted according to PEP-8 standards. e.g., 1 space after the def, no space before the Calling function names on ex4.py would (hopefully) return the result: C'insert, 'up.to.first', 'cutlist' Hint: look through the str methods, some of them will be quite helpful, such as startswith or find Justified Write a function called justified that takes as a parameter a file handle open for reading, and returns a boolean which is true if and only if every line in that file is left-justified (the first character is something other than a space). If any lines start with a space, the program should return False. Challenge: Ensure that your code works efficiently on a very long file with one of the first lines being non-left-justified Bonus: Section Average Write a function called section average3 that takes two parameters: The first parameter is an open file of midterm marks (no, they're not real marks). Each line represents a single student and consists of a student number, a name, a section code and a midterm grade, all separated by whitespace. An example file has been uploaded as ex5-grade.file.txt. The second parameter is a section code. Return the average midterm mark for all students in that section, or return None if the section code does not appear in the marks file for any students. As before, this will be run through the auto-marker, but the marks won't count. It's just for your own enjoyment. Hint: the split method might be particularly useful here. Hint: Notice that not everyone has the same number of names, so we can't just assume that the mark and section code will be at a particular index... at least not reading from the left

Explanation / Answer

def function_name(parameters):

      func_names = []

      for line in parameters:

            if line.startswith("def"):

                  temp = line.split()

                  end = temp[1].index("(")

                  name = temp[1][:end]

                  func_names.append(name)

      return func_names

def justified(parameters):

      value = True

      for index,line in enumerate(parameters):

            if index > 0 and (line.startswith(" ") or line.startswith(" ")):

                  value = False

      return value

def section_average(file,section_code):

      total_marks = 0

      total_students = 0

      avg_marks = 0

      value = None

      for line in file:

            if section_code in line:

                  temp = line.split()

                  total_marks += int(temp[-1])

                  total_students += 1

      try:

            avg_marks = total_marks/total_students

      except:

            pass

      else:

            value = avg_marks

     

      return value

     

fileName = "test.txt" #full path to the file

try:

      with open(fileName,'r') as f: #open file in read mode

            result = section_average(f,'A') #change function and parameters accordingly

      print (result)

except Exception as e:

      print (e)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote