Karaca’s Encryption

Annika Noren
3 min readNov 12, 2020

I can’t get enough of these Python code challenges

A red apple
Photo by an_vision on Unsplash

This week I continued on my binge of coding challenges from Edabit. There’s something relaxing about getting lost in a fun code challenge for a little while. The latest teaser was solving Karaca’s Encryption Algorithm:

Make a function that encrypts a given input with these steps:Input: “apple”Step 1: Reverse the input: “elppa”Step 2: Replace all vowels using the following chart:a => 0e => 1i => 2o => 2u => 3# “1lpp0”Step 3: Add “aca” to the end of the word: “1lpp0aca”Output: “1lpp0aca”

Taking it step-by-step, here’s the first version of my solution:

Then it was refactored to:

It worked fine; “apple” encrypted to “1lpp0aca” as expected.

Solution to code for “apple”
Solution for “apple”

Then I peeked at the other solutions and found they had used the string method translate() to solve the challenge. A challenge within a challenge for me — learn to use a new string method. Game on.

I started by reading about the string method translate() and the accompanying helper function str.maketrans(). Str.maketrans() returns a mapping table usable for the translate() method. Reading about str. maketrans() taught me about Unicode numbers, the syntax, and how to use translate() with a translation table or manual translation table.

The first of two methods I practiced was to create a translation table using the static method maketrans(). The syntax is:

string.maketrans(x,[y,[z]]) ) 

with y and z as optional arguments, and this is how it went:

The result:

Translation Table and Translated String

So, what was going on? All three arguments were supplied, so the a, b, and c from the firstString are mapped to the g, h, and i of the secondString. The thirdString is the removal string so the a and b are reset to no mapping (or none). In the string “abcdef” that will be translated, the a and b are removed, the c is mapped to i, nothing happened to def, and the result is idef.

The other way is to provide the translation table in the form of a dictionary for translate() to use. For example:

Again, the result was:

Original String and Translated String

Once I understood how translate() and maketrans() worked, I revisited the encryption algorithm. My first version:

And my fourth and final version (a one-line function!):

--

--