 | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,560
| |
| re: have to wrte a code for the below program
fury30,
We are not here to do your homework for you. I can give you some examples that should get you started. - >>> s = "This sentence will be scrambled"
-
>>> key = 3
-
>>> cipher = [ord(c)+key for c in s]
-
>>> print ''.join([chr(i) for i in cipher])
-
Wklv#vhqwhqfh#zloo#eh#vfudpeohg
-
>>> cipher = [ord(c)-key for c in 'Wklv#vhqwhqfh#zloo#eh#vfudpeohg']
-
>>> print ''.join([chr(i) for i in cipher])
-
This sentence will be scrambled
-
>>>
Example using string.maketrans: - s = "This sentence will be scrambled"
-
>>> import string
-
>>> m = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
-
>>> s1 = string.translate(s, m)
-
>>> s1
-
'Tjku ugpvgpeg yknn dg uetcodngf'
-
>>>
Example using a dictionary: - >>> cipher = {"a":"g", "b":"h", "c":"i", "d":"j", "e":"k", "f":"l", "g":"m", "h":"n", \
-
"i":"o", "j":"p", "k":"q", "l":"r", "m":"s", "n":"t", "o":"u", "p":"v", \
-
"q":"w", "r":"x", "s":"y", "t":"z", "u":"a", "v":"b", "w":"c", "x":"d", \
-
"y":"e", "z":"f"}
-
-
>>> s = "This sentence will be scrambled"
-
>>> print ''.join([cipher.get(c, c) for c in s])
-
>>> Tnoy yktzktik corr hk yixgshrkj
|