コード例 #1
0
def test_reduce():
    assert functional.reduce(lambda a, e: a + e,
                              functional.range(1, 11), 0) == \
           55
    assert functional.reduce(lambda a, e: a + e,
                              functional.range(1, 11)) == \
           55
    assert functional.reduce(lambda a, e: a + e,
                              [ [ 1 ], [ 2, 3 ], [ 4, 5, 6 ] ]) == \
           [ 1, 2, 3, 4, 5, 6 ]
    assert functional.reduce(lambda a, e: a + len(e),
                              [ [ 1 ], [ 2, 3 ], [ 4, 5, 6 ] ],
                              0) == \
           6
コード例 #2
0
def is_linear_independent(basis):
    shape_basis = shape(basis)
    V = F.reduce(basis, lambda a, b: mat_add(a, b),
                 [0 for _ in range(0, shape_basis[0])])

    try:
        return len(linear_combination(V, basis)) > 0
    except ValueError:
        return False
コード例 #3
0
ファイル: plotutil.py プロジェクト: bmarcote/jiveplot
def split_label(l, inplot):
    # two brand new empty labels
    mk_lab = lambda: label({}, [])

    def reductor(pl_dsl, attr):
        v = getattr(l, attr)
        setattr(pl_dsl[0], attr, v) if attr in inplot else setattr(
            pl_dsl[1], attr, v)
        return pl_dsl

    return reduce(reductor, label._attrs, (mk_lab(), mk_lab()))
コード例 #4
0
ファイル: plotutil.py プロジェクト: bmarcote/jiveplot
def join_label(l1, l2):
    def attrval(a):
        # it's ok if _either_ of l1 or l2 has the attribute but not both
        aval = filter_(is_not_none, [getattr(l1, a), getattr(l2, a)])
        if len(aval) > 1:
            raise RuntimeError("Duplicate attribute value {0}: {1} {2}".format(
                a, aval[0], aval[1]))
        return None if len(aval) == 0 else aval[0]

    return label(
        reduce(lambda acc, a: operator.setitem(acc, a, attrval(str(a))) or acc,
               label._attrs, dict()), label._attrs)
コード例 #5
0
def linear_combination(V, basis):
    len_V = shape(V)[0]
    shape_basis = shape(basis)

    if F.any(shape_basis, lambda b: b != len_V):
        error_message = "Length of V and basis must be same."
        raise ValueError(error_message)

    S = [Symbol('S{}'.format(i)) for i in range(len_V)]
    expr = [
        F.reduce([S[j] * basis[i][j]
                  for j in range(len_V)], lambda x, y: x + y, -V[i])
        for i in range(len_V)
    ]
    solution = solve(expr, dict=True)

    if len(solution) == 0:
        error_message = "There is linear independent basis."
        raise ValueError(error_message)

    s = solution[0]
    return [s[S[i]] for i in range(len_V)]
コード例 #6
0
ファイル: test.py プロジェクト: nbc-pet-task/functional.py
 def test_reduce(self):
     self.assertEqual(F.reduce(_list, lambda x, y: x + y, 0), 10)
コード例 #7
0
ファイル: mathExt.py プロジェクト: nbc-pet-task/functional.py
def sum(arg):
    if not F.is_sequence(arg): return None
    return F.reduce(arg, lambda a, b: a+b, 0)
コード例 #8
0
def encrypt(numbers):
    min_number = min(numbers)
    return int(
        F.reduce(numbers, lambda a, b: a * b, 1) / min_number *
        (min_number + 1))
コード例 #9
0
ファイル: 33.py プロジェクト: DevStarSJ/algorithmExercise
from fractions import Fraction
import functional as F

result = []
result2 = []
for a in range(11, 99):
    for b in range(a + 1, 100):
        A = a / b
        if a//10 == b%10 and a * (b//10) == b * (a%10):
            result.append((a,b))
            result2.append((a%10, b//10))
        if a%10 == b//10 and a * (b%10) == b * (a//10):
            result.append((a,b))
            result2.append((a//10, b%10))

print(result, result2)
print(Fraction(F.reduce([x for x, _ in result2], lambda a,b: a * b, 1),
         F.reduce([x for _, x in result2], lambda a, b: a * b, 1)))