Skip to content

Instantly share code, notes, and snippets.

@sahglie
Last active October 4, 2022 16:34
Show Gist options
  • Save sahglie/32728c38ac5c4d533b7bf6f8d77cf946 to your computer and use it in GitHub Desktop.
Save sahglie/32728c38ac5c4d533b7bf6f8d77cf946 to your computer and use it in GitHub Desktop.
programming interview question

Instructions

You are going to write a function that can validate a chart-string identification number. A chart-string number normally contain dashes and looks like: 3-598-21508-8

Background

The chart-string format is 9 digits (0 to 9) plus one check character (either a digit or an X only). In the case the check character is an X, this represents the value '10'. These may be communicated with or without hyphens. In addition to conforming to the above format, a chart-string must also be validated by the following formula:

(x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0

If the result is 0, then it is a valid chart-string, otherwise it is invalid.

Example

Let's take the chart-string 3-598-21508-8. We plug it in to the formula, and get:

(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0

Since the result is 0, this proves that our chart-string is valid.

Task

Given a string the program should check if the provided string is a valid chart-string. Putting this into place requires some thinking about preprocessing/parsing of the string prior to calculating the check digit for the chart-string.

The program should be able to verify chart-string both with and without separating dashes.

Examples

Valid chart-strings

  • "3-598-21508-8"
  • "3-598-21507-X"
  • "3598215088"
  • "359821507X"

Invalid chart-strings

  • "3-598-21508-9"
  • "3-598-21507-A"
  • "3-598-P1581-X"
  • "3-598-2X507-9"
  • "359821507"
  • "3598215078X"
  • "00"
  • "3-598-21507"
  • "3-598-21515-X"
  • ""
  • "134456729"
  • "3132P34035"
  • "98245726788"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment