Solution: Largest Number
Contents
Solution: Largest Number#
Write functions that returns:
the largest number of 3;
the largest in a list of any size.
Function definition: The largest in a list#
Complete the function bellow so that it returns the largest
number in the list numbers
.
def get_largest(x, y, z):
# use the x value as largest value
largest = x
# if y is large: replace the largest value by y
if largest < y:
largest = y
# if z is large: replace the largest value by z
if largest < z:
largest = z
# return the largest value
return largest
Testing: The largest number of 3#
Check if your function returns the expected value using the cell below.
import unittest
class UnitTests(unittest.TestCase):
def test_first(self):
self.assertEqual(get_largest(100, 1, 2), 100, 'The function should return 100')
def test_second(self):
self.assertEqual(get_largest(1, 100, 2), 100, 'The function should return 100')
def test_third(self):
self.assertEqual(get_largest(1, 2, 100), 100, 'The function should return 100')
unittest.main(argv=[''], verbosity=2,exit=False)
test_first (__main__.UnitTests) ...
ok
test_second (__main__.UnitTests) ...
ok
test_third (__main__.UnitTests) ...
ok
----------------------------------------------------------------------
Ran 3 tests in 0.002s
OK
<unittest.main.TestProgram at 0x7f9cc0502dd0>
Function definition: The largest in a list#
Complete the function bellow so that it returns the largest
number in the list numbers
.
def get_largest_list(numbers):
# using the first number of the list value as largest value
largest = numbers[0]
# iterate over all numbers
for number in numbers:
# if number is large: replace the largest value by number
if largest < number:
largest = number
# return the largest value
return largest
Testing: The largest in a list#
Check if your function returns the expected value using the cell below.
Tip: The numbers
list bellow is defined using a short syntax known as List Comprehension.
import unittest
class UnitTests(unittest.TestCase):
def test_type(self):
self.assertEqual(type(get_largest_list([1,2,3,4,5,100])), type(100), 'The function should return an integer.')
def test_position0(self):
x = [100,1,2,3,4,5,6,7,8,9]
self.assertEqual(get_largest_list(x), 100, 'The function should return 100')
def test_position3(self):
x = [0,1,2,100,4,5,6,7,8,9]
self.assertEqual(get_largest_list(x), 100, 'The function should return 100')
def test_position6(self):
x = [0,1,2,3,4,5,100,7,8,9]
self.assertEqual(get_largest_list(x), 100, 'The function should return 100')
def test_position9(self):
x = [0,1,2,3,4,5,6,7,8,100]
self.assertEqual(get_largest_list(x), 100, 'The function should return 100')
unittest.main(argv=[''], verbosity=2,exit=False)
test_position0 (__main__.UnitTests) ...
ok
test_position3 (__main__.UnitTests) ...
ok
test_position6 (__main__.UnitTests) ...
ok
test_position9 (__main__.UnitTests) ...
ok
test_type (__main__.UnitTests) ...
ok
----------------------------------------------------------------------
Ran 5 tests in 0.003s
OK
<unittest.main.TestProgram at 0x7f9cc046dbd0>