-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhilbert.py
executable file
·47 lines (35 loc) · 1.12 KB
/
hilbert.py
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
#!/usr/bin/env python3
"""A primitive implementation of the Hilbert's Hotel problem.
This implementation operates with infinite generators."""
def naturals():
"""Returns generator of whole numbers starting at 1."""
n = 1
while True:
yield n
n += 1
def rooms():
"""Returns generator of tuples of room number (int) and guest
(string)."""
nats = naturals()
while True:
n = next(nats)
yield (n, "guest_" + str(n))
def check_in(guest, rooms):
"""Returns generator of tuples of room number (int) and guest
(string), after checking in the given guest into the first room."""
pred = guest
while True:
n, succ = next(rooms)
yield (n, pred)
pred = succ
if __name__ == "__main__":
# nats = naturals()
# print(next(nats))
# print(next(nats))
# hotel = rooms()
# print(next(hotel))
# print(next(hotel))
hilberts_hotel = check_in("hilbert", rooms())
assert(next(hilberts_hotel) == (1, "hilbert"))
assert(next(hilberts_hotel) == (2, "guest_1"))
assert(next(hilberts_hotel) == (3, "guest_2"))