I Love Lance & Janice

Well, here is another one of those Google code challenges that popped up – a pretty easy challenge that did not take all that long before I submitted a solution that worked in both PyCharm and Idle36. But it didn’t work in the Google editor. I must have beaten my head on the wall for about 15 minutes before I read the “constraints” file and found out:

Python
======

Your code will run inside a Python 2.7.6 sandbox.

Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
foobar:~/i_love_lance_janice guest$ feedback

I sent apologetic feedback: “Sorry, I don’t do archaic languages.”

So if you might care, here is what you will see:
foobar:~/i_love_lance_janice

I Love Lance & Janice
=====================

You’ve caught two of your fellow minions passing coded notes back and forth – while they’re on duty, no less! Worse, you’re pretty sure it’s not job-related – they’re both huge fans of the space soap opera “Lance & Janice”. You know how much Commander Lambda hates waste, so if you can prove that these minions are wasting her time passing non-job-related notes, it’ll put you that much closer to a promotion.

Fortunately for you, the minions aren’t exactly advanced cryptographers. In their code, every lowercase letter [a..z] is replaced with the corresponding one in [z..a], while every other character (including uppercase letters and punctuation) is left untouched. That is, ‘a’ becomes ‘z’, ‘b’ becomes ‘y’, ‘c’ becomes ‘x’, etc. For instance, the word “vmxibkgrlm”, when decoded, would become “encryption”.

Write a function called answer(s) which takes in a string and returns the deciphered string so you can show the commander proof that these minions are talking about “Lance & Janice” instead of doing their jobs.

Languages
=========

To provide a Python solution, edit solution.py
To provide a Java solution, edit solution.java

Test cases
==========

Inputs:
(string) s = “wrw blf hvv ozhg mrtsg’h vkrhlwv?”
Output:
(string) “did you see last night’s episode?”

Inputs:
(string) s = “Yvzs! I xzm’g yvorvev Lzmxv olhg srh qly zg gsv xlolmb!!”
Output:
(string) “Yeah! I can’t believe Lance lost his job at the colony!!”

Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.

You will need basic navigation commands – a tiny google bash of sorts:

Use the following shell commands:

cd change directory [dir_name]
cat print file [file_name]
deleteme delete all of your data associated with foobar
edit open file in editor [file_name]
feedback provide feedback on foobar
less print a file a page at a time [file_name]
ls list directory contents [dir_name]
request request new challenge
status print progress
submit submit final solution file for assessment [file_name]
verify runs tests on solution file [file_name]

Keyboard help:

Ctrl + S save the open file [when editor is focussed]
Ctrl + E close the editor [when editor is focussed]

Toggle between the editor and terminal using ESC followed by TAB, then activate with ENTER.

That last line about toggling did not work for me.  Anyway here is a perfectly good solution for those living in the twenty-first century.  I’ll bet you can do better if you try.

def answer(s):
    forward_lc = "abcdefghijklmnopqrstuvwxyz"
    listbackward = []
    for letter in range(0, len(forward_lc)):
        listbackward.append(forward_lc[letter])
    listforward = listbackward.copy()
    listbackward.reverse()
    decoded_list=[]
    for letter in s:
        if ord(letter)>=97 and ord(letter) <= 122:
            index=listbackward.index(letter)
            decoded_list.append(listforward[index])
        else:
            decoded_list.append(letter)
    decoded_str="".join(decoded_list)
    return decoded_str

myanswer = answer("Yvzs! I xzm'g yvorvev Lzmxv olhg srh qly zg gsv xlolmb!!")
print(myanswer)