コード例 #1
0
def test_map():
    assert functional.map(lambda e: e * 2,
                          functional.range(1, 11)) == \
           [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ]
    assert functional.map(lambda e: e,
                          [ [ 1 ], [ 2 ], [ 3 ] ]) == \
           [ [ 1 ], [ 2 ], [ 3 ] ]
コード例 #2
0
 def test_requires_iterable(self):
     from functional import map
     
     try:
         map(str, 5)
     except TypeError:
         pass
     else:
         raise AssertionError("Failed to raise TypeError")
コード例 #3
0
 def test_requires_callable(self):
     from functional import map
     
     try:
         map(None, [4, 5])
     except TypeError:
         pass
     else:
         raise AssertionError("Failed to raise TypeError")
コード例 #4
0
    def test_requires_callable(self):
        from functional import map

        try:
            map(None, [4, 5])
        except TypeError:
            pass
        else:
            raise AssertionError("Failed to raise TypeError")
コード例 #5
0
    def test_requires_iterable(self):
        from functional import map

        try:
            map(str, 5)
        except TypeError:
            pass
        else:
            raise AssertionError("Failed to raise TypeError")
コード例 #6
0
    def test_single_iterable(self):
        from functional import map
        from operator import add

        try:
            map(add, [4, 5, 6], [6, 7, 8])
        except TypeError:
            pass
        else:
            raise AssertionError("Failed to raise TypeError")
コード例 #7
0
 def test_single_iterable(self):
     from functional import map
     from operator import add
     
     try:
         map(add, [4, 5, 6], [6, 7, 8])
     except TypeError:
         pass
     else:
         raise AssertionError("Failed to raise TypeError")
