def bootOK(b): canRow = SetlxSet( [SetlxString('Mutter'), SetlxString('Vater'), SetlxString('Polizist')]) return ((SetlxString('Boot') in b) and ((stlx_len(b)) <= 3)) and ((canRow * b) != SetlxSet([]))
def stlx_powerset(s): """If s is a set, the expression pow(s) computes the power set of s. The power set of s is defined as the set of all subsets of s.""" def powerset_generator(i): for subset in it.chain.from_iterable( it.combinations(i, r) for r in range(len(i) + 1)): yield set(subset) return SetlxSet(SetlxSet(z) for z in powerset_generator(s))
def verbotenSide(s): boys = SetlxSet([SetlxString('Anton'), SetlxString('Bruno')]) girls = SetlxSet([SetlxString('Cindy'), SetlxString('Doris')]) return ((((SetlxString('Verbrecher') in s) and ((stlx_len(s)) > 1)) and (not (SetlxString('Polizist') in s))) or ((((boys * s) != SetlxSet([])) and (SetlxString('Mutter') in s)) and (not (SetlxString('Vater') in s)))) or ( (((girls * s) != SetlxSet([])) and (SetlxString('Vater') in s)) and (not (SetlxString('Mutter') in s)))
def findPath(x, y, r): p = SetlxSet([SetlxList([x])]) while True: oldP = p p = p + (pathProduct(p, r)) found = SetlxSet([l for l in p if ((l[stlx_len(l)]) == y)]) if found != SetlxSet([]): return stlx_arb(found) if p == oldP: return
def test_set_powerset(): s = """ s1 := { 1, 2 }; result := 2 ** s1; """ assert_res( s, { 'result': SetlxSet( [SetlxSet(), SetlxSet([1]), SetlxSet([1, 2]), SetlxSet([2])]) })
def test_set_comprehension_cray(): s = 'primes := { p:p in {2..100} | { t:t in {2..p-1} | p % t == 0 } == {} };' primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ] assert_res(s, {'primes': SetlxSet(primes)})
def test_binop_set(): s = Template(""" s1 := { 1, 2 }; s2 := { 2, 3 }; result := s1 $op s2; """) cases = [ ('op', 'result'), ('+', SetlxSet([1, 2, 3])), ('-', SetlxSet([1])), ('*', SetlxSet([2])), ('><', SetlxSet([(1, 2), (1, 3), (2, 2), (2, 3)])), ('%', SetlxSet([1, 3])), ] assert_res_cases(s, cases)
def stlx_range(s): """ If r is a binary relation, then the equality range(r) = { y :[x,y] in R } holds. """ lst = [y for unused, y in s] return SetlxSet(lst)
def test_for_loop_double(): s = """ accum := {}; for(x in {1..2}, y in {-1,-2..-2}) { accum += {[x, y]}; } """ assert_res(s, {'accum': SetlxSet([[1, -2], [1, -1], [2, -2], [2, -1]])})
def test_assignment_augmented_set(): s = Template(""" s1 := { 1, 2 }; s2 := { 2, 3 }; s1 $op s2; """) cases = { '+=': SetlxSet((1, 2, 3)), '-=': SetlxSet((1, )), '*=': SetlxSet((2, )), '%=': SetlxSet((1, 3)), } for op, result in cases.items(): source = s.substitute(op=op) assert_res(source, {'s1': result})
def test_procedure_two(): s = Template(""" factors := procedure(p) { return {f : f in { 1 .. p } | p % f == 0 }; }; primes := procedure(n) { return {p : p in { 2 .. n } | factors(p) == { 1, p } }; }; result := primes($n); """) cases = [ ('n', 'result'), ('2', SetlxSet([2])), ('10', SetlxSet([2, 3, 5, 7])), ('50', SetlxSet([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47])), ] assert_res_cases(s, cases)
def printPath(path, all): for i in SetlxList(stlx_lst_from_range(1, (stlx_len(path)))): left = path[i] right = all - left if ((stlx_len(left)) == 9) or ((stlx_len(right)) == 9): stlx_print(left - SetlxSet([SetlxString('Boot')]), 18 * SetlxString(' '), right - SetlxSet([SetlxString('Boot')])) stlx_print(SetlxString('')) else: stlx_print(left - SetlxSet([SetlxString('Boot')]), 20 * SetlxString(' '), right - SetlxSet([SetlxString('Boot')])) stlx_print(SetlxString('')) if i == (stlx_len(path)): break if SetlxString('Boot') in left: m = (left - (path[i + 1])) - SetlxSet([SetlxString('Boot')]) stlx_print(SetlxString(' >>>> '), m, SetlxString(' >>>> ')) else: m = (right - (all - (path[i + 1]))) - SetlxSet([SetlxString('Boot')]) stlx_print(SetlxString(' <<<< '), m, SetlxString(' <<<< ')) stlx_print(SetlxString(''))
def test_procedure_primes(): s = Template(""" primes := procedure(n) { s := { 2..n }; return s - { p*q : [p, q] in s >< s }; }; result := primes($n); """) cases = { '2': [2], '10': [2, 3, 5, 7], '50': [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47], } for n, result in cases.items(): source = s.substitute(n=n) assert_res(source, {'result': SetlxSet(result)})
def test_set_cartesian(): s = """ s1 := { 1, 2 }; result := s1 ** 2; """ assert_res(s, {'result': SetlxSet([(1, 1), (1, 2), (2, 1), (2, 2)])})
def test_create(): x = SetlxSet([5, 4, 4, 2, 5, 1]) eq_(len(x), 4)
def test_get_by_int_key(): x = SetlxSet([[4, 3, 2, 1]]) x[3]
def test_set_comprehension_cond(): s = """ p := 42; divisors := { t:t in {2..p-1} | p % t == 0 }; """ assert_res(s, {'divisors': SetlxSet([2, 3, 6, 7, 14, 21])})
def test_set_comprehension_two_iterators(): s = 'x := {a ** b: a in {1..3}, b in {2..4}};' assert_res(s, {'x': SetlxSet([1, 4, 8, 9, 16, 27, 81])})
def test_set_value(): x = SetlxSet([[1, 2], [3, 4]]) x[5] = 6 eq_(x[5], 6)
def test_range_set(): assert_res('x := {1..5};', {'x': SetlxSet([1, 2, 3, 4, 5])}) assert_res('x := {1..-1};', {'x': SetlxSet([])}) assert_res('x := {1,3..10};', {'x': SetlxSet([1, 3, 5, 7, 9])}) assert_res('x := {10,8..1};', {'x': SetlxSet([10, 8, 6, 4, 2])}) assert_res('x := {-1,-2..-2};', {'x': SetlxSet([-2, -1])})
def test_set(): assert_res('x := {};', {'x': SetlxSet([])}) assert_res('x := {1};', {'x': SetlxSet([1])}) assert_res('x := {1,2};', {'x': SetlxSet([1, 2])}) assert_res('x := {1,2,3};', {'x': SetlxSet([1, 2, 3])}) assert_res('x := {1+3,2-4,3**0};', {'x': SetlxSet([4, -2, 1])})
def test_get_map_by_int_key(): x = SetlxSet([[1, 2], [3, 4]]) eq_(x[1], 2) eq_(x[3], 4)
def cyclic(p): return (stlx_len((SetlxSet([x for x in p])))) < (stlx_len(p))
def test_get_duplicate(): x = SetlxSet([[1, 1], [1, 4], [3, 3]]) eq_(x[1], None) eq_(x[2], None)
def stlx_cartesian(*args): return SetlxSet(it.product(*args))
def test_set_comprehension_minimal(): s = 'x := {2*n : n in [1..5]};' assert_res(s, {'x': SetlxSet([2, 4, 6, 8, 10])})
def stlx_collect(m): d = collections.defaultdict(int) for x in m: d[x] += 1 return SetlxSet([SetlxList([k, c]) for k, c in d.iteritems()])
def test_set_operations(): s1 = SetlxSet([1, 2]) s2 = SetlxSet([2, 3]) eq_(s1 + s2, SetlxSet([1, 2, 3])) eq_(s1 - s2, SetlxSet([1])) eq_(s1 * s2, SetlxSet([2])) eq_(s1 % s2, SetlxSet([1, 3])) eq_(stlx_pow(s1, 2), SetlxSet([(1, 1), (1, 2), (2, 1), (2, 2)])) eq_(stlx_pow(2, s2), SetlxSet([SetlxSet(), SetlxSet([2]), SetlxSet([2, 3]), SetlxSet([3])]))
def stlx_domain(s): """""" lst = [x for x, unused in s] return SetlxSet(lst)
def pathProduct(p, q): return SetlxSet([ add(x, y) for x in p for y in q if (((x[stlx_len(x)]) == (y[1])) and (not (cyclic(add(x, y))))) ])