CCC '04 J4 - Simple Encryption

View as PDF

Submit solution

Points: 5
Time limit: 2.0s
Memory limit: 64M

Problem type
Canadian Computing Competition: 2004 Stage 1, Junior #4

One of the simplest ways of coding a message is to do a letter shift.

For example, if you shift the letters in the original message by 5 then A in your original message becomes F in the coded message. (B \to G, C \to H, …, T \to Y, U \to Z, V \to A, …, Z \to E). To decode the message, you simply need to shift back by the same number.

A slightly trickier encryption uses a keyword to determine the amount of the shift. Suppose you were using the keyword ACT. To encode the message, you take the original message, remove everything but the alphabetic characters, and form the message into a block that has the same width as the keyword. Here is a sample message to encrypt:

BANANA & PEEL

The blocked version of the message is shown below with the keyword ACT as a header.

ACT
 
BAN
ANA
PEE
L

Now, the message is encoded using a letter shift. However, this time it is not a uniform shift, it will depend upon the keyword letter at the top of the column. If the letter at the top of the column is an A, then the letters in that column are not shifted. If the letter is a B, then the letters in that column shift by 1, and so on. In the example, the letters in the third column will shift by 19 since the T is the 20th letter of the alphabet.

The encoded message is:

ACT
 
BCG
APT
PGX
L

You will write a program that will accept a keyword and a string to be encoded. The keyword will never have more than 6 characters. The message will always be given in all uppercase characters.

Input Specification

The first line of input consists of the keyword. The second line of the input is the message to be encoded. The keyword length will never exceed 6 characters. The total message length also will never exceed 60 characters.

Output Specification

Output the encoded message on a single line.

Sample Input 1

ACT
BANANA & PEEL

Sample Output 1

BCGAPTPGXL

Sample Input 2

TRICKY
I LOVE PROGRAMMING!

Sample Output 2

BCWXONKFOTKKFZVI

CCC problem statements in large part from the PEG OJ


Comments


  • 1
    Tommy_Shan  commented on Aug. 28, 2021, 6:08 p.m. edit 3

    Python's ord ("letter(upcase)") with chr(The number after ord(or any change))is a good choice for this question.

    >>> ord("A")
    65
    >>> chr(65)
    "A"

  • 1
    Amateur360  commented on June 5, 2021, 2:28 p.m. edited

    Here are a few tips:

    • To shift letters, change ASCII values. Create a variable to shift letters.
    • To make the block/table with the words, enter the characters row by row.
    • Do not print out spaces in the encrypted word.

  • 0
    Summation  commented on May 21, 2021, 10:56 a.m. edited

    Does anyone know why I can't pass test case #5? All the other ones seem to be fine, but there is a problem with the last test case and I can't figure out what's wrong. Any help is appreciated.


    • 3
      Badmode  commented on May 21, 2021, 12:18 p.m.

      Try this test case:

      HTANQI
      THANKS, THANKS, THANKS.

      Output should be:

      AAAAAAAAAAAAAAAAAA

  • 0
    AKKI67  commented on April 11, 2021, 2:53 p.m.

    I am not getting test cases #2 and #3. Can anyone help me find out what is wrong with my code. I tested it with the sample inputs and seemed to work fine, but it isn't working.


  • 0
    Tintin  commented on Jan. 3, 2021, 5:30 p.m.

    Is the code supposed to encrypt values like exclamation marks in the sample input 2?


    • 2
      Air  commented on Jan. 3, 2021, 11:27 p.m.

      From the problem description:

      To encode the message, you take the original message, remove everything but the alphabetic characters, and [...]


  • 14
    Computer_Genius  commented on July 7, 2020, 12:41 p.m. edit 2

    The letter-number mappings, as a Python dictionary:

    {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E', 6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J', 11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O', 16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T', 21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y', 26: 'Z', 'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10, 'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}

    • 2
      Badmode  commented on Nov. 20, 2020, 10:11 p.m. edited

      You can also just do ord(letter) - 96. This brings the letter 'a' to 1 - 'b' to 2 - and so on.


      • 0
        rasput  commented on July 17, 2021, 2:09 p.m.

        You can also just do ord(letter) - ord("a") + 1 or ord(letter) - ord("A") + 1


  • 0
    Orion222  commented on April 29, 2020, 8:17 p.m.

    if i have completed this problem, how can i check other people's submissions?


    • 1
      thealt  commented on April 30, 2020, 5:43 p.m.

      Click on all submissions or best submissions. Choose one, click view and then view source.


  • 2
    mpasc1  commented on Jan. 10, 2020, 2:11 p.m.

    Remember ascii values are a thing.


  • 3
    Kirito  commented on June 1, 2016, 9:21 p.m.

    If the letter at the top of the column is an "A", the the letters in that column are not shifted.

    (As pointed out by d on his profile page)


    • 2
      Phoenix1369  commented on June 1, 2016, 9:50 p.m.

      Thank you for noticing, and thanks to d as well. The problem description has been fixed.

      remove everthing but the alphabetic characters


  • 2
    odaniel  commented on Jan. 2, 2016, 2:28 a.m.

    The sample input contains a blank line between the two lines of input; the actual input does not.


    • 2
      cheesecake  commented on Jan. 2, 2016, 12:10 p.m.

      Thank you for noticing, sample inputs have been fixed.