示例#1
0
def pythagorean_triples(n=None):
    """Generate n Pythagorean triples ordered by the value of c ascending. If n
    is None or not given, generate infinitly. Default is None.
    Examples:
    >>> list(pythagorean_triples(5))
    [(3, 4, 5), (5, 12, 13), (15, 8, 17), (7, 24, 25), (21, 20, 29)]
    """
    iterator = _count() if n is None else range(n)
    iterator = iter(iterator)

    base_mat = ((1, 2, 2),
            (2, 1, 2),
            (2, 2, 3))
    multiplier = ((1, -1, 1), (1, 1, 1), (-1, 1, 1))
    matrices = []
    for multip in multiplier:
        mat = []
        for row, elem in zip(base_mat, multip):
            mat.append(tuple(map(lambda e: e * elem, row)))
        matrices.append(tuple(mat))
    matrices = tuple(matrices)

    heap = [(5, (3, 4, 5))]
    for i in iterator:
        _, triple = _heappop(heap)
        yield triple
        for matrix in matrices:
            next_triple = tuple(map(lambda col: sum(_starmap(_mul,
                zip(triple, col))), zip(*matrix)))
            _heappush(heap, (next_triple[2], next_triple))
示例#2
0
  def elements(self):
    # apply repeat function on each item 
    # chained inputs from a single iterabe

    # iteritems() 
    # Return an iterator over the dictionary's (key, value) pairs.     
    # items()
    # Return a copy of dictionary's list of (key, value) pairs.     
    return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
示例#3
0
 def elements(self):
     """Iterator over elements repeating each as many times as its count.
     
     >>> c = Counter('ABCABC')
     >>> sorted(c.elements())
     ['A', 'A', 'B', 'B', 'C', 'C']
     
     # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
     >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
     >>> product = 1
     >>> for factor in prime_factors.elements():     # loop over factors
     ...     product *= factor                       # and multiply them
     >>> product
     1836
     
     Note, if an element's count has been set to zero or is a negative
     number, elements() will ignore it.
     
     """
     return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
示例#4
0
    def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.items()))
示例#5
0
文件: counter.py 项目: myditto/crab
    def elements(self):
        '''Iterator over elements repeating each as many times as its count.

        >>> from scikits.crab.utils.counter import Counter
        >>> c = Counter('ABCABC')
        >>> sorted(c.elements())
        ['A', 'A', 'B', 'B', 'C', 'C']

        # Knuth's example for prime factors of 1836:  2**2 * 3**3 * 17**1
        >>> prime_factors = Counter({2: 2, 3: 3, 17: 1})
        >>> product = 1
        >>> for factor in prime_factors.elements():     # loop over factors
        ...     product *= factor                       # and multiply them
        >>> product
        1836

        Note, if an element's count has been set to zero or is a negative
        number, elements() will ignore it.

        '''
        # Emulate Bag.do from Smalltalk and Multiset.begin from C++.
        return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
 def elements(self):
     return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
示例#7
0
def transform_items_to_values(dct, func):
    """ apply a function to each value
    """
    return dict(zip(dct.keys(), _starmap(func, dct.items())))
示例#8
0
 def elements(self):
     return _chain.from_iterable(_starmap(_repeat, self.iteritems()))
示例#9
0
 def elements(self):
     'Iterator returning each element as many times as its multiplicity.'
     return _chain.from_iterable(_starmap(_repeat, self._ms.items()))