Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-python/699c8e045ee7cb94ed2322d8.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
699c8e045ee7cb94ed2322d8 Challenge 217: Captured Chess Pieces 29 challenge-217

--description--

Given an array of strings representing chess pieces you still have on the board, calculate the value of the pieces your opponent has captured.

In chess, you start with 16 pieces:

Piece Abbreviation Quantity Value
Pawn "P" 8 1
Rook "R" 2 5
Knight "N" 2 3
Bishop "B" 2 3
Queen "Q" 1 9
King "K" 1 0
  • The given array will only contain the abbreviations above.
  • Any of the 16 pieces not included in the given array have been captured.
  • Return the total value of all captured pieces, unless...
  • If the King has been captured, return "Checkmate".

--hints--

get_captured_value(["P", "P", "P", "P", "P", "P", "R", "R", "N", "B", "Q", "K"]) should return 8.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["P", "P", "P", "P", "P", "P", "R", "R", "N", "B", "Q", "K"]), 8)`)
}})

get_captured_value(["P", "P", "P", "P", "P", "R", "B", "K"]) should return 26.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["P", "P", "P", "P", "P", "R", "B", "K"]), 26)`)
}})

get_captured_value(["K", "P", "P", "N", "P", "P", "R", "P", "B", "P", "N", "B"]) should return 16.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["K", "P", "P", "N", "P", "P", "R", "P", "B", "P", "N", "B"]), 16)`)
}})

get_captured_value(["P", "Q", "N", "P", "P", "B", "K", "P", "R", "R", "P", "P", "B", "P"]) should return 4.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["P", "Q", "N", "P", "P", "B", "K", "P", "R", "R", "P", "P", "B", "P"]), 4)`)
}})

get_captured_value(["P", "K"]) should return 38.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["P", "K"]), 38)`)
}})

get_captured_value(["N", "P", "P", "B", "K", "P", "Q", "N", "P", "P", "R", "R", "P", "P", "P", "B"]) should return 0.

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["N", "P", "P", "B", "K", "P", "Q", "N", "P", "P", "R", "R", "P", "P", "P", "B"]), 0)`)
}})

get_captured_value(["N", "P", "P", "B", "P", "R", "Q", "P", "P", "P", "B"]) should return "Checkmate".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_captured_value(["N", "P", "P", "B", "P", "R", "Q", "P", "P", "P", "B"]), "Checkmate")`)
}})

--seed--

--seed-contents--

def get_captured_value(pieces):

    return pieces

--solutions--

def get_captured_value(pieces):
    if "K" not in pieces:
        return "Checkmate"

    values = {"P":1, "R":5, "N":3, "B":3, "Q":9, "K":0}
    remaining = "PPPPPPPPRRNNBBQK"

    for p in pieces:
        remaining = remaining.replace(p, "", 1)

    return sum(values[p] for p in remaining)