迭代通过作为python函数中的参数传递的列表

时间:2016-07-20 12:58:57

标签: python

我无法创建正确的算法,正确的代码应符合单元测试中的规范,如下所示:

创建一个函数get_algorithm_result来实现下面的算法
1.获取数字列表L1,L2,L3 .... LN作为参数
2.假设L1是最大的,最大= L1
3.从列表中取下一个号码Li并执行以下操作 4.如果最大值小于李
5.最大=李
6.如果李是列表中的最后一个号码,那么
7.返回最大并出来
8.否则从步骤3开始重复相同的过程

创建一个函数prime_number,执行以下操作 •将参数作为整数和
•如果值为prime或
,则返回布尔值true •如果值不是素数,则返回布尔值false

单元测试是:

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")

我已经尝试了所有我能做到的......

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

我的错误报告是:

  1. test_maximum_number_one 第11行中的失败,在test_maximum_number_one中self.assertEqual(结果,78,msg =“不正确的数字”)AssertionError:数字不正确

  2. test_maximum_number_two 第15行中的失败,在test_maximum_number_two self.assertEqual(结果,“zoo”,msg =“不正确的值”)AssertionError:不正确的值

  3. 此处有人,请帮帮忙。

    由于

4 个答案:

答案 0 :(得分:0)

关键字参数list1在每个函数调用中保留,并被修改。您可以在函数中复制该列表并处理副本:

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

答案 1 :(得分:0)

for将逐个获取所有列表元素:
只需为第一个执行此操作:

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

检查that第二个。

答案 2 :(得分:0)

我不确定您为什么要更新原始的号码列表,因为这不是您要求的说明。这是你之后的事情:

# 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

答案 3 :(得分:0)

我已将代码修改为此,但返回了相同的错误消息。 是否有明确的方法来检查当前循环中的项目是否是最后一个列表项?

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