Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions first_largest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
10 changes: 7 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down