Problem 4

Recognize numbers shown on a digital LED display.

The number is encoded as follows:

    a
   ___
f |   |
  | g | b
   ---
  |   | c
e |___|
    d

For example, the number 2 is encoded as abged.

Data:

VALID_LETTERS

Specify the valid identifiers for the segments.

TO_NUMBER

Define the mapping: segments identifier 🠒 displayed number.

FROM_NUMBER

Define the inverse mapping of TO_NUMBER: number 🠒 segments identifier

Functions:

decode(text)

Decode the given text, representing segment identifiers, as a digit.

encode(number)

Encode the given number as segments identifiers of the LED display.

VALID_LETTERS = {'a', 'b', 'c', 'd', 'e', 'f', 'g'}

Specify the valid identifiers for the segments.

TO_NUMBER = {'abc': 7, 'abcdef': 0, 'abcdefg': 8, 'abcdfg': 9, 'abcdg': 3, 'abdeg': 2, 'acdefg': 6, 'acdfg': 5, 'bc': 1, 'bcfg': 4}

Define the mapping: segments identifier 🠒 displayed number.

FROM_NUMBER = {0: 'abcdef', 1: 'bc', 2: 'abdeg', 3: 'abcdg', 4: 'bcfg', 5: 'acdfg', 6: 'acdefg', 7: 'abc', 8: 'abcdefg', 9: 'abcdfg'}

Define the inverse mapping of TO_NUMBER: number 🠒 segments identifier

decode(text: str) int[source]

Decode the given text, representing segment identifiers, as a digit.

Requires
  • all(letter in VALID_LETTERS for letter in text)

    (only valid identifiers of line segments)

  • len(set(text)) == len(text)

    (only one entry per line segment)

Ensures
  • 0 <= result <= 9

encode(number: int) str[source]

Encode the given number as segments identifiers of the LED display.

Requires
  • 0 <= number <= 9

Ensures
  • number == decode(result)