简体   繁体   中英

Grouping numbers and sums python

I was given some numbers lets say

1 2 3 4 5 6 7

Would there be some way to find a way to group them so that the sum of the multiple groups is odd then even? I was given this problem for fun.

So far, I have tried something like this,

numbers="7 1 3 5 7 9 11 13"
a_list = numbers.split()
map_object = map(int, a_list)
list_of_integers = list(map_object)
print(list_of_integers)

N=list_of_integers[0]

def subset_sumed(numbers, target, partial=[]):
    s = sum(partial)
    if s == target:
        print("sum(%s)=%s" % (partial, target))
        print(partial)
        return(s)
    if s >= target:
        return 

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i + 1:]
        subset_sumed(remaining, target, partial + [n])

del list_of_integers[0]
list_of_integers.sort()
sumed=list_of_integers[-1]+list_of_integers[-2]
sumss=3

possible_combinations=[]
while sumss<sumed:
  a=subset_sumed(list_of_integers,int(sumss))
  sumss = int(sumss)+1
  possible_combinations.append(a)

i=0
nextint1=2
nextint2=1
groupnumber=0
integersused=[]
print(possible_combinations)

This will get even and then odd groups (groups of alternating parity).

The approach uses recursion and the fact that adding an even number to a number doesn't alter the parity.

Explanation is in the comments below (it is mostly comments).

def get_groups(remaining, curr_sum, group, target, result):
    # case 1. End condition / base case
    if not remaining:
        # If the current group's sum matches target parity, simply append it as its own group
        # case 1.a
        if curr_sum % 2 == target:
            return result + [group]
        if result:
            # case 1.b
            # combine this group with the previous one
            result[-1] = result[-1] + group
        else:
            # case 1.d
            # There's no previous group to combine with
            print('impossible!')
            return None
        # case 1.c
        # If this group's sum is odd, but the target parity is even (which is true at this point)
        # then the previous group's sum was odd and adding the group to previous group makes even
        # and an even group can be added to its previous group without changing that group's parity
        if curr_sum % 2 == 1:
            last = result.pop()
            result[-1] = result[-1] + last
        return result
    # case 2. reached target parity with current group's sum
    if curr_sum > 0 and curr_sum % 2 == target:
        return get_groups(remaining, 0, [], 1 - target, result + [group])

    # case 3. keep moving rightwards, including the leftmost element into current group
    head = remaining[0]
    return get_groups(remaining[1:], curr_sum + head, group + [head], target, result)

def get_even_odd_groups(a):
    return get_groups(a, 0, [], 0, [])

A = [7, 1, 3, 5, 7, 9, 11, 13]
B = [7, 1, 3, 5, 7, 9, 11, 13, 2, 4, 6]
C = [7, 1, 3, 5, 7, 9, 11]
D = [9, 2, 2]
print(get_even_odd_groups(A))    # case 1.a
print(get_even_odd_groups(B))    # case 1.b (but not 1.c)
print(get_even_odd_groups(C))    # case 1.c (and therefore also 1.b)
print(get_even_odd_groups(D))    # case 1.c (impossible!)

Output:

[[7, 1], [3], [5, 7], [9], [11, 13]]
[[7, 1], [3], [5, 7], [9], [2, 11, 13, 2, 4, 6]]
[[7, 1], [3], [5, 7, 9, 11]]
impossible!
None

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