# Generated from the published Markdown; run scripts/build_language_examples.py to refresh.
# Independent examples may reuse top-level names. Python remains the explanation language.


# topics/foundations/data-and-algorithms.md
def find_stop(route, target):
    for index, stop in enumerate(route):
        if stop == target:
            return index
    return None

route = ["KR", "MUS", "UTN", "MUS"]
print(find_stop(route, "MUS"))  # 1
print(find_stop(route, "CLM"))  # None

# topics/foundations/objects-and-references.md
stop = "Kent Ridge"
passengers = 12
stop = "Museum"
print(stop)  # Museum

# topics/foundations/objects-and-references.md
route_a = ["KR", "MUS"]
route_b = route_a
print(route_a is route_b)  # True

# topics/foundations/objects-and-references.md
x = ["KR", "MUS"]
y = ["KR", "MUS"]
z = x
print(x == y)  # True
print(x is y)  # False
print(x is z)  # True

# topics/foundations/objects-and-references.md
next_node = None
print(next_node is None)  # True

# topics/foundations/mutation-and-copying.md
record = ("KR", [12, 15])
record[1].append(18)
print(record)  # ('KR', [12, 15, 18])

# topics/foundations/mutation-and-copying.md
route_a = ["KR", "MUS"]
route_b = route_a
route_b.append("UTN")
print(route_a)  # ['KR', 'MUS', 'UTN']

route_b = ["CLM"]
print(route_a)  # ['KR', 'MUS', 'UTN']
print(route_b)  # ['CLM']

# topics/foundations/mutation-and-copying.md
def replace_route(route):
    route = ["Botanic Gardens"]

def extend_route(route):
    route.append("Botanic Gardens")

stops = ["Kent Ridge"]
replace_route(stops)
print(stops)  # ['Kent Ridge']
extend_route(stops)
print(stops)  # ['Kent Ridge', 'Botanic Gardens']

# topics/foundations/mutation-and-copying.md
routes = [["KR", "MUS"], ["KR", "UTN"]]
backup = routes.copy()
print(backup is routes)        # False
print(backup[0] is routes[0])  # True
backup[0].append("CLM")
print(routes[0])              # ['KR', 'MUS', 'CLM']

# topics/foundations/mutation-and-copying.md
from copy import deepcopy

original = [["KR", "MUS"], ["KR", "UTN"]]
independent = deepcopy(original)
independent[0].append("CLM")
print(original[0])     # ['KR', 'MUS']
print(independent[0])  # ['KR', 'MUS', 'CLM']

# topics/foundations/mutation-and-copying.md
def add_stop(stop, route=None):
    if route is None:
        route = []
    route.append(stop)
    return route

first = add_stop("KR")
second = add_stop("MUS")
print(first, second)  # ['KR'] ['MUS']
print(first is second)  # False

# topics/foundations/collections-and-classes.md
location = (1.2937, 103.7845)
latitude, longitude = location

passenger_counts = {"Kent Ridge": 12, "Museum": 7, "University Town": 18}
busy_stops = [
    name
    for name, count in passenger_counts.items()
    if count >= 10
]
print(busy_stops)  # ['Kent Ridge', 'University Town']

# topics/foundations/collections-and-classes.md
class ShuttleStop:
    def __init__(self, code: str, name: str) -> None:
        self.code = code
        self.name = name
        self._passengers = 0

    def board(self, number: int = 1) -> None:
        self._check_number(number)
        self._passengers += number

    def alight(self, number: int = 1) -> None:
        self._check_number(number)
        if number > self._passengers:
            raise ValueError("Not enough passengers")
        self._passengers -= number

    def _check_number(self, number: int) -> None:
        if type(number) is not int or number < 0:
            raise ValueError("Expected a nonnegative integer count")

    def __repr__(self) -> str:
        return (
            f"ShuttleStop({self.code!r}, {self.name!r}, "
            f"passengers={self._passengers})"
        )

museum = ShuttleStop("MUS", "Museum")
utown = ShuttleStop("UTN", "University Town")
museum.board(3)
utown.board(8)
museum.alight(1)
print(museum)  # ShuttleStop('MUS', 'Museum', passengers=2)
print(utown)   # ShuttleStop('UTN', 'University Town', passengers=8)

# topics/foundations/collections-and-classes.md
class RouteNode:
    def __init__(self, stop, next_node=None):
        self.stop = stop
        self.next = next_node

second = RouteNode(ShuttleStop("MUS", "Museum"))
first = RouteNode(ShuttleStop("KR", "Kent Ridge"), second)

current = first
while current is not None:
    print(current.stop.name)
    current = current.next
