# # function_examples.py # # Example functions from lecture # # Computer Science 111 # def triple(x): """ Returns the triple of the input x. """ return 3*x def undo(s): """ Adds the prefix 'un' to the input s. """ return 'un' + s def redo(s): """ Adds the prefix 're' to the input s. """ return 're' + s def circle_area(diam): """ Computes the area of a circle with a diameter diam. """ radius = diam / 2 area = 3.14159 * (radius**2) return area def rect_perim(l, w): """ Computes the perimeter of a rectangle with length l and width w. """ return 2*l + 2*w def avg_first_last(values): """ Takes a list of numbers with at least one element and returns the average of the first and last elements. """ first = values[0] last = values[-1] return (first + last) / 2 def abs_value(x): """ Returns the absolute value of the iput x """ if x < 0: x = -1 * x return x def pass_fail(avg): """ Takes a numeric average avg and returns a string that indicates if the average is passing or failing. """ if avg >= 60: grade = 'pass' else: grade = 'fail' return grade def letter_grade(avg): """ Takes a numeric average avg and returns a string that indicates the letter grade corresponding to that average. """ if avg >= 90: grade = 'A' elif avg >= 80: grade = 'B' elif avg >= 70: grade = 'C' elif avg >= 60: grade = 'D' else: grade = 'F' return grade