counting odd numbers in a list python - python

This is a part of my homework assignment and im close to the final answer but not quite yet. I need to write a function that counts odd numbers in a list.
Create a recursive function count_odd(l) which takes as its only argument a list of integers. The function will return a count of the number of list elements that are odd, i.e., not evenly divisible by 2.\
>>> print count_odd([])
0
>>> print count_odd([1, 3, 5])
3
>>> print count_odd([2, 4, 6])
0
>>> print count_odd([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])
8
Here is what i have so far:
#- recursive function count_odd -#
def count_odd(l):
"""returns a count of the odd integers in l.
PRE: l is a list of integers.
POST: l is unchanged."""
count_odd=0
while count_odd<len(l):
if l[count_odd]%2==0:
count_odd=count_odd
else:
l[count_odd]%2!=0
count_odd=count_odd+1
return count_odd
#- test harness
print count_odd([])
print count_odd([1, 3, 5])
print count_odd([2, 4, 6])
print count_odd([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144])
Can u help explain what im missing. The first two test harness works fine but i cant get the final two. Thanks!

Since this is homework, consider this pseudo-code that just counts a list:
function count (LIST)
if LIST has more items
// recursive case.
// Add one for the current item we are counting,
// and call count() again to process the *remaining* items.
remaining = everything in LIST except the first item
return 1 + count(remaining)
else
// base case -- what "ends" the recursion
// If an item is removed each time, the list will eventually be empty.
return 0
This is very similar to what the homework is asking for, but it needs to be translate to Python and you must work out the correct recursive case logic.
Happy coding.

def count_odd(L):
return (L[0]%2) + count_odd(L[1:]) if L else 0

Are slices ok? Doesn't feel recursive to me, but I guess the whole thing is kind of against usual idioms (i.e. - recursion of this sort in Python):
def countOdd(l):
if l == list(): return 0 # base case, empty list means we're done
return l[0] % 2 + countOdd(l[1:]) # add 1 (or don't) depending on odd/even of element 0. recurse on the rest
x%2 is 1 for odds, 0 for evens. If you are uncomfortable with it or just don't understand it, use the following in place of the last line above:
thisElement = l[0]
restOfList = l[1:]
if thisElement % 2 == 0: currentElementOdd = 0
else: currentElementOdd = 1
return currentElementOdd + countOdd(restOfList)
PS - this is pretty recursive, see what your teacher says if you turn this in =P
>>> def countOdd(l):
... return fold(lambda x,y: x+(y&1),l,0)
...
>>> def fold(f,l,a):
... if l == list(): return a
... return fold(f,l[1:],f(a,l[0]))

All of the prior answers are subdividing the problem into subproblems of size 1 and size n-1. Several people noted that the recursive stack might easily blow out. This solution should keep the recursive stack size at O(log n):
def count_odd(series):
l = len(series) >> 1
if l < 1:
return series[0] & 1 if series else 0
else:
return count_odd(series[:l]) + count_odd(series[l:])

The goal of recursion is to divide the problem into smaller pieces, and apply the solution to the smaller pieces. In this case, we can check if the first number of the list (l[0]) is odd, then call the function again (this is the "recursion") with the rest of the list (l[1:]), adding our current result to the result of the recursion.

def count_odd(series):
if not series:
return 0
else:
left, right = series[0], series[1:]
return count_odd(right) + (1 if (left & 1) else 0)

