示例#1
0
 def test_drop_4(self):
     # Works on infinite lists
     self.assertEqual(zz.count(1).drop(2).take(3).list(), [3, 4, 5])
示例#2
0
 def test_index_7(self):
     pos = zz.count(0, step=1).drop(2).index(lambda x: x % 3 == 1)
     self.assertEqual(pos, 2)
示例#3
0
 def foo():
     arr = []
     for i in zz.count(1):
         arr.append(i)
         yield arr
示例#4
0
 def test_any_7(self):
     # Short-circuits on infinite sequences
     self.assertTrue(zz.count(1).any(lambda x: x > 100))
示例#5
0
 def test_length_3(self):
     self.assertEqual(zz.count(1).take(50).length(), 50)
示例#6
0
 def test_accumulate_3(self):
     self.assertEqual(zz.count(1).accumulate(operator.mul, init = 1).take(5).list(), [1, 1, 2, 6, 24])
#!/usr/bin/python3

import alakazam as zz
from alakazam import _1, _2, _3, _4, _5

from collections import defaultdict

NUMBER = 5

cubes = []
cube_cache = defaultdict(lambda: 0)

def is_permutation(a, b):
    return sorted(str(a)) == sorted(str(b))

for i in zz.count(1).map(_1 ** 3):
    print(i)
    i1 = str(sorted(str(i)))
    cubes.append(i)
    cube_cache[i1] += 1
    if cube_cache[i1] == NUMBER:
        print(zz.of(cubes).filter(lambda x: is_permutation(x, i)).foldr_lazy(lambda x, _: x))
        break
示例#8
0
 def test_group_2(self):
     self.assertEqual(zz.count(0).group(4).take(2).tuple(), ([0, 1, 2, 3], [4, 5, 6, 7]))
示例#9
0
 def test_filter_2(self):
     # filter should be lazy
     self.assertEqual(zz.count(0).filter(lambda x: x % 2 == 1).take(5).list(), [1, 3, 5, 7, 9])
示例#10
0
 def test_takewhile_2(self):
     # Works on infinite lists
     self.assertEqual(zz.count(1).takewhile(lambda x: x % 4 != 0).take(3).list(), [1, 2, 3])
示例#11
0
 def test_enumerate_3(self):
     # Works on infinite lists
     self.assertEqual(zz.count(10).enumerate().take(2).list(), [(0, 10), (1, 11)])
示例#12
0
 def test_groupby_3(self):
     self.assertEqual(zz.count(0).groupby(key = lambda x: x // 3).take(4).tuple(), ((0, [0, 1, 2]), (1, [3, 4, 5]), (2, [6, 7, 8]), (3, [9, 10, 11])))
示例#13
0
 def test_filterfalse_2(self):
     # filterfalse should be lazy
     self.assertEqual(zz.count(0).filterfalse(lambda x: x % 2 == 1).take(5).list(), [0, 2, 4, 6, 8])
示例#14
0
 def test_dropwhile_2(self):
     # Works on infinite lists
     self.assertEqual(zz.count(1).dropwhile(lambda x: x % 3 != 0).take(3).list(), [3, 4, 5])
示例#15
0
 def test_accumulate_1(self):
     self.assertEqual(zz.count(0).accumulate().take(5).list(), [0, 1, 3, 6, 10])
示例#16
0
 def test_zip_7(self):
     iterable = zz.count(0).zip(range(2))
     self.assertEqual(iterable.list(), [(0, 0), (1, 1)])
示例#17
0
 def test_accumulate_2(self):
     self.assertEqual(zz.count(1).accumulate(operator.mul).take(5).list(), [1, 2, 6, 24, 120])
示例#18
0
 def test_flatten_2(self):
     # Works on infinite lists
     self.assertEqual(zz.count(0).map(lambda x: [x]).flatten().take(10).list(), list(range(10)))
示例#19
0
 def test_chain_4(self):
     # chain is lazy
     self.assertEqual(zz.of([1, 2, 3]).chain(zz.count(0), [-1]).take(5).list(), [1, 2, 3, 0, 1])
示例#20
0
 def test_withobject_2(self):
     self.assertEqual(zz.count(1).withobject(-1).take(3).list(), [(-1, 1), (-1, 2), (-1, 3)])
示例#21
0
 def test_foldr_lazy_6(self):
     # Should stop if the argument is not called
     func = lambda x, y: x
     self.assertEqual(zz.count(999).foldr_lazy(func, init=10), 999)
示例#22
0
 def test_interlace_3(self):
     iterable = zz.count(1, step=1).interlace(zz.count(-1, step=-1))
     self.assertEqual(iterable.take(10).list(), [1, -1, 2, -2, 3, -3, 4, -4, 5, -5])
示例#23
0
 def test_null_3(self):
     # Works on infinite lists
     self.assertFalse(zz.count(1).null())
示例#24
0
 def test_intersperse_3(self):
     iterable = zz.count(1, step=1).intersperse(0)
     self.assertEqual(iterable.take(10).list(), [1, 0, 2, 0, 3, 0, 4, 0, 5, 0])
示例#25
0
 def test_apply_2(self):
     gen = zz.count(0)  # Infinite sequence
     self.assertEqual(gen.apply(lambda _: "example text"), "example text")
示例#26
0
 def test_indices_4(self):
     iterable = zz.count(0, step=1).indices(lambda x: x % 3 == 0)
     self.assertEqual(iterable.take(5).list(), [0, 3, 6, 9, 12])
示例#27
0
 def test_count_2(self):
     iterable = zz.count(10, 2).take(10)
     result = range(10, 30, 2)
     self.assertEqual(list(iterable), list(result))
示例#28
0
 def test_map_2(self):
     # map should be lazy
     self.assertEqual(zz.count(0).map(lambda x: x + 1).take(5).list(), list(range(1, 6)))