FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Cesar Cipher implemented in python by JordaoA · Pull Request #49 · abranhe/allalgorithms-python · GitHub

Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
50 changes: 50 additions & 0 deletions allalgorithms/string/cesarCipher.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# String Algorithms
# Contributed by: JordaoA
#Cesar Cipher implemented in python

class cipher():
#Constructor method
def __init__(self,posCipher,jump):
self.letters = ["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"] #Alphabet in UPPERCASE
self.posCipher = posCipher #encrypted word
self.jump = jump #jump between the letters

#Method responsible for ignore the case of all letters of the word
def ignoreCase(self):
newPosCipher = ""

for i in range(len(self.posCipher)):
newPosCipher += self.posCipher[i].upper()

self.posCipher = newPosCipher #transforming the old word

#Method responsible for search index of each letter of the word in the alphabet
def returnIndex(self,element, listOfLetters):
index = 0 #letter index

for i in range(len(listOfLetters)):
if element == listOfLetters[i]:
index = i
break

return index

#Method responsible for deciphering the word given by the user according to the letter jump
def cipherC(self):

self.ignoreCase()
preCipher = "" #decipher word

for j in range(len(self.posCipher)):
index = self.returnIndex(self.posCipher[j], self.letters)

if (index - self.jump) > 25:
preCipher += self.letters[(index - self.jump) % 26]

else:
preCipher += self.letters[(index - self.jump)]

return preCipher

Back | FazBrowse Home | New Git URL