TheAlgorithms/Python · error · Exception

Duplicate symbol ({i})

Error message

Duplicate symbol ({i})

What it means

Raised by _plugboard() when the same letter appears more than once in the plugboard string. Each letter can be connected to at most one partner, so duplicates like 'PICTURES' containing two S's (or 'AABB'-style repeats) are rejected.

Source

Thrown at ciphers/enigma_machine2.py:153

        msg = f"Plugboard setting isn't type string ({type(pbstring)})"
        raise TypeError(msg)
    elif len(pbstring) % 2 != 0:
        msg = f"Odd number of symbols ({len(pbstring)})"
        raise Exception(msg)
    elif pbstring == "":
        return {}

    pbstring.replace(" ", "")

    # Checks if all characters are unique
    tmppbl = set()
    for i in pbstring:
        if i not in abc:
            msg = f"'{i}' not in list of symbols"
            raise Exception(msg)
        elif i in tmppbl:
            msg = f"Duplicate symbol ({i})"
            raise Exception(msg)
        else:
            tmppbl.add(i)
    del tmppbl

    # Created the dictionary
    pb = {}
    for j in range(0, len(pbstring) - 1, 2):
        pb[pbstring[j]] = pbstring[j + 1]
        pb[pbstring[j + 1]] = pbstring[j]

    return pb


def enigma(
    text: str,
    rotor_position: RotorPositionT,
    rotor_selection: RotorSelectionT = (rotor1, rotor2, rotor3),
    plugb: str = "",

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure every letter appears exactly once: use distinct pairs like 'POLA' or 'AMFILEV'
  2. Validate uniqueness first: len(set(pb)) == len(pb)
  3. Build settings programmatically from a disjoint-pairs structure

Example fix

# before
pb = 'PICTURES'  # S used twice

# after
pb = 'PICTURED'  # all letters distinct (example)
Defensive patterns

Strategy: validation

Validate before calling

assert len(set(pb)) == len(pb), 'each plugboard letter may appear once'

Type guard

def has_unique_symbols(pb: str) -> bool:
    pb = pb.replace(' ', '')
    return len(set(pb)) == len(pb)

Try / catch

try:
    pbdict = _plugboard(pb)
except Exception as exc:
    if "Duplicate symbol" in str(exc):
        raise ConfigError(f'plugboard letter reused: {exc}') from exc
    raise

Prevention

When it happens

Trigger: _plugboard('PICTURES') — S occurs twice; any string where a letter is used in two different pairs; self-pairs like 'PP'.

Common situations: Hand-composed settings that accidentally reuse a letter; concatenating pair lists that share a letter; converting a dict where two keys map to the same value.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/0dcb0616cb7890ef. Report an issue: GitHub.