コード例 #8
0
def test_partial():
    def add(a, b):
        return a + b

    def add10(x):
        return add(x, 10)

    assert functional.map(add10,
                          functional.range(1, 11)) == \
           [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
    assert functional.map(functional.partial(add, 10),
                          functional.range(1, 11)) == \
           [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
コード例 #9
0
def handle_country(country: str, filter_func: callable,
                   qrz_session: requests.Session) -> (str, list):
    return country[1], chain(
        filter_func,
        to(map(func=get_call_sign_data(qrz_session=qrz_session),
               kw='call_sign'),
           kw='iterable'),
    )(func=startswith(substring=country[0]))
コード例 #10
0
def handle_arrl_data(contacted_call_signs: tuple, missing_countries: dict,
                     qrz_session: requests.Session) -> dict:
    return chain(
        map(func=handle_country(filter_func=filterer(
            iterable=contacted_call_signs, kw='string'),
                                qrz_session=qrz_session),
            kw='country'), filter_empty_values,
        dict)(iterable=missing_countries.items())
コード例 #11
0
 def test_map(self):
     from functional import map
     def as_listcomp(func, seq):
         return map(func, seq), [func(x) for x in seq]
 
     self.assertEqual(*as_listcomp(str, [4, 5, 6]))
     self.assertEqual(*as_listcomp(str, "abc"))
     self.assertEqual(*as_listcomp(str, (5, 6, 7)))
     
     def gen():
         yield 5
         yield 6
         yield 7
         
     self.assertEqual(map(str, gen()), [str(x) for x in gen()])
コード例 #12
0
    def test_map(self):
        from functional import map

        def as_listcomp(func, seq):
            return map(func, seq), [func(x) for x in seq]

        self.assertEqual(*as_listcomp(str, [4, 5, 6]))
        self.assertEqual(*as_listcomp(str, "abc"))
        self.assertEqual(*as_listcomp(str, (5, 6, 7)))

        def gen():
            yield 5
            yield 6
            yield 7

        self.assertEqual(map(str, gen()), [str(x) for x in gen()])
コード例 #13
0
def test_curry():
    def args(*a):
        return a

    cargs = functional.curry(args, 5)
    cargsab = cargs('a')('b')
    assert cargsab('c')('d')('e') == ('a', 'b', 'c', 'd', 'e')
    assert cargsab('x')('y')('z') == ('a', 'b', 'x', 'y', 'z')
    assert cargsab('c')('d')('e') == ('a', 'b', 'c', 'd', 'e')
    assert cargsab('x')('y')('z') == ('a', 'b', 'x', 'y', 'z')

    def add(a, b):
        return a + b

    cadd = functional.curry(add)
    assert cadd(10)(5) == 15 and cadd(20)(5) == 25

    assert functional.map(functional.curry(add)(10),
                          functional.range(1, 11)) == \
           [ 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ]
コード例 #14
0
def stock_history(init_investment, monthly_contribution, stock_prices):
    stock_prices = F.map(stock_prices,
                         F.pipe(lambda x: x.split(" "),
                                lambda x: [int(a) for a in x]))

    sell_prices = stock_prices[-1]

    total_earning = 0
    total_investment = 0
    max_earning_rate = [1 for _ in sell_prices]

    for month in range(len(stock_prices) -2, -1, -1):
        current_prices = stock_prices[month]
        earning_rate = [sell_prices[i] / price for i, price in enumerate(current_prices)]
        max_earning_rate = [max(a, b) for a, b in zip(max_earning_rate, earning_rate)]

        money = init_investment if month == 0 else monthly_contribution
        total_earning += money * max(max_earning_rate)
        total_investment += money

    return int(round(total_earning - total_investment))
コード例 #15
0
 def as_listcomp(func, seq):
     return map(func, seq), [func(x) for x in seq]
コード例 #16
0
ファイル: 22.py プロジェクト: DevStarSJ/algorithmExercise
import functional as F
names = sorted(
    F.map(
        open("names.txt", 'r').readline().split(','),
        lambda x: x.replace('"', '')))
A = ord('A')


def getValue(c):
    return ord(c) - A + 1


score = 0
for i, name in enumerate(names):
    score += (i + 1) * sum([getValue(c) for c in name])

print(score)
コード例 #17
0
def test_factorial():
    assert functional.map(functional.factorial, functional.range(1, 11)) == \
           [ 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800 ]
コード例 #18
0
 def listmultiply2(l):
     return functional.map(lambda e: e * 2, l)
コード例 #19
0
ファイル: 32.py プロジェクト: DevStarSJ/algorithmExercise
results = set()

for a in set_a:
    len_a = len(a)
    for num_b in range(1, 9 - len(a)):
        set_b = get_except(sets[num_b], a)
        for b in set_b:
            len_b = len(b)
            len_c = 9 - len_a - len_b

            if not (len_c <= len_a + len_b <= len_c + 1):
                continue

            c = list((set_digits - set(a + b)))

            for cm in M.combination(c):
                cc = int("".join(F.map(cm, str)))
                if cc in results:
                    continue

                for am in M.combination(a):
                    for bm in M.combination(b):
                        aa = int("".join(F.map(am, str)))
                        bb = int("".join(F.map(bm, str)))

                        if aa * bb == cc:
                            print(aa, "x", bb, "=", cc)
                            results.add(cc)
results = list(results)
print(sum(results), ":", results)
コード例 #20
0
def test_fibonacci():
    assert functional.map(functional.fibonacci, functional.range(1, 11)) == \
           [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]
コード例 #21
0
ファイル: test.py プロジェクト: nbc-pet-task/functional.py
 def test_map(self):
     self.assertEqual(F.map(_list, lambda x: x * x), [1, 4, 9, 16])
     self.assertEqual(F.map(_tuple, lambda x: x * x), [1, 4, 9])
コード例 #22
0
 def test_with_bound_and_unbound_methods(self):
     data = map(str, range(10))
     join = self.thetype(str.join, '')
     self.assertEqual(join(data), '0123456789')
     join = self.thetype(''.join)
     self.assertEqual(join(data), '0123456789')
コード例 #23
0
ファイル: mathExt.py プロジェクト: nbc-pet-task/functional.py
def softmax2D(M, t = 1.0):
    E = F.map(M, lambda row: [math.exp(x/t) for x in row])
    total = F.sum(F.map(E, lambda row: F.sum(row)))
    return F.map(E, lambda row: [x/total for x in row])
コード例 #24
0
ファイル: 18.py プロジェクト: DevStarSJ/algorithmExercise
"75",
"95 64",
"17 47 82",
"18 35 87 10",
"20 04 82 47 65",
"19 01 23 75 03 34",
"88 02 77 73 07 63 67",
"99 65 04 28 06 16 70 92",
"41 41 26 56 83 40 80 70 33",
"41 48 72 33 47 32 37 16 94 29",
"53 71 44 65 25 43 91 52 97 51 14",
"70 11 33 28 77 73 17 78 39 68 17 57",
"91 71 52 38 17 14 91 43 58 50 27 29 48",
"63 66 04 68 89 53 67 30 73 16 69 87 40 31",
"04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
]
triangle = F.map(triangle, lambda y: [int(x) for x in y.split(' ')])

def get_upper(x, y):
    pos_y = y - 1
    start_x = 0 if x < 1 else x - 1
    prev_x_len = len(triangle[pos_y])
    end_x = min([x, prev_x_len])
    return max(triangle[pos_y][start_x:end_x + 1])

for y in range(1, len(triangle)):
    for x in range(len(triangle[y])):
        triangle[y][x] += get_upper(x, y)

print(max(triangle[-1]))
コード例 #25
0
 def test_with_bound_and_unbound_methods(self):
     data = map(str, range(10))
     join = self.thetype(str.join, '')
     self.assertEqual(join(data), '0123456789')
     join = self.thetype(''.join)
     self.assertEqual(join(data), '0123456789')
コード例 #26
0
 def as_listcomp(func, seq):
     return map(func, seq), [func(x) for x in seq]
コード例 #27
0
 def listadd1(l):
     return functional.map(lambda e: e + 1, l)