Exemplo n.º 1
0
 def test_case2(self):
     a = utility.list(1, 2)
     b = utility.list(3, 4)
     lst = utility.list(a, b)
     l_rev = reverse(lst)
     l_rev_ans = utility.list(b, a)
     self.assertTrue(utility.is_same_list(l_rev, l_rev_ans))
Exemplo n.º 2
0
def main():
    lst0 = list(1, 3, list(5, 7), 9)
    print_list(lst0)
    seven0 = car(
        cdr(
            car(
                cdr(
                    cdr(lst0)))))

    lst1 = list(list(7))
    print_list(lst1)
    seven1 = car(car(lst1))

    lst2 = list(1, list(2, list(3, list(4, list(5, list(6, 7))))))
    print_list(lst2)
    seven2 = car(cdr(
            car(cdr(
                car(cdr(
                    car(cdr(
                        car(cdr(
                            car(cdr(lst2))))))))))))

    assert 7 == seven0
    assert 7 == seven1
    assert 7 == seven2
Exemplo n.º 3
0
 def test_case1(self):
     tr = utility.list(1, utility.list(2, utility.list(3, 4), 5),
                       utility.list(6, 7))
     ans = utility.list(1, utility.list(4, utility.list(9, 16), 25),
                        utility.list(36, 49))
     trmap = tree_map(lambda x: x**2, tr)
     self.assertTrue(utility.is_same_list(trmap, ans))
Exemplo n.º 4
0
def square_list(list):
    if utility.is_null(list):
        return utility.list()
    else:
        return utility.cons(
            square(utility.car(list)),
            square_list(utility.cdr(list)))
Exemplo n.º 5
0
def reverse(lst):
    if utility.is_null(lst):
        return None
    elif utility.length(lst) == 1:
        return lst
    else:
        x = utility.car(lst)
        xs = utility.cdr(lst)
        return utility.append(reverse(xs), utility.list(x))
Exemplo n.º 6
0
 def test_case3(self):
     a = utility.list(1, 2)
     b = utility.list(3, 4)
     lst = utility.list(a, b)
     l_rev = deep_reverse(lst)
     l_rev_ans = utility.list(utility.list(4, 3), utility.list(2, 1))
     self.assertTrue(utility.is_same_list(l_rev, l_rev_ans))
Exemplo n.º 7
0
def same_parity(x, *z):
    """
    :type x: int
    :type z: object
    :rtype: FunctionType
    """
    y = list(*z)

    def same_parity_iter(a):
        """
        :type a: FunctionType | object
        :return:
        """
        if is_null(a):
            return None
        else:
            b = car(a)  # type: int
            if is_even(x - b):
                return cons(b, same_parity_iter(cdr(a)))
            else:
                return same_parity_iter(cdr(a))

    return cons(x, same_parity_iter(y))
Exemplo n.º 8
0
from utility import cons, car, cdr, list, is_null, is_same_list


def square(x):
    return x * x


def bad_square_list(items):
    def iter(things, answer):
        if is_null(things):
            return answer
        else:
            iter(cdr(things), cons(answer, square(car(things))))

    iter(items, None)


if __name__ == '__main__':
    L1 = list(1, 2, 3)
    print(bad_square_list(L1))  # None
Exemplo n.º 9
0
 def test_case1(self):
     lst = utility.list(1, 2, 3, 4)
     l_rev = deep_reverse(lst)
     l_rev_ans = utility.list(4, 3, 2, 1)
     self.assertTrue(utility.is_same_list(l_rev, l_rev_ans))
Exemplo n.º 10
0
 def test_case2(self):
     a = utility.list(1, 2)
     b = utility.list(3, 4)
     lst = utility.list(a, b)
     ll = utility.list(utility.list(1, 2), utility.list(3, 4))
     self.assertTrue(utility.is_same_list(lst, ll))
Exemplo n.º 11
0
 def test_case_3(self):
     l = list(1, 3, 0, 5, 0, 1)
     x = 2
     a = 79
     self.assertEqual(a, horner_eval(x, l))
Exemplo n.º 12
0
 def test_case1(self):
     self.assertEqual(count_leaves(utility.list()), 0)
     self.assertEqual(count_leaves(utility.list(1, 2, 3, 4, 5)), 5)
Exemplo n.º 13
0
from utility import cons, list, append, print_list

if __name__ == '__main__':
    x = list(1, 2, 3)
    y = list(4, 5, 6)

    print_list(append(x, y))
    print_list(cons(x, y))
    print_list(list(x, y))
Exemplo n.º 14
0
def make_mobile(left, right):
    return utility.list(left, right)
Exemplo n.º 15
0
def main():
    lst = utility.list(23, 72, 149, 34)
    last = last_pair(lst)
    print(last)
    assert 34 == last
Exemplo n.º 16
0
def uk_coins():
    return utility.list(100, 50, 20, 10, 5, 2, 1, 0.5)
Exemplo n.º 17
0
def main():
    lst = utility.list(1, 2, 3, 4, 5)
    print(lst)
    result = square_list_with_map(lst)
    print(result)
Exemplo n.º 18
0
 def test_case_1(self):
     self.assertTrue(is_same_list(same_parity(1, 2, 3, 4), list(1, 3)))
Exemplo n.º 19
0
def subsets(l):
    if is_null(l):
        return list(list())
    rest = subsets(cdr(l))
    return append(rest, map(lambda x: cons(car(l), x) , rest))
Exemplo n.º 20
0
from utility import car, cdr, cons, list, print_list, append, map, is_null, is_same_list


def subsets(l):
    if is_null(l):
        return list(list())
    rest = subsets(cdr(l))
    return append(rest, map(lambda x: cons(car(l), x) , rest))


if __name__ == '__main__':
    x = list(1, 2, 3)
    sub_x = subsets(x)
    print_list(x)
    print_list(sub_x)
Exemplo n.º 21
0
def make_branch(length, structure):
    return utility.list(length, structure)
Exemplo n.º 22
0
 def deep_reverse_element(a):
     if callable(a):
         return utility.list(deep_reverse(a))
     else:
         return utility.list(a)
Exemplo n.º 23
0
 def test_case_1(self):
     l = list(1, 0, 0)
     x = 2
     a = 1
     self.assertEqual(a, horner_eval(x, l))
Exemplo n.º 24
0
def main():
    for_each(print, utility.list(57, 321, 88))
Exemplo n.º 25
0
 def test_case_2(self):
     l = list(1, 1, 1)
     x = 2
     a = 7
     self.assertEqual(a, horner_eval(x, l))
Exemplo n.º 26
0
def us_coins():
    return utility.list(50, 25, 10, 5, 1)
Exemplo n.º 27
0
def main():
    def f(x): return print(x)
    for_each(f, utility.list(57, 321, 88))
Exemplo n.º 28
0
 def test_case_2(self):
     self.assertTrue(
         is_same_list(same_parity(2, 3, 4, 5, 6, 7), list(2, 4, 6)))
Exemplo n.º 29
0
 def list(self):
     return utility.list(self.path)