{
  "format": "tokbit",
  "version": 1,
  "name": "Last match",
  "main": [
    {
      "id": "u1",
      "kind": "declare",
      "name": "numbers",
      "type": "list[int]",
      "init": "[4, 8, 15, 16, 23, 42, 8, 4]"
    },
    {
      "id": "u2",
      "kind": "declare",
      "name": "target",
      "type": "int"
    },
    {
      "id": "u3",
      "kind": "declare",
      "name": "position",
      "type": "int",
      "init": "-1"
    },
    {
      "id": "u4",
      "kind": "declare",
      "name": "i",
      "type": "int",
      "init": "len(numbers) - 1",
      "comment": "Starts at the last index and goes towards the start."
    },
    {
      "id": "u5",
      "kind": "output",
      "values": "\"List:\", numbers"
    },
    {
      "id": "u6",
      "kind": "input",
      "target": "target",
      "prompt": "Number to find:"
    },
    {
      "id": "u7",
      "kind": "while",
      "condition": "i >= 0 and position == -1",
      "body": [
        {
          "id": "u8",
          "kind": "if",
          "condition": "numbers[i] == target",
          "then": [
            {
              "id": "u9",
              "kind": "assign",
              "target": "position",
              "value": "i"
            }
          ],
          "else": []
        },
        {
          "id": "u10",
          "kind": "assign",
          "target": "i",
          "value": "i - 1"
        }
      ]
    },
    {
      "id": "u11",
      "kind": "if",
      "condition": "position == -1",
      "then": [
        {
          "id": "u12",
          "kind": "output",
          "values": "\"The number\", target, \"is not in the list.\""
        }
      ],
      "else": [
        {
          "id": "u13",
          "kind": "output",
          "values": "\"Last found at position\", position"
        }
      ]
    }
  ],
  "functions": [],
  "comment": "Solution to the task “The last match”: Find the last match instead of the first. Going through the list from the end is enough.",
  "screen": "fit"
}
