Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 86 ยท Series, Coding and Relations

Coding-Decoding

Crack coding-decoding questions: letter shifting, opposite letters, number and symbol codes, sentence codes and conditional rules, with a runnable encoder.

In a coding-decoding question, a word, number or sentence has been turned into a different form by a hidden rule. You see one or two examples of the rule at work, and you must apply it to a new input (encoding) or work backwards from a code to the original (decoding). The rule is never random. It is one of a short list: shift the letters, mirror them, reverse them, replace them with numbers or symbols, or swap whole words.

Success is mostly about method. You compare the input and the output letter by letter, write the alphabet positions, and test the likely rules in order. This lesson uses the position table from Letter and Alphanumeric Series, so revise that first if A = 1 to Z = 26 and "opposite = 27 โˆ’ p" are not automatic yet.

How to attack a code

flowchart TB
    A["Compare lengths"] --> B{"Same length?"}
    B -->|Yes| C["Write positions of both words"]
    B -->|No| D["Look for sums, products, or dropped letters"]
    C --> E{"Same difference at every place?"}
    E -->|Yes| F["Fixed shift"]
    E -->|No| G{"Differences add to 27?"}
    G -->|Yes| H["Opposite letters"]
    G -->|No| I["Try reversal, growing shifts or conditions"]

Compare lengths and positions, then test the rules from simple to complex.

Type 1: letter shifting

Every letter moves the same number of places in the alphabet.

If BOARD is coded as DQCTF, how is CAT coded? Compare B and D (+2), O and Q (+2), A and C (+2), R and T (+2), D and F (+2). The rule is +2, so CAT becomes ECV (3 + 2 = 5 is E, 1 + 2 = 3 is C, 20 + 2 = 22 is V).

Shifts can also go backwards, wrap around the end of the alphabet (Y + 3 gives B), or depend on the letter's place in the word (+1 for the first letter, +2 for the second, and so on). In that last case, BOARD would become C, Q, D, V, I, which is CQDVI.

Type 2: opposite and mirror letters

Each letter is replaced by its mirror image: A becomes Z, B becomes Y, and so on. Opposites add up to 27, so the opposite of the letter at position p is at 27 โˆ’ p. CAT becomes XZG (C is 3, so 24 is X; A is 1, so 26 is Z; T is 20, so 7 is G).

If every difference between input and output positions is different, but each pair of positions sums to 27, the rule is the opposite letter.

Type 3: reversal and combined rules

Test whether the code is simply the word backwards: TAC for CAT. Then test combinations, which are common in harder papers: reverse the word, then shift each letter by one. For TEA, the reverse is AET, and shifting by +1 gives BFU.

Whenever you find a rule that explains the first letter but fails on the second, think about a combination or a rule that depends on position.

Type 4: number codes

Letters can be replaced by numbers in several ways.

  • Position codes: A = 1, B = 2, and so on. CAT becomes 3-1-20.
  • Sum of positions: CAT becomes 3 + 1 + 20 = 24. DOG is 4 + 15 + 7 = 26.
  • Reverse positions: A = 26, B = 25 (the opposite-letter idea applied to numbers).
  • Custom tables: the question gives its own table, such as "A = 5, B = 7, C = 9", so use that and nothing else.

Be careful with codes written without separators. The number 3120 could read as 3-1-20 (CAT) or as 31-20, and the question usually makes it clear.

Type 5: symbol and operator codes

A symbol stands for a letter, a word, or an arithmetic operation. In operator codes, you substitute first and apply the normal order of operations after.

If P means ร—, Q means รท, R means +, and S means โˆ’, what is 12 P 3 R 4 Q 2 S 5? Substitute: 12 ร— 3 + 4 รท 2 โˆ’ 5. Multiplication and division come first: 36 + 2 โˆ’ 5 = 33.

Another version swaps meanings of words: "If red is called blue, blue is called green, and green is called yellow, what is the colour of grass?" Grass is green in reality, and green is called yellow in the code, so the answer is yellow. Always use the code's name for the real object.

