Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active January 26, 2023 07:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ldong/9921248 to your computer and use it in GitHub Desktop.
Save ldong/9921248 to your computer and use it in GitHub Desktop.
Python Challenge walkthrough

Python Challenge

Click here to play Python Challenge

If you cannot solve this one, please stop reading right now.

K->M, O->M, E->G

The difference between A->B is 2

There're two ways to approach it.

A.

def convert(str):
  print chr(ord(str) + 2)
b = 'g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.'
for i in b:
  convert(i)

However as you can see, the output is messy because it also converted the white space and dots into other characters unintentionally.

i " h o p e " y o u " d i d n t " t r { n s l { t e " i t " | y " h { n d 0 " t h { t s " w h { t " c o m p u t e r s " { r e " f o r 0 " d o i n g " i t " i n " | y " h { n d " i s " i n e f f i c i e n t " { n d " t h { t ) s " w h y " t h i s " t e x t " i s " s o " l o n g 0 " u s i n g " s t r i n g 0 m { k e t r { n s * + " i s " r e c o m m e n d e d 0 " n o w " { p p l y " o n " t h e " u r l 0

B. Using this is much cleaner and neat. maketrans

from string import maketrans 
int = "abcdefghijklmnopqrstuvwxyz"
out = "cdefghijklmnopqrstuvwxyzab"
trans_int_out = maketrans(int, out)
print b.translate(trans_int_out)
### output
"i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url."

Like the output asked, apply translate on "map".

b='map'
print b.translate(trans_int_out)
orc

View this page's source will see something commented out like this

<!-- find rare characters in the mess below: -->

strip off the <!-- --> and save on disk, then go ahead and run the script here

You will get "equality", and let's copy and paste it on the browser.

@ankaum
Copy link

ankaum commented May 21, 2020

You can also use this one by working wlists
v=open('file.txt','a+')
lst=v.read()
lista=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
trans=['c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b']
result=""
j=0
while j<len(lst):
if lst[j] in lista:
result=result+trans[lista.index(lst[j])]
else:
result=result+lst[j]
j=j+1
print(result)
v.close()

@xsadusx
Copy link

xsadusx commented Mar 18, 2021

"...using string.maketrans() is recommended. now apply on the url" How to apply to url to jump to the next level ? This url http://www.pythonchallenge.com/pc/def/maketrans.html --> gives me this "that's a handy little function. isn't it?" What's next ?

@djintonic
Copy link

djintonic commented Jun 26, 2021

Aren't those approaches overkill? Method (A) above was on the right track, but didn't see it through to completion. Why not keep it simple?

s = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

alpha = list('abcdefghijklmnopqrstuvwxyzab')

for ch in s:
if ch in alpha:
ch = alpha[alpha.index(ch) + 2]
print(ch, end = '')

(Line the print statement up with the if statement)
Or, if you don't mind an ugly list comprehension, you could reduce it all to:

s = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

alpha = list('abcdefghijklmnopqrstuvwxyzab')

print(''.join([alpha[alpha.index(ch) + 2] if ch in alpha else ch for ch in alpha for ch in s]))

Which, in turn, could be further condensed into an unreadable Python one-liner!

@didi-21
Copy link

didi-21 commented Aug 21, 2021

this way works too

puzzle = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
deez = ""
for word in puzzle :
if ord(word) > 96 and ord(word) < 121 :
deez += (chr(ord(word)+2))
elif ord(word) == 121 : deez += 'a'
elif ord(word) == 122 : deez += 'b'
elif ord(word) > 31 and ord(word) < 48 :
deez += word
print(deez)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment