LEARN VOID / DATA & BEHAVIOR

Dictionaries

Associate keys and values while preserving insertion order.

dictionaries.voidDownload source ↓
func entry(value: i32) -> i32:
    var stats = {
        "health": 100,
        "damage": 15,
        "health": 101
    }
    var alias = stats
    stats["health"] = value
    stats["speed"] = 4
    if not stats.contains("health") or stats.contains("missing"):
        return -100
    var order = ""
    for item, key in stats:
        order += key
    if order != "healthdamagespeed":
        return -200
    if not stats.remove("damage") or stats.remove("damage"):
        return -300
    stats["damage"] = 9
    order = ""
    for item, key in reverse stats:
        order += key
    if order != "damagespeedhealth":
        return -400
    var answer = alias["health"] + stats.count as i32
    alias.clear()
    alias.clear()
    if stats.count != 0:
        return -500
    return answer
Input 5Expected result 8

Use a typed mapping

A dictionary literal associates keys with values. Repeating a key in this example replaces its value without moving its original insertion position. Adding a new key appends a new entry.

Work with membership and order

contains checks for a key, remove reports whether it removed one, and clear empties the dictionary. Iteration uses for item, key in stats; reverse visits the current insertion order backwards.

Share the mapping intentionally

alias and stats refer to the same dictionary. Updating a value through one view is visible through the other. The example saves input 5 plus a count of 3 before clearing the dictionary, then returns 8. Do not assume proposed get or pop APIs exist.