Versionsvergleich
Aus ProgrammingWiki
Vergleich: 2021-04-10T11:19:06+00:00 → 2021-04-19T07:10:29+00:00
<language>Python</language> | |
<code id="60717516e7a6a"> | |
| + | import copy |
| + | |
class stream: | |
| − | def make_stream(self): |
| − | n = False |
| − | while True: |
| − | yield n |
| − | #n += 1 |
| + | def make_stream(self): |
| + | n = False |
| + | while True: |
| + | yield n |
| − | def __init__(self): |
| − | self.li = [] |
| − | self.stream = self.make_stream() |
| + | def __init__(self, *args): |
| + | self.li = [] |
| + | if len(args) > 0: |
| + | self.stream = args[0] |
| + | else: |
| + | self.stream = self.make_stream() |
| − | def anext(self): |
| − | item = next(self.stream) |
| − | self.li.append(item) |
| − | return item |
| − | |
| − | def next_until(self, n): |
| − | if n < len(self.li): |
| − | return self.li[n] |
| − | else: |
| − | item = None |
| − | while n >= len(self.li): |
| − | item = self.anext() |
| − | return item |
| + | def anext(self): |
| + | try: |
| + | item = next(self.stream) |
| + | except TypeError: |
| + | item = next(self.stream.get_infinite()) |
| + | self.li.append(item) |
| + | return item |
| − | def get(self, n): |
| − | if n >= len(self.li): |
| − | return self.next_until(n) |
| − | else: |
| − | return self.li[n] |
| + | def next_until(self, n): |
| + | if n < len(self.li): |
| + | return self.li[n] |
| + | else: |
| + | item = None |
| + | while n >= len(self.li): |
| + | item = self.anext() |
| + | return item |
| − | def get_finite(self): |
| − | return self.li |
| + | def get(self, n): |
| + | if n >= len(self.li): |
| + | return self.next_until(n) |
| + | else: |
| + | return self.li[n] |
| − | def einzelbuchung(self, n, value, error = True): |
| − | self.get(n) |
| − | if self.li[n] == False: |
| − | self.li[n] = value |
| − | return 0 |
| − | else: |
| − | if error: |
| − | print(f"Error in einzelbuchung: Zimmer {n} ist belegt! Hier wohnt {self.li[n - 1]}") |
| − | return -1 |
| + | def get_finite(self): |
| + | return self.li |
| − | def zeig_mir(self, n): |
| − | self.get(n) |
| − | return self.get_finite()[0:n] |
| + | def get_infinite(self): |
| + | return self.stream |
| − | def kleingruppenbuchung(self, finite_bus): |
| − | for i, value in enumerate(finite_bus): |
| − | pos = i |
| − | while self.einzelbuchung(pos, value, False) == -1: |
| − | pos += 1 |
| − | |
| − | def check_in(self, infinite_bus): |
| − | pass |
| + | def einzelbuchung(self, n, value, error=True): |
| + | self.get(n) |
| + | if self.li[n] == False: |
| + | self.li[n] = value |
| + | return 0 |
| + | else: |
| + | if error: |
| + | print(f"Error in einzelbuchung: Zimmer {n} ist belegt! Hier wohnt {self.li[n - 1]}") |
| + | return -1 |
| + | def zeig_mir(self, n, seats = -1, p=True): |
| + | if seats >= 0: |
| + | ret = [] |
| + | for i in self.zeig_mir(n, -1, False): |
| + | ret.append(i.zeig_mir(seats, -1, p)) |
| + | return ret |
| + | else: |
| + | self.get(n - 1) #-? |
| + | ret = self.get_finite()[0:n] |
| + | if p: |
| + | ret.append("...") |
| + | print(ret) |
| + | return ret |
| + | |
| + | def kleingruppenbuchung(self, finite_bus): |
| + | for i, value in enumerate(finite_bus): |
| + | pos = i |
| + | while self.einzelbuchung(pos, value, False) == -1: |
| + | pos += 1 |
| + | |
| + | def fill_finite_part(self, infinite_bus): |
| + | finite_size = len(self.li) |
| + | f = infinite_bus.zeig_mir(finite_size, -1, False) |
| + | self.kleingruppenbuchung(f) |
| + | |
| + | |
| + | def imerge(self, a, b): |
| + | for i, j in zip(a, b): |
| + | yield i |
| + | yield j |
| + | |
| + | def i_bus_merge(self, a, b): |
| + | while True: |
| + | i = next(a) |
| + | j = next(b) |
| + | if i == False: |
| + | i = next(a) |
| + | if i == False: |
| + | yield j |
| + | else: |
| + | yield i |
| + | yield j |
| + | else: |
| + | yield i |
| + | yield j |
| + | |
| + | def check_in(self, infinite_bus): |
| + | self.fill_finite_part(infinite_bus) |
| + | self.stream = self.i_bus_merge(self.stream, infinite_bus.get_infinite()) |
| + | |
| + | |
| + | def gehe_in_gerade(self): |
| + | li_copy = copy.copy(self.li) |
| + | f = len(self.li) |
| + | self.li.extend([False] * f) |
| + | for i, value in list(enumerate(li_copy))[::-1]: |
| + | new_index = 2 * i |
| + | self.li[new_index] = li_copy[i] |
| + | if i != 0: |
| + | self.li[i] = False |
| + | self.stream = self.imerge(self.stream, self.make_stream()) |
| + | |
| + | def zeige_zimmer(self, n): |
| + | print(self.zeig_mir(n + 1, -1, False)[n]) |
| + | |
hilberthotel = stream() | |
| − | print(hilberthotel.zeig_mir(11)) |
| + | </code> |
| + | <run id="607d1db4c9cb6"> |
| + | hilberthotel.zeig_mir(11) |
| + | </run> |
| + | <run id="607d1db4c9d23"> |
| + | print("EINZELBUCHUNGEN") |
hilberthotel.einzelbuchung(3, "Otto Grundmann") | |
| − | print(hilberthotel.zeig_mir(12)) |
| + | hilberthotel.zeig_mir(12) |
hilberthotel.einzelbuchung(2, "Hella Milkel") | |
hilberthotel.einzelbuchung(3, "Heribert Wuischke") | |
hilberthotel.einzelbuchung(7, "Heribert Wuischke") | |
| − | print(hilberthotel.zeig_mir(12)) |
| − | print() |
| + | hilberthotel.zeig_mir(12) |
| + | </run> |
| + | |
| + | <code id="607d1db4c9a14"> |
class finite_bus: | |
| − | def __init__(self, id, places): |
| − | self.id = id |
| − | self.places = places |
| − | |
| − | def zeig_mir(self): |
| − | return [tuple((self.id, i)) for i in range(self.places + 1)] |
| + | def __init__(self, id, places): |
| + | self.id = id |
| + | self.places = places |
| + | def zeig_mir(self, p=True): |
| + | li = [tuple((self.id, i)) for i in range(self.places + 1)] |
| + | if p: |
| + | print(li) |
| + | return li |
| + | </code> |
| − | |
| + | <run id="607d1db4c9d8b"> |
| + | print("KLEINBUS") |
kleinbus815 = finite_bus(815, 8) | |
| − | print(kleinbus815.zeig_mir()) |
| + | kleinbus815.zeig_mir(False) |
| − | hilberthotel.kleingruppenbuchung(kleinbus815.zeig_mir()) |
| − | hilberthotel.zeig_mir(12) |
| + | hilberthotel.kleingruppenbuchung(kleinbus815.zeig_mir(False)) |
| + | hilberthotel.zeig_mir(15) |
| + | print() |
| + | </run> |
| − | class infinite_bus: |
| − | def __init__(self, id): |
| − | self.id = id |
| − | self.stream = self.make_stream() |
| − | |
| − | def make_stream(self): |
| + | <code id="607d1db4c9aaf"> |
| + | def make_infinite_bus(bus_id): |
n = 0 | |
| − | t = tuple((self.id, n)) |
| + | t = tuple((bus_id, n)) |
while True: | |
yield t | |
n += 1 | |
| − | t = tuple((self.id, n)) |
| + | t = tuple((bus_id, n)) |
| + | </code> |
| − | def zeig_mir(self, n): |
| − | result = [] |
| − | try: |
| − | for i in range(n + 1): |
| − | result.append(next(self.stream)) |
| − | except StopIteration: |
| − | pass |
| − | return result |
| − | bus3813 = infinite_bus(3813) |
| + | <run id="607d1db4c9df6"> |
| + | print("UNENDLICHER BUS") |
| + | bus3813 = stream(make_infinite_bus(3813)) |
bus3813.zeig_mir(8) | |
| + | hilberthotel.check_in(bus3813) |
| + | hilberthotel.zeig_mir(20) |
| + | print() |
| + | print("GEHE IN GERADE") |
| + | hilberthotel.gehe_in_gerade() |
| + | hilberthotel.zeig_mir(45) |
| + | print() |
| + | </run> |
| + | |
| + | <run id="607d1db4c9ec6"> |
| + | print("2. UNENDLICHER BUS") |
| + | bus33 = stream(make_infinite_bus(33)) |
| + | hilberthotel.check_in(bus33) |
| + | hilberthotel.zeig_mir(130) |
| + | print() |
| + | </run> |
| + | |
| + | |
| + | |
| + | <code id="607d1db4c9bb0"> |
| + | class konvoi: |
| + | def __init__(self, id): |
| + | self.id = id |
| + | self.places = places |
| + | |
| + | def konvoi_macher(id): |
| + | number = 0 |
| + | while True: |
| + | yield stream(make_infinite_bus(id + str(number))) |
| + | number += 1 |
</code> | |
| + | |
| + | <run id="607d1db4c9f47"> |
| + | print("KONVOI") |
| + | k1 = stream(konvoi_macher("MH")) |
| + | k1.zeig_mir(6, 4, True) |
| + | print() |
| + | k1.zeig_mir(6, 4, False) |
| + | </run> |
| + | |
| + | <code id="607d1db4c9c2e"> |
| + | import math |
| + | |
| + | def konvoi_kette(konvoi): |
| + | def get_x_y(z): |
| + | w = math.floor((math.sqrt(8*z + 1) - 1) / 2) |
| + | y = z - w*(w + 1) / 2 |
| + | x = w - y |
| + | return int(x), int(y) |
| + | |
| + | |
| + | def make_konvoi_kette(konvoi): |
| + | n = 0 |
| + | ret = [] |
| + | while True: |
| + | x, y = get_x_y(n) |
| + | yield konvoi.zeig_mir(x + 1, y + 1, False)[x][y] |
| + | n += 1 |
| + | return stream(make_konvoi_kette(konvoi)) |
| + | </code> |
| + | |
| + | |
| + | <run id="607d1db4c9fb7"> |
| + | kk = konvoi_kette(k1) |
| + | |
| + | hilberthotel.gehe_in_gerade() |
| + | hilberthotel.check_in(stream(kk)) |
| + | hilberthotel.zeig_mir(42) |
| + | </run> |
| + | |
| + | <run id="607d2ce4bbdf8"> |
| + | hilberthotel.zeige_zimmer(234) |
| + | </run> |