about summary refs log tree commit diff
path: root/scratch/deepmind/part_two/cafe-order-checker.py
blob: 0e31214b830d33ee4b02308f34180db42b4e0bfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import unittest


# Solution
def is_first_come_first_served(xs, ys, zs):
    i, j = 0, 0
    for z in zs:
        if i < len(xs) and z == xs[i]:
            i += 1
        elif j < len(ys) and z == ys[j]:
            j += 1
        else:
            return False
    return i == len(xs) and j == len(ys)


# Tests
class Test(unittest.TestCase):
    def test_both_registers_have_same_number_of_orders(self):
        result = is_first_come_first_served([1, 4, 5], [2, 3, 6],
                                            [1, 2, 3, 4, 5, 6])
        self.assertTrue(result)

    def test_registers_have_different_lengths(self):
        result = is_first_come_first_served([1, 5], [2, 3, 6], [1, 2, 6, 3, 5])
        self.assertFalse(result)

    def test_one_register_is_empty(self):
        result = is_first_come_first_served([], [2, 3, 6], [2, 3, 6])
        self.assertTrue(result)

    def test_served_orders_is_missing_orders(self):
        result = is_first_come_first_served([1, 5], [2, 3, 6], [1, 6, 3, 5])
        self.assertFalse(result)

    def test_served_orders_has_extra_orders(self):
        result = is_first_come_first_served([1, 5], [2, 3, 6],
                                            [1, 2, 3, 5, 6, 8])
        self.assertFalse(result)

    def test_one_register_has_extra_orders(self):
        result = is_first_come_first_served([1, 9], [7, 8], [1, 7, 8])
        self.assertFalse(result)

    def test_one_register_has_unserved_orders(self):
        result = is_first_come_first_served([55, 9], [7, 8], [1, 7, 8, 9])
        self.assertFalse(result)

    # Bonus
    def test_handles_repeats(self):
        actual = is_first_come_first_served([1, 2, 1], [3, 4, 5, 5],
                                            [3, 4, 1, 5, 5, 2, 1])
        self.assertTrue(actual)

    def test_kitchen_didnt_serve(self):
        actual = is_first_come_first_served([1, 2], [3, 4], [1, 3, 4])
        self.assertFalse(actual)

    def test_customer_didnt_pay(self):
        actual = is_first_come_first_served([2], [3, 4], [1, 3, 4])
        self.assertFalse(actual)


unittest.main(verbosity=2)