Naar hoofdinhoud
🔒 Voorbeeldmodus. De eerste vijftien Foundations-lessen zijn gratis; deze is Pro. Start een 7-daagse trial om de editor, AI-hints en de rest van het curriculum te ontgrendelen. Kaart vereist, op elk moment opzegbaar in Dashboard.Start 7-daagse trial →
← CursussenSystem Design for Python JuniorsModule 1 · Basisprincipes van systeemontwerpLoadbalancerspredict6 / 105
+75 XP
Opdracht
📝 **Taak:** Voorspel de vier regels die het fragment afdrukt. Twee algoritmen × sondeverzoeken; traceer elke keuze. 📋 Implementeer bovenstaande functie. Tests worden automatisch uitgevoerd. 💡 **Hint:** Herlees de theorie als je vastloopt.
Voorspel uitvoer

Lees de code zorgvuldig

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.

Wat zal het programma uitprinten? Schrijf hier:

💬 Discussie

Wees de eerste — stel een vraag of deel een tip.
Log in om mee te doen aan de discussie. Lezen is gratis.
Discussie laden…