From 383121d962da318c4a425edf1ee235b329268bd3 Mon Sep 17 00:00:00 2001 From: Benjamin Weinhold Date: Wed, 22 Sep 2021 12:21:18 -0500 Subject: [PATCH 1/3] updated test and first largest --- first_largest.py | 16 ++++++++++++---- tests.py | 10 +++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/first_largest.py b/first_largest.py index db28c39..6ac9707 100644 --- a/first_largest.py +++ b/first_largest.py @@ -2,14 +2,22 @@ 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 + maxval = a[0] + location = 0 + for i in range(1,len(a)): + if a[i] > maxval: + maxval = a[i] + location = i + if loc == True: + return maxval, location + else: + return maxval if __name__ == "__main__": a = [1,2,3,2,1] - print("Largest element is {:}".format(largest_element(a))) + 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__': From e987369a3d9114cbf6753dd9f82bf98c264e93bd Mon Sep 17 00:00:00 2001 From: Benjamin Weinhold Date: Wed, 22 Sep 2021 14:46:02 -0500 Subject: [PATCH 2/3] used enumerate --- first_largest.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/first_largest.py b/first_largest.py index 6ac9707..99d9408 100644 --- a/first_largest.py +++ b/first_largest.py @@ -7,9 +7,13 @@ def largest_element(a, loc = False): """ maxval = a[0] location = 0 - for i in range(1,len(a)): - if a[i] > maxval: - maxval = a[i] + # for i in range(1,len(a)): + # if a[i] > maxval: + # maxval = a[i] + # location = i + for (i,e) in enumerate(a): + if e > maxval: + maxval = e location = i if loc == True: return maxval, location From 8ab59154f46dc9acc7638fbb7d3eb5824d11655a Mon Sep 17 00:00:00 2001 From: Benjamin Weinhold Date: Fri, 24 Sep 2021 12:16:45 -0500 Subject: [PATCH 3/3] added exception error handling --- first_largest.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/first_largest.py b/first_largest.py index 99d9408..54152d6 100644 --- a/first_largest.py +++ b/first_largest.py @@ -5,23 +5,26 @@ def largest_element(a, loc = False): """ Return the largest element of a sequence a. """ - maxval = a[0] - location = 0 - # for i in range(1,len(a)): - # if a[i] > maxval: - # maxval = a[i] - # location = i - for (i,e) in enumerate(a): - if e > maxval: - maxval = e - location = i - if loc == True: - return maxval, location - else: - return maxval + 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] + a = ["a","b","c",2,1] print("Largest element is {:}".format(largest_element(a, loc=True)))