My own accounting python euler problem

Dan Bishop danb_83 at yahoo.com
Sun Nov 8 11:52:37 EST 2009


On Nov 8, 4:43 am, Ozz <notva... at wathever.com> wrote:
> Hi,
>
> > My first question is:
> > 1. given a list of invoives I=[500, 400, 450, 200, 600, 700] and a
> > check Ch=600
> > how can I print all the different combinations of invoices that the
> > check is possibly cancelling
>
> Incidentally, I'm currently learning python myself, and was working on
> more or less the same problem as an exercise;
>
> For listing all different subsets of a list (This is what I came up
> with. Can it be implemented shorter, btw?):
>
> def subsets(L):
>          S = []
>          if (len(L) == 1):
>                  return [L, []]
>          else:
>                  for s in subsets(L[1:]):
>                          S.append(s)
>                          S.append(s + [ L[0]])
>          return S

You can avoid the S list my making it a generator:

def subsets(L):
    if L:
        for s in subsets(L[1:]):
            yield s
            yield s + [L[0]]
    else:
        yield []



More information about the Python-list mailing list