Zum Hauptinhalt springen
🔒 Vorschaumodus. Die ersten fünfzehn Foundations-Lektionen sind kostenlos; diese hier ist Pro. Starte einen 7-Tage-Trial, um den Editor, AI-Hinweise und den Rest des Lehrplans freizuschalten. Karte erforderlich, jederzeit im Dashboard kündbar.7-Tage-Trial starten →
← KurseSystem Design for Python JuniorsModul 1 · Grundlagen des SystemdesignsLoad Balancerpredict6 / 105
+75 XP
Aufgabe
📝 **Aufgabe:** Sagen Sie die vier Zeilen voraus, die das Snippet ausgibt. Zwei Algorithmen × Prüfanfragen; Verfolgen Sie jeden Pick. 📋 Implementieren Sie die obige Funktion. Tests laufen automatisch ab. 💡 **Hinweis:** Lesen Sie die Theorie noch einmal, wenn Sie nicht weiterkommen.
Sage die Ausgabe vorher

Lies den Code sorgfältig

apps = ["app1", "app2", "app3"]

# Round-robin: cycle through apps in order.
class RoundRobin:
    def __init__(self, apps):
        self.apps = apps
        self.i = 0
    def pick(self, ip):
        choice = self.apps[self.i % len(self.apps)]
        self.i += 1
        return choice

# IP-hash: same ip ALWAYS lands on the same app — sticky sessions.
# We use sum(ord(c)) instead of hash() so the answer is reproducible
# across Python versions (CPython hash() is randomised per process).
class IPHash:
    def __init__(self, apps):
        self.apps = apps
    def pick(self, ip):
        return self.apps[sum(ord(c) for c in ip) % len(self.apps)]

rr = RoundRobin(apps)
print(rr.pick("10.0.0.1"))    # request 1 — cycle position 0
print(rr.pick("10.0.0.2"))    # request 2 — cycle position 1

ih = IPHash(apps)
print(ih.pick("10.0.0.5"))    # same IP twice — should pin
print(ih.pick("10.0.0.5"))

# What does this print? Type your prediction.

Was wird das Programm ausgeben? Schreib hier:

💬 Diskussion

Sei der erste — stelle eine Frage oder teile einen Tipp.
Anmelden um an der Diskussion teilzunehmen. Lesen ist kostenlos.
Diskussion wird geladen…