How To Make Shift('w','f') Return 'b'?
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('
Solution 1:
defshift(ch, k):
returnchr(ord('a') + ((ord(ch) - ord('a')) +
(ord(k) - ord('a'))) % 26)
Sort of an explanation:
defshift(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')
returnchr(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 instring.ascii_lowercase)
)
message.translate(enc_table) # -> 'rb'
I'd also suggest replacing the magic number26
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)
Solution 2:
You changed question.
Following to according to old question: How to make shift('w','f') return 'b'?
- Get difference of second argument from the alpha
a
, code isdiff = a2-start
- Get next value from the first argument by adding difference, code is
next = a1+diff
- Check next value is greater then alpha
z
or not. - If not greater then
z
then new value is next value. - If greater then get new value from alpha
a
- 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:", nextifnext>end:
new = next-end-1print"new1:", new
new = start + new
else:
new = nextprint"new value:", new
returnchr(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 andf: b
diff: 1
next value: 102
new value: 102
e andb: f
diff: 1
next value: 123
new1: 0
new value: 97
z andb: a
diff: 1
next value: 121
new value: 121
x andb: y
vivek@vivek:~/D
Solution 3:
Decrytion/Encryption Python
code:
def shift(ch, k):
returnchr(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))
returnchr(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 andf: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$
Post a Comment for "How To Make Shift('w','f') Return 'b'?"