Tail recursion
def count_odd(integers):
def iter_(lst, count):
return iter_(rest(lst), count + is_odd(first(lst))) if lst else count
return iter_(integers, 0)
def is_odd(integer):
"""Whether the `integer` is odd."""
return integer % 2 != 0 # or `return integer & 1`
def first(lst):
"""Get the first element from the `lst` list.
Return `None` if there are no elements.
"""
return lst[0] if lst else None
def rest(lst):
"""Return `lst` list without the first element."""
return lst[1:]
There is no tail-call optimization in Python, so the above version is purely educational.
The call could be visualize as:
count_odd([1,2,3]) # returns
iter_([1,2,3], 0) # could be replaced by; depth=1
iter_([2,3], 0 + is_odd(1)) if [1,2,3] else 0 # `bool([1,2,3])` is True in Python
iter_([2,3], 0 + True) # `True == 1` in Python
iter_([2,3], 1) # depth=2
iter_([3], 1 + is_odd(2)) if [2,3] else 1
iter_([3], 1 + False) # `False == 0` in Python
iter_([3], 1) # depth=3
iter_([], 1 + is_odd(3)) if [3] else 1
iter_([], 2) # depth=4
iter_(rest([]), 2 + is_odd(first([])) if [] else 2 # bool([]) is False in Python
2 # the answer
Simple trampolining
To avoid 'max recursion depth exceeded' errors for large arrays all tail calls in recursive functions can be wrapped in lambda: expressions; and special trampoline() function can be used to unwrap such expressions. It effectively converts recursion into iterating over a simple loop:
import functools
def trampoline(function):
"""Resolve delayed calls."""
#functools.wraps(function)
def wrapper(*args):
f = function(*args)
while callable(f):
f = f()
return f
return wrapper
def iter_(lst, count):
#NOTE: added `lambda:` before the tail call
return (lambda:iter_(rest(lst), count+is_odd(first(lst)))) if lst else count
#trampoline
def count_odd(integers):
return iter_(integers, 0)
Example:
count_odd([1,2,3])
iter_([1,2,3], 0) # returns callable
lambda:iter_(rest(lst), count+is_odd(first(lst))) # f = f()
iter_([2,3], 0+is_odd(1)) # returns callable
lambda:iter_(rest(lst), count+is_odd(first(lst))) # f = f()
iter_([3], 1+is_odd(2)) # returns callable
lambda:iter_(rest(lst), count+is_odd(first(lst))) # f = f()
iter_([], 1+is_odd(3))
2 # callable(2) is False

I would write it like this:
def countOddNumbers(numbers):
sum = 0
for num in numbers:
if num%2!=0:
sum += numbers.count(num)
return sum

not sure if i got your question , but as above something similar:
def countOddNumbers(numbers):
count=0
for i in numbers:
if i%2!=0:
count+=1
return count

Generator can give quick result in one line code:
sum((x%2 for x in nums))

Related

Write a program to determine if a given list possesses an additive sequence

I am an undergraduate student who love programming. I had met a problem today which confuses me a lot.
Write a program to determine if a given list possesses an additive
sequence. That is, a sequence of 3 consecutive elements where the
first two elements sum to the third. Return the sum value. Or return
None if there are no such sums.
Example 1:
Input: [0,1,2,3,5,8,13]
Output: 3, since 1+2 = 3. Note that 8 is also the sum of the two previous values, but it occurs after 3
Example 2:
Input: [1,2,4,5,6]
Output: None. Since no consecutive values sum to a third consecutive value.
My answer is:
def sequence_sum(my_list):
for x in range(0, len(my_list)):
if (my_list[x] + my_list[x+1]) != my_list[x+2]:
return None
else:
return my_list[x+2]
The test results read as:
Traceback (most recent call last):
File "/grade/run/test.py", line 30, in test_larger
self.assertEqual(sequence_sum(my_arr), 1)
AssertionError: None != 1
As well as:
Traceback (most recent call last):
File "/grade/run/test.py", line 35, in test_another
self.assertEqual(sequence_sum([0,3,4,5,9]), 9)
AssertionError: None != 9
for x in range(0, len(my_list)):
if (my_list[x] + my_list[x+1]) != my_list[x+2]:
return None
else:
return my_list[x+2]
It is a mistake to have both branches return because it means either way the function will return in the very first iteration of the loop. If you find a match it is correct to return straightaway. But when you don't find a match, you can't declare failure. There may be a match later. The return None needs to be delayed until the entire list has been searched.
for x in range(0, len(my_list)):
if my_list[x] + my_list[x+1] == my_list[x+2]:
return my_list[x+2]
return None
That'll fix the immediate problem, but run it and you will now encounter index out of range exceptions. I'll leave it to you to debug that problem.
Seems to me that you're only checking the first portion of the list. return will exit the function no matter where it is so after checking if 0 + 3 equals 4 it returns None. Therefore never getting to the end to check for 9.
To fix this move your return to the end of the function and chance the comparator to ==.
for loop:
if ([list[x]+list[x+1]) == list[x+2]:
return list[x+2]
return None
for x in range(0, len(my_list) - 2):
if my_list[x] + my_list[x + 1] == my_list[x + 2]:
return my_list[x + 2]
return None
You are checking for all the indexes (0, 1, 2, 3...) until the second one before the last as to not trigger an index out of boundary exception.
There, you check if any of the two consecutive ones are a sum of the next element in the list.
If there is such pair, you return it. If there is not (after going through the whole list), you return None.
You can use deque to save two last elements:
from collections import deque
def func(l: list):
seq = deque(maxlen=2)
for num, i in enumerate(l):
if num > 1 and sum(seq) == i:
return i
else:
seq.append(i)
print(func([0, 1, 2, 3, 5, 8, 13]))
# 3
print(func([1,2,4,5,6]))
# None
Yet another way:
found_sums = [x for i, x in enumerate(data[2:])
if data[i] + data[i+1] == x]
first_value = next(iter(found_sums), None)
found_sums is a list comprehension that returns the values which are equal to the sum of the two preceding ones. first_value returns the first of these values or alternatively None, if the list is empty.
Test:
for data in ([0,1,2,3,5,8,13], [1,2,4,5,6]):
found_sums = [x for i, x in enumerate(data[2:]) if data[i] + data[i + 1] == x]
first_value = next(iter(found_sums), None)
print('Result for {}:\t{}'.format(data, first_value))
This returns:
Result for [0, 1, 2, 3, 5, 8, 13]: 3
Result for [1, 2, 4, 5, 6]: None

Need to combine two functions into one (Python)

Here is my code-
def Max(lst):
if len(lst) == 1:
return lst[0]
else:
m = Max(lst[1:])
if m > lst[0]:
return m
else:
return lst[0]
def Min(lst):
if len(lst) == 1:
return lst[0]
else:
m = Min(lst[1:])
if m < lst[0]:
return m
else:
return lst[0]
print("Max number:",Max([5,4,100,0,2]))
print("Min number:",Min([5,4,100,0,2]))
Basically I need a single function that returns both the largest and smallest number and it needs to be recursively. How would would I change this code?
Some types of recursive algorithms/implementations operating on a list input are very quite easy to come up with, if you know the "trick". That trick being:
Just assume you already have a function that can do what you want.
Wait, no, that doesn't really make sense, does it? Then we'd already be done.
Let's try that again:
Just assume you already have a function that can do what you want (but only for inputs 1 element smaller than you need).
There, much better. While a bit silly, that's an assumption we can work with.
So what do we want? In your example, it's returning the minimum and maximum elements of a list. Let's assume we want them returned as a 2-tuple (a.k.a. a "pair"):
lst = [5, 4, 100, 0, 2]
# Well, actually, we can only do this for a smaller list,
# as per our assumption above.
lst = lst[1:]
lst_min, lst_max = magic_min_max(lst) # I want a pony!
assert lst_min == 0 # Wishful thinking
assert lst_max == 100 # Wishful thinking
If we have such a magic function, can we use it to solve the problem for the actual input size? Let's try:
def real_min_max(lst):
candidate = lst[0]
rest_of_the_list = lst[1:]
min_of_rest, max_of_rest = magic_min_max(rest_of_the_list) # Allowed because
# smaller than lst
min_of_lst = candidate if candidate < min_of_rest else min_of_rest
max_of_lst = candidate if candidate > max_of_rest else max_of_rest
return min_of_lst, max_of_lst
Not exactly easy, but pretty straight forward, isn't it? But let's assume our magic function magic_min_max has an additional restriction: It cannot handle empty lists. (After all, an empty list doesn't have neither a minimum nor a maximum element. Not even magic can change that.)
So if lst has size 1, we must not call the magic function. No problem for us, though. That case is easy to detect and easy to circumvent. The single element is both minimum and maximum of its list, so we just return it twice:
def real_min_max(lst):
candidate = lst[0]
if len(lst) == 1:
return candidate, candidate # single element is both min & max
rest_of_the_list = lst[1:]
min_of_rest, max_of_rest = magic_min_max(rest_of_the_list) # Allowed because
# smaller than lst
# but (if we get
# here) not empty
min_of_lst = candidate if candidate < min_of_rest else min_of_rest
max_of_lst = candidate if candidate > max_of_rest else max_of_rest
return min_of_lst, max_of_lst
So that's that.
But wait ... there is no magic. If we want to call a function, it has to actually exist. So we need to implement a function that can return the minimum and maximum of a list, so we can call it in real_min_max instead of magic_min_max. As this is about recursion, you know the solution: real_min_max is that function (once it's fixed by calling a function that does exist) so we can have it call itself:
def real_min_max(lst):
candidate = lst[0]
if len(lst) == 1:
return candidate, candidate # single element is both min & max
rest_of_the_list = lst[1:]
min_of_rest, max_of_rest = real_min_max(rest_of_the_list) # No magic needed,
# just recursion!
min_of_lst = candidate if candidate < min_of_rest else min_of_rest
max_of_lst = candidate if candidate > max_of_rest else max_of_rest
return min_of_lst, max_of_lst
Let's try it:
lst = [5, 4, 100, 0, 2]
real_min_max(lst) # returns (0, 100)
It works!
import sys
class MaxMin:
max = -sys.maxint - 1
min = sys.maxint
def getMaxMin(self, lst, obj):
if len(lst) == 1:
obj.max = lst[0]
obj.min = lst[0]
else:
self.getMaxMin(lst[1:], obj)
if obj.max < lst[0]:
obj.max = lst[0]
if obj.min > lst[0]:
obj.min = lst[0]
obj = MaxMin()
obj.getMaxMin([5,4,100,0,2], obj)
print("Max number:",obj.max)
print("Min number:",obj.min)
That is the exact idea of higher order functions. You can add a compare parameter in your function, and pass lambda a, b: a>b for Min and lambda a, b: a < b for max. then, instead of m > lst[0], use compare(m, lst[0])

Any alternate to slice sling of integers?

I am attempting to make a recursive function that adds the two last numbers until there are none left. For example:
sumDigits(239)
would equate to:
2+3+9=14
It is difficult because the input must be an integer, which cannot be sliced without converting it. I decided to try to turn it into a lists because I thought the pop() method would be useful for this. It appears as though this approach is not working. Any suggestions?
EXECUTION:
>>> sumDigits('234')
9
>>> sumDigits('2343436432424')
8
>>>
CODE:
def sumDigits(n):
l1 = list(str(n))
if len(l1) > 1:
a = int(l1.pop())
b = int(l1.pop())
l1.append(str(a+b))
return sumDigits(int(''.join(l1)))
else:
return n
With functional tools like reduce() the problem is solved by
from functools import reduce
def sumDigits(n):
return reduce((lambda x, y: int(x) + int(y)), list(str(n)))
Instead of passing a string you should pass a list of integers:
def sumDigits(l1):
if len(l1) > 1:
a = l1.pop()
b = l1.pop()
l1.append(a+b)
return sumDigits(l1)
else:
return l1[0]
print sumDigits([2,3, 4])
print sumDigits([2, 3, 4, 3, 4, 3, 6, 4, 3, 2, 4, 2, 4])
The problem with your approach is that:
'23434364324|24|' -> '2343436432|46|' -> '2343436432 | 10',
here now pop will return 0 and 1, instead of 2 and 10 as you would've expected. Hence the wrong output.
Simple solution:
>>> s = '2343436432424'
>>> sum(int(x) for x in s)
44
Since everybody seems intent on solving your homework for you, here's the elegant recursive solution.
def sumDigits(n):
if n < 10:
return n
return n % 10 + sumDigits(n / 10)
EDIT
The simple solution is:
def sumDigits(n):
return sum(int(i) for i in str(n))
Upon your answer of my comment the below solution is not applicable.
def sumDigits(n):
n = [int(i) for i in str(n)]
return sumDigitsRec(n)
def sumDigitsRec(li):
if len(li) > 1:
li[-1] += li.pop()
return sumDigits(''.join(str(i) for i in li))
else:
return li[0]
As strings:
def sumDigits(n):
answer = 0
for num in n:
answer += int(num)
return answer
Without slicing, using only an integer input:
def sumDigits(n):
answer = 0
while n:
answer += n%10
n /= 10
return answer
If I understand your question correctly, you want to stop summing the digits when the sum is a 2 digit number. I think the bug in your program is that you need if len(l1) > 2: not if len(l1) > 1: to make sure you don't recurse when you have just 2 digits.
you can do it recursively in this way:
def sumDigits(n,s=0):
if len(n) == 0:
return s
else:
return sumDigits(n[:-1],s+int(n[-1]))
if you want simpler and pytonic way (not recursive) you can do this
>>> s = 2343436432424
>>> sum(map(int,str(s)))
44
All the solutions provided failed to meet the prereqs which were stated in the problem description. This is the correct answer:
Use **kwargs and exceptions to utilize holding variables in recursion function.
For example:
def recursionFunc(x, **kwargs):
try:
count = kwargs['count']
except:
count = 0
#Code down here to add num from x to count
#remove last index on every iteration from x
#etc
return recursionFunc(x, count = count)
It's working fine, as the following check will show:
>>> 2343436432424 % 9
8
If you didn't mean for it to be called recursively then just don't do so, i.e. stop checking the length and just return the sum.

Find the nth lucky number generated by a sieve in Python

I'm trying to make a program in Python which will generate the nth lucky number according to the lucky number sieve. I'm fairly new to Python so I don't know how to do all that much yet. So far I've figured out how to make a function which determines all lucky numbers below a specified number:
def lucky(number):
l = range(1, number + 1, 2)
i = 1
while i < len(l):
del l[l[i] - 1::l[i]]
i += 1
return l
Is there a way to modify this so that I can instead find the nth lucky number? I thought about increasing the specified number gradually until a list of the appropriate length to find the required lucky number was created, but that seems like a really inefficient way of doing it.
Edit: I came up with this, but is there a better way?
def lucky(number):
f = 2
n = number * f
while True:
l = range(1, n + 1, 2)
i = 1
while i < len(l):
del l[l[i] - 1::l[i]]
i += 1
if len(l) >= number:
return l[number - 1]
f += 1
n = number * f
I came up with this, but is there a better way?
Truth is, there will always be a better way, the remaining question being: is it good enough for your need?
One possible improvement would be to turn all this into a generator function. That way, you would only compute new values as they are consumed. I came up with this version, which I only validated up to about 60 terms:
import itertools
def _idx_after_removal(removed_indices, value):
for removed in removed_indices:
value -= value / removed
return value
def _should_be_excluded(removed_indices, value):
for j in range(len(removed_indices) - 1):
value_idx = _idx_after_removal(removed_indices[:j + 1], value)
if value_idx % removed_indices[j + 1] == 0:
return True
return False
def lucky():
yield 1
removed_indices = [2]
for i in itertools.count(3, 2):
if not _should_be_excluded(removed_indices, i):
yield i
removed_indices.append(i)
removed_indices = list(set(removed_indices))
removed_indices.sort()
If you want to extract for example the 100th term from this generator, you can use itertools nth recipe:
def nth(iterable, n, default=None):
"Returns the nth item or a default value"
return next(itertools.islice(iterable, n, None), default)
print nth(lucky(), 100)
I hope this works, and there's without any doubt more room for code improvement (but as stated previously, there's always room for improvement!).
With numpy arrays, you can make use of boolean indexing, which may help. For example:
>>> a = numpy.arange(10)
>>> print a
[0 1 2 3 4 5 6 7 8 9]
>>> print a[a > 3]
[4 5 6 7 8 9]
>>> mask = np.array([True, False, True, False, True, False, True, False, True, False])
>>> print a[mask]
[0 2 4 6 8]
Here is a lucky number function using numpy arrays:
import numpy as np
class Didnt_Findit(Exception):
pass
def lucky(n):
'''Return the nth lucky number.
n --> int
returns int
'''
# initial seed
lucky_numbers = [1]
# how many numbers do you need to get to n?
candidates = np.arange(1, n*100, 2)
# use numpy array boolean indexing
next_lucky = candidates[candidates > lucky_numbers[-1]][0]
# accumulate lucky numbers till you have n of them
while next_lucky < candidates[-1]:
lucky_numbers.append(next_lucky)
#print lucky_numbers
if len(lucky_numbers) == n:
return lucky_numbers[-1]
mask_start = next_lucky - 1
mask_step = next_lucky
mask = np.array([True] * len(candidates))
mask[mask_start::mask_step] = False
#print mask
candidates = candidates[mask]
next_lucky = candidates[ candidates > lucky_numbers[-1]][0]
raise Didnt_Findit('n = ', n)
>>> print lucky(10)
33
>>> print lucky(50)
261
>>> print lucky(500)
3975
Checked mine and #icecrime's output for 10, 50 and 500 - they matched.
Yours is much faster than mine and scales better with n.
n=input('enter n ')
a= list(xrange(1,n))
x=a[1]
for i in range(1,n):
del a[x-1::x]
x=a[i]
l=len(a)
if i==l-1:
break
print "lucky numbers till %d" % n
print a
lets do this with an example.lets print lucky numbers till 100
put n=100
firstly a=1,2,3,4,5....100
x=a[1]=2
del a[1::2] leaves
a=1,3,5,7....99
now l=50
and now x=3
then del a[2::3] leaving a =1,3,7,9,13,15,.....
and loop continues till i==l-1

looping through loops in python?

I'm trying to solve this problem on the easy section of coderbyte and the prompt is:
Have the function ArrayAdditionI(arr) take the array of numbers stored in arr and return the string true if any combination of numbers in the array can be added up to equal the largest number in the array, otherwise return the string false. For example: if arr contains [4, 6, 23, 10, 1, 3] the output should return true because 4 + 6 + 10 + 3 = 23. The array will not be empty, will not contain all the same elements, and may contain negative numbers.
Here's my solution.
def ArrayAddition(arr):
arr = sorted(arr, reverse=True)
large = arr.pop(0)
storage = 0
placeholder = 0
for r in range(len(arr)):
for n in arr:
if n + storage == large: return True
elif n + storage < large: storage += n
else: continue
storage = 0
if placeholder == 0: placeholder = arr.pop(0)
else: arr.append(placeholder); placeholder = arr.pop(0)
return False
print ArrayAddition([2,95,96,97,98,99,100])
I'm not even sure if this is correct, but it seems to cover all the numbers I plug in. I'm wondering if there is a better way to solve this through algorithm which I know nothing of. I'm thinking a for within a for within a for, etc loop would do the trick, but I don't know how to do that.
What I have in mind is accomplishing this with A+B, A+C, A+D ... A+B+C ... A+B+C+D+E
e.g)
for i in range(len(arr):
print "III: III{}III".format(i)
storage = []
for j in range(len(arr):
print "JJ: II({}),JJ({})".format(i,j)
for k in range(len(arr):
print "K: I{}, J{}, K{}".format(i,j,k)
I've searched all over and found the suggestion of itertool, but I'm wondering if there is a way to write this code up more raw.
Thanks.
A recursive solution:
def GetSum(n, arr):
if len(arr) == 0 and n != 0:
return False
return (n == 0 or
GetSum(n, arr[1:]) or
GetSum(n-arr[0], arr[1:]))
def ArrayAddition(arr):
arrs = sorted(arr)
return GetSum(arrs[-1], arrs[:-1])
print ArrayAddition([2,95,96,97,98,99,100])
The GetSum function returns False when the required sum is non-zero and there are no items in the array. Then it checks for 3 cases:
If the required sum, n, is zero then the goal is achieved.
If we can get the sum with the remaining items after the first item is removed, then the goal is achieved.
If we can get the required sum minus the first element of the list on the rest of the list the goal is achieved.
Your solution doesn't work.
>>> ArrayAddition([10, 11, 20, 21, 30, 31, 60])
False
The simple solution is to use itertools to iterate over all subsets of the input (that don't contain the largest number):
def subsetsum(l):
l = list(l)
target = max(l)
l.remove(l)
for subset_size in xrange(1+len(l)):
for subset in itertools.combinations(l, subset_size):
if sum(subset) == target:
return True
return False
If you want to avoid itertools, you'll need to generate subsets directly. That can be accomplished by counting in binary and using the set bits to determine which elements to pick:
def subsetsum(l):
l = list(l)
target = max(l)
l.remove(l)
for subset_index in xrange(2**len(l)):
subtotal = 0
for i, num in enumerate(l):
# If bit i is set in subset_index
if subset_index & (1 << i):
subtotal += num
if subtotal == target:
return True
return False
Update: I forgot that you want to check all possible combinations. Use this instead:
def ArrayAddition(l):
for length in range(2, len(l)):
for lst in itertools.combinations(l, length):
if sum(lst) in l:
print(lst, sum(lst))
return True
return False
One-liner solution:
>>> any(any(sum(lst) in l for lst in itertools.combinations(l, length)) for length in range(2, len(l)))
Hope this helps!
Generate all the sums of the powerset and test them against the max
def ArrayAddition(L):
return any(sum(k for j,k in enumerate(L) if 1<<j&i)==max(L) for i in range(1<<len(L)))
You could improve this by doing some preprocessing - find the max first and remove it from L
One more way to do it...
Code:
import itertools
def func(l):
m = max(l)
rem = [itertools.combinations([x for x in l if not x == m],i) for i in range(2,len(l)-1)]
print [item for i in rem for item in i if sum(item)==m ]
if __name__=='__main__':
func([1,2,3,4,5])
Output:
[(1, 4), (2, 3)]
Hope this helps.. :)
If I understood the question correctly, simply this should return what you want:
2*max(a)<=sum(a)

Categories

Resources