简体   繁体   中英

Af unction to test if an input positive integer is equal to the sum of 2 or more integers

I'm super confused about this code I have to write. I must use a function to see if an inputted positive integer is equal to the sum of 2 or more positive integers, printing a list of those values if true and printing false if not. THE INTEGERS THAT ARE EQUAL TO THE INPUTED INTEGER HAVE TO BE ODD.

Since there are no restrictions on how many positive integers there are, or where they come from, the easiest solution is to just express the answer as a sum of 1s, which you can get by multiplying the list [1] by the input:

>>> def is_greater_than_one(n):
...     if n <= 1:
...         print(False)
...     else:
...         print([1] * n)
...
>>> is_greater_than_one(1)
False
>>> is_greater_than_one(2)
[1, 1]
>>> is_greater_than_one(10)
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

One way might be to put one pointer at index 0 pointing to '1' and other at last index pointing to n-1 (n being inputted number) . Then add the summation to the list if the output is correct . Increment the first pointer , decrement the other work the loop till both pointers point to same number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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