【问题标题】:Writing a procedure in python that finds primes in a list, and stores them in an empty list用python编写一个过程,在列表中找到素数,并将它们存储在一个空列表中
【发布时间】:2020-10-03 18:09:14
【问题描述】:

我目前在课堂上做这项作业时遇到了一些问题:

“编写一个程序 primes_only(input_list,prime_list),它接受两个列表作为参数,一个输入数字列表和一个名为 prime_list 的空列表。该过程必须检查列表 input_list 中的每个数字。如果该数字是素数,它应该将该数字的副本添加到素数列表中。您必须使用之前编写的函数来为您完成大部分工作。"

这是我到目前为止尝试做的事情:

input_list = int(input())
prime_list = []

def primes_only(input_list,prime_list):
    for x in range(2,input_list):
        if(input_list%x)==0:
            prime_list.append(x)
        else:
print(prime_list)

    def is_prime(n):
    if (n==1):
        return False
    elif (n==2):
        return True;
    else:
        for x in range(2,n):
            if(n % x==0):
                return False
        return True    

有人可以帮忙吗?

【问题讨论】:

  • 你有什么问题?请提出一个具体问题。 “有人能帮我写这个程序吗”对于 Stack Overflow 来说是题外话

标签: python python-3.x list primes


【解决方案1】:

我不知道你的教学大纲中有什么,你可以使用什么,你不能使用什么, 所以我写了一个非常简单的。 我已经使用 cmets(语句后跟 # 符号)描述了我在程序中所做的事情。 而且我保持 is_prime 函数不变。(尽管您可以像 Javier 所说的那样以更花哨的方式来做) 另外,作为输入给出的数字必须用逗号分隔,并且必须一次输入。

代码如下:

def is_prime(n):
    if (n==1):
        return False
    elif (n==2):
        return True;
    else:
        for x in range(2,n):
            if(n % x==0):
                return False
        return True

def primes_only(input_list, prime_list):
    # Iterates over each number and checks whether the
    # number is prime or not, if so then it is appended
    # to the prime list
    for n in input_list:
        # Check if the number is prime and whether it
        # has already been entered into the list.
        # This checks that the same element is not appended
        # twice
        if is_prime(n) and prime_list.count(n) == 0:
            prime_list.append(n)

# Take input from the user with numbers separated by commas
# and split the input into numbers in the form of a string 
input_list = input("Enter the elments->").split(",")

# Convert the numbers from string to int
for i in range(len(input_list)):
    input_list[i] = int(input_list[i])

prime_list = []
primes_only(input_list, prime_list)
print(prime_list)

示例输入和输出:

Enter the elments->1,5,3,2,4,7,3,19
[5, 3, 2, 7, 19]

【讨论】:

    【解决方案2】:

    问题的描述表明同时发生了两件事:

    1. 检测数字是否为素数。
    2. 将号码复制到另一个列表中。

    在这种情况下,我会编写两个函数:is_prime 和您的 primes_only

    def is_prime(n: int) -> bool:
        """Returns whether the given number is prime."""
        assert n > 1, 'The number must be greater than 1'
    
        for i in range(2, n):  # Could use square root of n as the upper bound.
            if n % i == 0:
                return False
        return True
    

    这个函数应该很容易在 REPL 上手动测试,或者更好的是,创建一些测试用例。

    然后:

    def primes_only(nums: Iterable[int], primes: List[int]) -> None:
        for n in nums:
            if is_prime(n):
                primes.append(n)
    

    更高级的版本:

    def primes_only(nums: Iterable[int], primes: List[int]) -> None:
        primes.extend(n for n in nums if is_prime(n))
    

    (我没有测试代码)

    【讨论】:

    • 格式让我很困惑。你实际上也写了这两个函数。干得好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2019-07-28
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多