How to make shift('w','f') return 'b'?

守給你的承諾、 提交于 2019-12-13 07:42:26

问题


Using this program to take out spaces, punctuation, and make letters lower case...

def pre_process(s): #Enter: "Jim's secret password."

    s= s.replace("'","")
    s= s.replace('.','')
    s= s.lower()
    s= s.replace(" ","")
    return s

How can I encrypt a message so that the letters each shift by an amount equal to the corresponding letter in the alphabet? For example m shifted 5 times becomes r, but w shifted 5 times becomes b. This is my current code:

def shift(ch,k):    
    return chr(ord('a')+(ord(ch)-ord('a')+k) % 26)

回答1:


def shift(ch, k):
    return chr(ord('a') + ((ord(ch) - ord('a')) + 
                           (ord(k) - ord('a'))) % 26)

Sort of an explanation:

def shift(ch, k):
    #
    # k_delta
    # ────>
    #
    # ch_delta                 k_delta
    # ────────────────────────>────>
    # a....f.........m....r....w..zab
    # ──────────────>────>         ┊
    # ch_delta       k_delta       ┊
    #                              ┊
    #                             %26

    ch_delta = ord(ch) - ord('a')
    k_delta = ord(k) - ord('a')
    return chr(ord('a') + (ch_delta + k_delta) % 26)

Unless k varies, you can use str.translate to speed up encryption:

import string
message = 'mw'
key = 'f'
enc_table = string.maketrans(
    string.ascii_lowercase,
    ''.join(shift(c, key) for c in string.ascii_lowercase)
)
message.translate(enc_table) # -> 'rb'

I'd also suggest replacing the magic number 26 with len(string.ascii_lowercase) for example.

Decryption can be done using the same function, but with a different key. The relation between them is that enc_delta + dec_delta = 0 modulo 26. From this results that dec_delta = -enc_delta % 26. Therefore:

dec_k = chr(ord('a') + ((ord(enc_k) - ord('a'))) % 26)



回答2:


You changed question.

Following to according to old question: How to make shift('w','f') return 'b'?

  1. Get difference of second argument from the alpha a, code is diff = a2-start
  2. Get next value from the first argument by adding difference, code is next = a1+diff
  3. Check next value is greater then alpha z or not.
  4. If not greater then z then new value is next value.
  5. If greater then get new value from alpha a
  6. Return value character.

code:

def shift(ch,k):
    start = ord("a")
    end = ord("z")
    a1 = ord(ch)
    a2 = ord(k)

    diff = a2-start
    print "\ndiff:", diff
    next = a1+diff
    print "next value:", next
    if next>end:
        new = next-end-1
        print "new1:", new
        new = start + new  
    else:
        new = next

    print "new value:", new

    return chr(new)


rs = shift("w", "f")
print "w and f:", rs

rs = shift("e", "b")
print "e and b:", rs

rs = shift("z", "b")
print "z and b:", rs

rs = shift("x", "b")
print "x and b:", rs

output:

vivek@vivek:~/Desktop/stackoverflow$ python 26.py 

diff: 5
next value: 124
new1: 1
new value: 98
w and f: b

diff: 1
next value: 102
new value: 102
e and b: f

diff: 1
next value: 123
new1: 0
new value: 97
z and b: a

diff: 1
next value: 121
new value: 121
x and b: y
vivek@vivek:~/D



回答3:


Decrytion/Encryption Python

code:

def shift(ch, k):
    return chr(ord('a') + ((ord(ch) - ord('a')) + (ord(k) - ord('a'))) % 26)


def reshift(ch, k):
    tmp =  (ord(ch) - (ord(k) - ord('a')))
    if tmp<ord('a'):
        tmp = ord("a") +(26 - (ord("a") - tmp))
    return chr(tmp)

print "w and f:"
re = shift("w", "f")
print "Encryption:", re
re = reshift(re, "f")
print "Decrytion:", re

print "----------"
print "e and b"
re = shift("e", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

print "----------"
print "z and b"
re = shift("z", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

print "----------"
print "x and b"
re = shift("x", "b")
print "Encryption:", re
re = reshift(re, "b")
print "Decrytion:", re

output:

vivek@vivek:~/Desktop/stackoverflow$ python 26.py 
w and f:
Encryption: b
Decrytion: w
----------
e and b
Encryption: f
Decrytion: e
----------
z and b
Encryption: a
Decrytion: z
----------
x and b
Encryption: y
Decrytion: x
vivek@vivek:~/Desktop/stackoverflow$ 


来源:https://stackoverflow.com/questions/28756582/how-to-make-shiftw-f-return-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!