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 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 solution(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.

Test cases

— Python cases —
Input:
solution.solution(“wrw blf hvv ozhg mrtsg’h vkrhlwv?”)
Output:
did you see last night’s episode?

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

Python Solution

<span>def</span> <span>decode</span><span>(</span><span>char</span><span>):</span>
<span>if</span> <span>char</span><span>.</span><span>islower</span><span>():</span>
<span>return</span> <span>chr</span><span>(</span><span>ord</span><span>(</span><span>'a'</span><span>)</span> <span>+</span> <span>ord</span><span>(</span><span>'z'</span><span>)</span> <span>-</span> <span>ord</span><span>(</span><span>char</span><span>))</span>
<span>return</span> <span>char</span>
<span>def</span> <span>solution</span><span>(</span><span>x</span><span>):</span>
<span>return</span> <span>""</span><span>.</span><span>join</span><span>([</span><span>decode</span><span>(</span><span>char</span><span>)</span> <span>for</span> <span>char</span> <span>in</span> <span>x</span><span>])</span>
<span>def</span> <span>decode</span><span>(</span><span>char</span><span>):</span>
    <span>if</span> <span>char</span><span>.</span><span>islower</span><span>():</span>
        <span>return</span> <span>chr</span><span>(</span><span>ord</span><span>(</span><span>'a'</span><span>)</span> <span>+</span> <span>ord</span><span>(</span><span>'z'</span><span>)</span> <span>-</span> <span>ord</span><span>(</span><span>char</span><span>))</span>
    <span>return</span> <span>char</span>

<span>def</span> <span>solution</span><span>(</span><span>x</span><span>):</span>
    <span>return</span> <span>""</span><span>.</span><span>join</span><span>([</span><span>decode</span><span>(</span><span>char</span><span>)</span> <span>for</span> <span>char</span> <span>in</span> <span>x</span><span>])</span>
def decode(char): if char.islower(): return chr(ord('a') + ord('z') - ord(char)) return char def solution(x): return "".join([decode(char) for char in x])

Enter fullscreen mode Exit fullscreen mode

原文链接:Foobar: I Love Lance & Janice

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
You must learn a new way to think before you can master a new way to be.
在掌握新方法之前,你必须要先换一种思考方法
评论 抢沙发

请登录后发表评论

    暂无评论内容