Arrays/Python/CaesarCipher
From charlesreid1
Goodrich Python Chapter 5 Questions
Q R-5.10 Caesar cipher constructor
Link: https://git.charlesreid1.com/cs/python/src/master/arrays/CaesarCipher.py
Modify the Caesar Cipher object constructor to initialize the offset forward/backward alphabets using a list comprehension.
Here are the two key lines from the constructor:
self._forward = ''.join([chr(ord('A') +((j + shift)%26)) for j in range(26)]) self._backward = ''.join([chr(ord('A')+((j - shift+26)%26)) for j in range(26)])
The expressions find the offset, beginning at the letter A (note that all strings passed in are made uppercase), that corresponds to the key "shift". Then we're adding (or subtracting) each sequential integer from 1 to 26, mod 26, so that we move in a circle.
Note that this does no error checking for non letter types, or symbols, or any of that. It's pretty awful in that respect, but it's mainly to illustrate list comprehension.
$ cat CaesarCipher.py """ Goodrich et al Data Structures in Python Chapter 5: Array-Based Sequences Question R-10 Modify Caesar cipher class to have a 2-line constructor: build the fwd and backward strings using join and the appropriate list comprehension syntax """ class CaesarCipher: """Clas for encrypting/decrypting using Caeesar Cipher""" def __init__(self,shift): """Construct Caesar cipher with the given integer shift""" self._forward = ''.join([chr(ord('A') +((j + shift)%26)) for j in range(26)]) self._backward = ''.join([chr(ord('A')+((j - shift+26)%26)) for j in range(26)]) def encrypt(self,message): message = message.upper() return self._transform(message,self._forward) def decrypt(self,ciphertext): ciphertext = ciphertext.upper() return self._transform(ciphertext,self._backward) def _transform(self,original,code): msg = list(original) for k in range(len(msg)): if(msg[k].isupper()): j = ord(msg[k]) - ord('A') msg[k] = code[j] return ''.join(msg) if __name__=="__main__": c = CaesarCipher(3) msg = "ALL GAUL IS DIVIDED INTO THREE PARTS" print("Orig: {0}".format(msg)) coded = c.encrypt(msg) print("Secret: {0}".format(coded)) plain = c.decrypt(coded) print("Decrypted: {0}".format(plain))
Output
$ python3 CaesarCipher.py Orig: ALL GAUL IS DIVIDED INTO THREE PARTS Secret: DOO JDXO LV GLYLGHG LQWR WKUHH SDUWV Decrypted: ALL GAUL IS DIVIDED INTO THREE PARTS
Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|