Module TTally
[hide private]
[frames] | no frames]

Module TTally

source code

More arithmetic support for "zero-agnostic" numbers.

This class optimizes the common search computation: given this subset of cords, can I make a sum which is present in the full set of cords, and if so, which cords could be sums?

Examples

Create the set of "possible answers":

>>> cords = Possibilities.create([9,18,90,180,181,222,635])
>>> str(cords)
'[9, 18, 90, 180, 181, 222, 635]'

Find the sums which can be made (note that zeroes are elided in the sums):

>>> sums = tsum([116,118,2436,4440], cords)
>>> list(sorted(sums))
[90, 180]
>>> list(tsum([30,30,30], cords))
[90]

You can also pass the sum in as a list of (frequency, number) tuples, for example:

>>> sums = tsum([(1,116),(1,118),(1,2436),(1,4440)], cords)
>>> list(sorted(sums))
[90, 180]
>>> list(tsum([(3,30)], cords))
[90]

Note that the Possibilities data structure can be reused across multiple sequential calls to tsum, but it is not thread-safe.



Classes [hide private]
  Possibilities
A set of possible "answers" to a summation.
Functions [hide private]
 
tsum(arg, possibilities)
Return an iterator over the possible zero-agnostic sums (from those included in the given possibilities object) which it is possible to obtain by summing the given arguments.
source code
Function Details [hide private]

tsum(arg, possibilities)

source code 
Return an iterator over the possible zero-agnostic sums (from those included in the given possibilities object) which it is possible to obtain by summing the given arguments. The arg may either be a straight list/iterable of zero-agnostic integers, or it may be a list/iterable of pairs (in which case the first component gives the multiplier for the zero-agnostic number in the second component). See the TTally module docs for examples.