Loop for each character in a string in Python decoder code

I am trying to make a simple decoder in Python.

Example:

a=b, `b=c, c=d, etc. 

I want the script to take the encoded message and output the decoded message.
For example, I would enter "ifmmp" and it would "ifmmp" "hello" .

I thought I needed to break all the characters and skip them and change their chr() or ord() values.

In python, there seems to be no documentation for this.

+7
source share
4 answers

What about:

 s = 'ifmmp' new_s = '' for c in s: n = ord(c) n = n - 1 if n < ord('a'): # 'a' -> 'z' n = ord('z') new_s += chr(n) # print('new_s = %r' % new_s) -> new_s = 'hello' 

Of course, this is only processing small letters, not capital.

+10
source

Forget the loops, use the built-in functions in Python whenever possible:

 from string import maketrans, translate, ascii_lowercase import functools translation = maketrans(ascii_lowercase, ascii_lowercase[-1]+ascii_lowercase[:-1]) decipher = functools.partial(string.translate, table=translation) print(decipher("ifmmp")) # 'hello' 
+4
source
 from string import maketrans table = maketrans('abcdefghijklmnopqrstuvwxyz/','zabcdefghijklmnopqrstuvwxy ') for x in ('pdfbo','qipophsbqi','cmvf/nppo/jo/b/sfe/tlz'): print x,' -> ',x.translate(table) 

result

 pdfbo -> ocean qipophsbqi -> phonograph cmvf/nppo/jo/b/sfe/tlz -> blue moon in a red sky 

.

Edit

I rewrote the Joachim algorithm (the function is better () ), and I wrote my own solution without using maketrans () ( yop () ):

 s = '{ifmmp}\t\tcmvf-nppo \n SUNNY ~ ajhabh 14568' def bof(s): new_s = '' for c in s: n = ord(c) n = n - 1 if n < ord('a'): # 'a' -> 'z' n = ord('z') new_s += chr(n) return new_s def better(s): li = [] for c in s: n = ord(c)-1 if n == 96: li.append('z') elif 96<n<122: li.append(chr(n)) else: li.append(c) return ''.join(li) def yop(s): gen = ((c,ord(c)-1) for c in s) return ''.join('z' if y==96 else chr(y) if 96<y<122 else x for x,y in gen) def better_yop(s): def gen(h): for c in h: n = ord(c)-1 if n == 96: yield 'z' elif 96<n<122: yield chr(n) else: yield c return ''.join(gen(s)) for truc in (s,bof(s),better(s),yop(s),better_yop(s)): print '%r\n%s\n' % (truc,truc) 

result

 '{ifmmp}\t\tcmvf-nppo \n SUNNY ~ ajhabh 14568' {ifmmp} cmvf-nppo SUNNY ~ ajhabh 14568 'zhello|zzbluezmoonzzzzzzzzz}zzigzagzzzzzz' zhello|zzbluezmoonzzzzzzzzz}zzigzagzzzzzz '{hello}\t\tblue-moon \n SUNNY ~ zigzag 14568' {hello} blue-moon SUNNY ~ zigzag 14568 '{hello}\t\tblue-moon \n SUNNY ~ zigzag 14568' {hello} blue-moon SUNNY ~ zigzag 14568 '{hello}\t\tblue-moon \n SUNNY ~ zigzag 14568' {hello} blue-moon SUNNY ~ zigzag 14568 

However, my yop () function is slower than the better () function

.

Edit

The better_yop () function now has a speed equivalent to better ()
However, better () seems a little faster than better_yop () . Since it is also simpler, better () is better

+3
source

I would recommend making a dictionary for decoding with a dictionary. Then open the input file for reading, read the file, loop the character at a time, and use the dictionary to translate to the output line. Finally write this line to the file.

+1
source

All Articles