Type 6: conditional codes

The rule changes with a condition on the input. For example: "If the word has an even number of letters, reverse it; if odd, shift each letter by +1." Then TOP (3 letters) becomes UPQ, and PLAY (4 letters) becomes YALP. Other common conditions are "if the first letter is a vowel" or "vowels become * and consonants shift by +1" (so CAT becomes D*U).

Run the encoder

This script contains ten of the rules above. It encodes four words under each rule, so you can compare how the rules change the same input.

JS Coding rule encoder

Press Run (or Ctrl + Enter) to see the console output here.

No output.

You can add your own rule by writing a new function in the schemes object. Remember the position formula: a letter at position p moves to p + k, and positions wrap around after 26.

Decoding and sentence codes

Decoding reverses the rule. If CAT is encoded by +2, decode by โˆ’2. When you do not know the shift, you can try all 25 and look for a readable word. KHOOR gives HELLO at a shift of 3. This also shows why code-breaking is not difficult: a single shift has only 25 possibilities.

In sentence codes, you get several coded sentences and must find which code word means which real word. The method is to find a word that appears in two sentences and a code that appears in both codes.

Suppose "red fox runs" is 5 9 2, "fox jumps high" is 9 4 7 and "high red moon" is 7 5 3.

  • "red fox runs" and "fox jumps high" share fox, and the codes share 9, so fox = 9.
  • "red fox runs" and "high red moon" share red, and the codes share 5, so red = 5, which leaves runs = 2.
  • "fox jumps high" and "high red moon" share high, with code 7, so high = 7, leaving jumps = 4 and moon = 3.
JS Decode by brute force and solve a sentence code

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first loop prints all 25 candidate shifts and flags the one that is readable. The second finds the shared words automatically.

Common mistakes

  • Starting with the whole word. Compare one letter first, then confirm on every other letter.
  • Forgetting the wrap-around. Y + 3 is B, not a symbol after Z.
  • Mixing up direction. Find whether the code moves the letter forward or back, and apply the same direction for the new word.
  • Ignoring position-dependent rules. If the differences change from letter to letter, check the pattern of the differences.
  • Using real meanings in word-swap codes. Follow the code's own names.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. If BOARD is coded as DQCTF, how is CAT coded?

    Every letter moves forward by 2: B to D, O to Q, A to C, R to T, D to F. So C to E, A to C, T to V give ECV.
  2. 2. In a code the letters are replaced by their opposite letters (A with Z, B with Y). How is CAT written?

    The opposite of a letter at position p is at position 27 - p. C (3) becomes 24, which is X. A (1) becomes 26, which is Z. T (20) becomes 7, which is G.
  3. 3. A rule says 'reverse the word, then shift every letter by +1'. What does TEA become?

    The reverse of TEA is AET. Shifting A, E, T by +1 gives B, F, U, so BFU.
  4. 4. If P means multiplication, Q means division, R means addition and S means subtraction, what is the value of 12 P 3 R 4 Q 2 S 5?

    After substitution the expression is 12 x 3 + 4 / 2 - 5. Multiplication and division first give 36 + 2 - 5 = 33.
  5. 5. The code word KHOOR was made by shifting every letter of the original word forward by 3. What was the original word?

    Shift each letter back by 3: K to H, H to E, O to L, O to L, R to O. The word is HELLO.

Summary

  • Compare input and output letter by letter, and write the alphabet positions.
  • Common rules: fixed shift, shifts that grow with position, opposite letters (sum 27), reversal, and combinations.
  • Number codes use positions, sums or a table given in the question.
  • In operator codes, substitute first, then apply the normal order of operations.
  • Conditional codes change the rule according to the word's length or letters.
  • In sentence codes, find words shared by two sentences and match the shared code.

Next: Analogies and Odd One Out, where you find the relationship between pairs and spot the item that does not belong.