Hello everybody!
Michael here, and as I previously promised, here’s part 2 of my five-year blog anniversary post (after all, what fun it is to split the 5-year anniversary letter into two posts).
My Answer To Michael’s Five-Year Coding Challenge
Also, as I previously promised on the last post, I will discuss my solution for Michael’s Five-Year Coding Challenge. I know I said you can use any programming language you like for this challenge (as long as your code follows certain criteria outlined in my previous post https://michaelsprogrammingbytes.com/2023/06/13/the-glorious-five-year-plan-part-one/), but here’s my approach using Python!
secretCode = {'Z': 'A', 'Y': 'B', 'X': 'C', 'W': 'D', 'V': 'E', 'U': 'F', 'T': 'G', 'S': 'H', 'R': 'I', 'Q': 'J', 'P': 'K',
'O': 'L', 'N': 'M', 'M': 'N', 'L': 'O', 'K': 'P', 'J': 'Q', 'I': 'R', 'H': 'S', 'G': 'T', 'F': 'U', 'E': 'V',
'D': 'W', 'C': 'X', 'B': 'Y', 'A': 'Z'}
encodedString = """Gszmp blf gl zoo nb ivzwvih uli urev dlmwviufo bvzih. R dlfowm'g szev pvkg gsrh yolt ifmmrmt uli zh olmt zh R wrw drgslfg blfi dlmwviufo hfkklig. slkv blf ovzimvw hlnvgsrmt zolmt gsv dzb gsvhv ozhg urev bvzih-zmw, svb, nzbyv blf tzrmvw z olev lu kiltiznnrmt (zmw kviszkh mvd qly hprooh) rm gsv kilxvhh. Gszg dlfow xvigzrmob nzpv nv z evib szkkb dirgvi. Sviv'h gl nzmb, nzmb nliv bvzih lu xlwrmt gltvgsvi! Nrxszvo"""
punctuation = ['\'', '.', '!', '(', ')', '-']
decodedString = ''
for e in encodedString:
for k, v in secretCode.items():
if e == k:
decodedString += v
elif e.lower() == k.lower():
decodedString += v.lower()
if e == ' ':
decodedString += ' '
elif e in punctuation:
decodedString += e
print(decodedString)
As you can see, I solved this coding challenge with 20 simple lines of Python code (not including the output). How did I accomplish this? Here are 10 things I kept in mind when solving my own puzzle:
- I used the
secretCodedictionary to set my reverse substitution cipher (HINT: even though there are both uppercase and lowercase letters, you won’t need two separate dictionaries for the cipher-I’ll explain why). - I created a list containing all of the punctuation in this message-you’ll see why it’s important later. The list is aptly named
punctuation. - I used the
decodedStringvariable to store the decoded message-I personally thought it was more convinient than figuring out how to replace all the characters inencodedStringone by one. - I created a nested for-loop to iterate both through the characters in
encodedStringand the items (that is, both keys and values) in thesecretCodedictionary. - In the nested loop where I iterate through the items of the
secretCodedictionary (for k, v in secretCode.items()), I check if each character in theencodedStringstring equals the current corresponding key in thesecretCodedictionary and if so, I append the corresponding value to thedecodedStringstring. - Remember how I said that even though my secret message contains both upper and lower-case letters you won’t need to create two dictionaries? Well, the statement
elif e.lower() == k.lower()handles this scenario in just two lines of code by checking to see if the lowercase version of the character inencodedStringequals the lowercase version of the corresponding key and if so, I append the lowercase corresponding value to thedecodedStringstring. - How would I handle spaces and punctuation in the
encodedString? Well, in the case of spaces, if a character inencodedStringis a space, I append it to thedecodedStringjust as I did with the letters. - The same logic applies for any punctuation in
encodedString, but this time, I check to see if a character equals any element in thepunctuationlist that I mentioned in item #2. - One thing I kept in mind when checking for spaces and punctuation in
encodedString-I kept theif-elifstatement pair OUTSIDE of the dictionary loop because if I placed those statements inside that loop, I would’ve ended up with a ton of spaces and punctuation. However, placing this code outside of the dictionary loop ensures that I end up with the correct amount of spaces and punctuation. - Last but not least, I printed the
decodedStringmessage. Without further ado, here it is:
Thank you to all my readers for five wonderful years. I wouldn't have kept this blog running for as long as I did without your wonderful support. hope you learned something along the way these last five years-and hey maybe you gained a love of programming (and perhaps new job skills) in the process. That would certainly make me a very happy writer. Here's to many many more years of coding together! Michael
Trust me, dear readers, I mean every word of this. I wouldn’t kept this blog running as long as I did without you!
One More Note
I know I’ve emphasized this over these last two posts, but thank you, thank you, thank you loyal readers for reading my blog for the last five years. Hopefully you’ll keep coming back for more, because boy do I have several more years of great coding content I can provide (and you bet it’ll still be good when I hit year 10 and beyond)!
However, there is someone I wanted to acknowledge on this five-year anniversary. Rather, a furry friend I’d like to mention.
His name is Simba (or Orange Boy) and he is my fluffy orange cat who certainly helped me during the blog’s early days (and yes, he was there when I wrote the welcome post). Here he is on the night of September 17, 2018, likely looking over me as I wrote another post (or searching for a post college job, as I was in the thick of job hunting in fall 2018). He is certainly a good fluffy boy!

Also, here’s Simba with Marbles, his sister (the brown cat aka Pretty Girl), who accompanied me during writing every now and then:
The kitties on Christmas morning 2017-pre blog days.
To many more years of developing together,
Michael