两轮,一轮design一轮coding
Design:
PC Game Store and Launcher
Design the backend architecture for a Steam-like PC game store and desktop launcher.
You can think of the system as a combination of:
- An e-commerce store for browsing and purchasing games.
- A digital entitlement system that tracks which games each user owns.
- A large-file ign和high level design全部cover,再开始deep dive。
Coding:
Elevator scheduling
Part 1.
Part 2.
还有Part 3,但我在Part 2就没debug完,就没看到part 3了。有代码模板:
models.py:
- from dataclasses import dataclass, field
- # NB: A floor/level is 0-indexed for coding simplicity even though IRL we usually
- # refer to the first floor as floor 1. Keep that in mind for debugging / printing.
- @dataclass
- class Person:
- id: str
- """Just an identifier of an individual person."""
- src: int
- """The floor the person started on when they called the elevator."""
- dst: int
- """The floor the person is trying to get to."""
- def __str__(self) -> str:
- return f"{self.id}({self.src}->{self.dst})"
- @dataclass
- class Floor:
- level: int
- """Which level does this floor represent?"""
- people: list[Person] = field(default_factory=list)
- """The people on this floor currently waiting for an elevator."""
- @dataclass
- class Elevator:
- """You can add/remove/modify fields as you'd like here."""
- id: str
- """Just an identifier of an individual elevator."""
- curr_floor: Floor
- """The elevator's current floor."""
- passengers: list[Person] = field(default_factory=list)
- """The people in this elevator right now."""
- direction: int = 1
- """
- The elevator's direction.
- 1 is up, -1 is down, but you can change this as you'd like or use some other
- convention.
- """
- def __str__(self) -> str:
- passengers_str = ",".join([str(person) for person in self.passengers])
- match self.direction:
- case 0:
- dir_str = "↔"
- case 1:
- dir_str = "↑"
- case -1:
- dir_str = "↓"
- case _:
- raise Exception(f"Unknown direction {self.direction}")
- return f"{dir_str}[{passengers_str}]"
- @dataclass
- class Building:
- elevators: list[Elevator]
- @property
- def elevator(self) -> Elevator:
- """
- For parts 1-3, there is only 1 elevator, so use this accessor for convenience.
- """
- return self.elevators[0]
- floors: list[Floor]
- @property
- def num_floors(self) -> int:
- return len(self.floors)
- @dataclass
- class _World:
- building: Building
- tick_to_calls: dict[int, list[Person]]
- tick: int = 0
复制代码 loop.py- from typing import Callable
- from animation import animate
- from models import Building, _World
- def _is_simulation_over(world: _World) -> bool:
- if world.tick <= max(world.tick_to_calls.keys()):
- return False
- for elevator in world.building.elevators:
- if len(elevator.passengers) > 0:
- return False
- for floor in world.building.floors:
- if len(floor.people) > 0:
- return False
- return True
- def run_simulation(
- world: _World,
- do_tick_fn: Callable[[Building], None],
- debug: bool = False,
- ) -> _World:
- """
- Runs the elevator simulation until complete.
- """
- while True:
- for person_calling in world.tick_to_calls[world.tick]:
- world.building.floors[person_calling.src].people.append(person_calling)
- if debug:
- animate(world)
- do_tick_fn(world.building)
- world.tick += 1
- print("current level:", world.building.elevators[0].curr_floor.level)
- if _is_simulation_over(world):
- break
- if debug:
- animate(world)
- return world
复制代码 part1.py, part2.py- from models import Building
- def do_tick(building: Building) -> None:
- pass
复制代码
|