diff --git a/first_largest.py b/first_largest.py index db28c39..54152d6 100644 --- a/first_largest.py +++ b/first_largest.py @@ -2,14 +2,29 @@ Python program to find the largest element and its location. """ -def largest_element(a): +def largest_element(a, loc = False): """ Return the largest element of a sequence a. """ - - return None + try: + maxval = a[0] + location = 0 + for (i,e) in enumerate(a): + if e > maxval: + maxval = e + location = i + if loc == True: + return maxval, location + else: + return maxval + except ValueError: + return "Value Error" + except TypeError: + return "Type error, my dude. You can't compare those." + except: + return "Unforseen error! What did you do?" if __name__ == "__main__": - a = [1,2,3,2,1] - print("Largest element is {:}".format(largest_element(a))) + a = ["a","b","c",2,1] + print("Largest element is {:}".format(largest_element(a, loc=True))) diff --git a/tests.py b/tests.py index 2813679..ae2671a 100644 --- a/tests.py +++ b/tests.py @@ -5,11 +5,15 @@ class FirstLargest(unittest.TestCase): def test_largest_is_first(self): - given = [1,2,3,2,1] - expect = 3 + given = [5,3,4,2,1] + expect = 5 got = largest_element(given) self.assertEqual(got, expect) - + def test_largest_is_first(self): + given = [5,3,4,2,1] + expect = 5, 0 + got = largest_element(given, loc=True) + self.assertEqual(got, expect) """ ADD MORE """ if __name__ == '__main__':