I'm having trouble creating the correct algo, the correct code should meet the specs in the unit test, as follows:
Create a function get_algorithm_result to implement the algorithm below
1. Get a list of numbers L1, L2, L3....LN as argument
2. Assume L1 is the largest, Largest = L1
3. Take next number Li from the list and do the following
4. If Largest is less than Li
5. Largest = Li
6. If Li is last number from the list then
7. return Largest and come out
8. Else repeat same process starting from step 3
Create a function prime_number that does the following
• Takes as parameter an integer and
• Returns boolean value true if the value is prime or
• Returns boolean value false if the value is not prime
The unit test is:
import unittest
class AlgorithmTestCases(unittest.TestCase):
def test_maximum_number_one(self):
result = get_algorithm_result([1, 78, 34, 12, 10, 3])
self.assertEqual(result, 78, msg="Incorrect number")
def test_maximum_number_two(self):
result = get_algorithm_result(["apples", "oranges", "mangoes", "banana", "zoo"])
self.assertEqual(result, "zoo", msg="Incorrect value")
def test_prime_number_one(self):
result = prime_number(1)
self.assertEqual(result, False, msg="Result is invalid")
def test_prime_number_two(self):
result = prime_number(78)
self.assertEqual(result, False, msg="Result is invalid")
def test_prime_number_three(self):
result = prime_number(11)
self.assertEqual(result, True, msg="Result is invalid")
I have tried all I can by coming up with this...
def get_algorithm_result(list1=[1, 78, 34, 12, 10, 3]):
max_index = len(list1) - 1
for i in list1:
max_num = i
while max_num is i:
if list1[list1.index(i) + 1] > max_num:
list1[list1.index(i) + 1] = max_num
if list1.index(i) + 1 is max_index:
return max_num
else:
return max_num
break
def prime_number(x):
if x > 1:
for i in range(2, x + 1):
if x % i == 0 and i != x and i != 1:
return False
else:
return True
else:
return False
My error report is:
test_maximum_number_one Failure in line 11, in test_maximum_number_one self.assertEqual(result, 78, msg="Incorrect number") AssertionError: Incorrect number
test_maximum_number_two Failure in line 15, in test_maximum_number_two self.assertEqual(result, "zoo", msg="Incorrect value") AssertionError: Incorrect value
Anyone here, please help out.
Thanks