0

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:

  1. test_maximum_number_one Failure in line 11, in test_maximum_number_one self.assertEqual(result, 78, msg="Incorrect number") AssertionError: Incorrect number

  2. 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

Jan Bluemink
  • 3,467
  • 1
  • 21
  • 35
jacob_makau
  • 21
  • 1
  • 4
  • 1
    so for the first one you just want the max value of the list. Am I right? – Gaetan Jul 20 '16 at 13:22
  • Do you understand what the error means and how the ```TestCase``` works? If you put some print statements in your loop, you should be able to see what is going on. – wwii Jul 20 '16 at 13:26
  • For what purpose you add `while` loop? Why not do everything on `for`? – Arnial Jul 20 '16 at 13:41
  • @Stages I want to 1. Get the max item in the list and 2. Check if this max item is the last item in the list, and if it is not the last item, then go on looping to end of list regardless of having already found the max item. – jacob_makau Jul 21 '16 at 13:30

3 Answers3

0

The keyword argument list1 is retained in every function call, and is modified. You could make a copy of that list inside the function and work on the copy:

def get_algorithm_result(list_1=[1, 78, 34, 12, 10, 3]):
    working_list = working_list.copy() if working_list else []
    max_index = len(working_list) - 1

    for i in working_list:
        max_num = i
        while max_num is i:
            if working_list[working_list.index(i) + 1] > max_num:
                working_list[working_list.index(i) + 1] = max_num

                if working_list.index(i) + 1 is max_index:
                    return max_num

            else:
                return max_num
        break
DurgaDatta
  • 3,952
  • 4
  • 27
  • 34
0

for will take all your list elements one by one :
simply do this for the first one :

def get_algorithm_result(list1=[1, 78, 34, 12, 10, 3]):
max_index = 0
index = 0
max_num = list1[0]

for i in list1:
    if i > max_num:
        max_num = i
        max_index = index
    index += 1     
return max_num

Check that for the second one.

Community
  • 1
  • 1
Gaetan
  • 325
  • 1
  • 15
0

I'm not sure why you're updateing the original list of numbers as that is not what the description you give calls for. Is this what you're after :

# 1. Get a list of numbers L1, L2, L3....LN as argument
def get_algorithm_result(list1=[1, 78, 34, 12, 10, 3]):
    # 2. Assume L1 is the largest, Largest = L1
    largest = list1[0]
    # 3. Take next number Li from the list and do the following
    for item in list1[1:]
        # 4. If Largest is less than Li
        if largest < item:
            # 5. Largest = Li
            largest = item
    # 6. If Li is last number from the list then (loop will have ended)
    # 7. return Largest and come out
    # 8. Else repeat same process starting from step 3 (next iteration of loop)
    return largest
R.Sharp
  • 296
  • 1
  • 8
  • I have tried to work with the solutions above and modified the program to this: – jacob_makau Jul 21 '16 at 13:43
  • list1 = [1, 2, 34, 12, 10, 78] def get_algorithm_result(*list1): max_num = list1[0] for i in range(1, len(list1)): if list1[i] > max_num: max_num = list1[i] return max_num 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 I still have the same error message as initially. I am wondering if there is an explicit way to check if item i in for loop is the last list item. – jacob_makau Jul 21 '16 at 13:45