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 systeemontwerpSharding (horizontale verdeling)predict8 / 105
+100 XP
Opdracht
📝 **Taak:** Voorspel de uitvoer van 5 regels: de eerste 4 regels tonen shard:count voor shards 0..3 in de 4-shard-indeling, daarna één regel met door komma's samengevoegde zetten na opnieuw sharden naar 5 shards. 📋 Implementeer bovenstaande functie. Tests worden automatisch uitgevoerd. 💡 **Hint:** Herlees de theorie als je vastloopt.
Voorspel uitvoer

Lees de code zorgvuldig

from collections import Counter

USERS = ["u1", "u2", "u3", "u4", "u5", "u6", "u7", "u8"]

def shard_for(uid, n):
    return sum(ord(c) for c in uid) % n

def distribution(users, n):
    return Counter(shard_for(u, n) for u in users)

# 4-shard layout — print one line per shard.
d4 = distribution(USERS, 4)
for s in range(4):
    print(f"shard{s}:{d4[s]}")

# Re-shard to 5: how many users HOP to a different bucket?
moved = [u for u in USERS if shard_for(u, 4) != shard_for(u, 5)]
print("moved:" + ",".join(moved))

# 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…