简体   繁体   English

Function 必须返回大于等于的最小偶数的列表,按升序排列

[英]Function must return a list of the smallest even integers that are greater than or equal to, sorted into ascending order

Python: List of Even Integers, implement a function that: Python:偶数列表,实现一个 function :

  1. Is named even甚至被命名为
  2. takes 2 Integer arguments, start, and n.需要 2 Integer arguments,开始和 n。
  3. returns a list of n smallest even integers greater than or equal to start in ascending order.按升序返回大于等于 start 的 n 个最小偶数的列表。

Implementation of the function will be tested by a provided code stub on several input files. function 的实现将通过提供的代码存根在几个输入文件上进行测试。 Each input file contains parameters for the function call.每个输入文件都包含 function 调用的参数。 The function will be called with those parameters, and the result of its execution will be printed to the standard output by the provided code.将使用这些参数调用 function,其执行结果将通过提供的代码打印到标准 output。

Constraints约束

• 1 <= start, n <= 100 • 1 <= 开始,n <= 100

Input Format Format for Custom Testing自定义测试的输入格式格式

In the first and only line, there are two space separated integers, start, and n.在第一行也是唯一的一行中,有两个空格分隔的整数 start 和 n。

Sample Case 0示例案例 0

Sample Input样本输入

STDIN Function 2 4 start = 2, n = 4 STDIN Function 2 4 开始 = 2,n = 4

Sample Output样品 Output

2 4 6 8 2 4 6 8

Explanation The function must return a list of the 4 smallest even integers that are greater than or equal to 2, sorted into ascending order: 2,4,6, and 8.说明 function 必须返回大于等于 2 的 4 个最小偶数的列表,按升序排列:2、4、6、8。

Below is my code so far, but it only prints the even number within the range of the list not the length which is mentioned in the list:-到目前为止,下面是我的代码,但它只打印列表范围内的偶数,而不是列表中提到的长度:-


def even(start, n):
    count = []

    for i in range(start, n):
        if 1 <= start and n <= 100:
            if i % 2 == 0:
                count.append(i)
    return [count]

start = int(input("Enter the number where the even should start from:  "))
n = int(input("Enter the value of length you want to show the even numbers of: "))
print(even(start, n))

I believe you're running into trouble when in the range declaration.我相信您在范围声明中遇到了麻烦。

for i in range(start, n):

This could probably be rewritten to range(start, start + n) .这可能会被重写为range(start, start + n) With some changes to your loop logic, this reasoning will get you there.通过对循环逻辑进行一些更改,这种推理将使您到达那里。

A different approach to this problem could be to determine whether of not the start val is even or not and then adding even numbers to it.解决此问题的另一种方法可能是确定起始值是否为偶数,然后向其添加偶数。 In the solution below, the meaning of length is more clearly represented.在下面的解决方案中,长度的含义更清楚地表示了。

def even(start, n): 
    count = []
        
    # Check if start is even
    if start % 2 != 0:
        start = start + 1 
        
    # A set of even numbers can be thought of the set of all numbers multiplied by 2                                                                                                                                                                                                                                           
    for i in range(0, n): 
        count.append(start + i*2)   
    return count

start = int(input("Enter the number where the even should start from:  "))
n = int(input("Enter the value of length you want to show the even numbers of: "))
print(even(start, n))

Your code is mostly wrong.您的代码大多是错误的。 Do it like this:像这样做:

def even(start, n):
    if start % 2:
        start += 1 # If odd, start from the next even
    return [x for x in range(start, start+2*n+1, 2)]

def even(start, n):
    if start % 2:
        start += 1 # If odd, start from the next even
    return [x for x in range(start, start + 2 * n, 2)]

start = int(input("Enter the number where the even should start from:  "))
n = int(input("Enter the value of length you want to show the even numbers of: "))
print(even(start, n))

Your code is almost ok.你的代码几乎没问题。 You only need a few changes in this part.您只需要在这部分进行一些更改。

for i in range(start, start+2*n):
    
        if i % 2 == 0:
            count.append(i)
return count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM