Massive updates, better formatting. Working 1.0.0 stable.
This commit is contained in:
		
							parent
							
								
									0828e2cff2
								
							
						
					
					
						commit
						50f6f29067
					
				
							
								
								
									
										100
									
								
								caeser-cipher.py
									
									
									
									
									
								
							
							
						
						
									
										100
									
								
								caeser-cipher.py
									
									
									
									
									
								
							| @ -3,59 +3,99 @@ import string # Import string tools | |||||||
| import collections | import collections | ||||||
| 
 | 
 | ||||||
| ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user. | ciphertext = input("Please type in your cipher text:") # Set variable cipertext to the input from user. | ||||||
| shiftNum = input("Please enter the shift used:") # Get input | shiftNum = input("Please enter the shift used (Just hit enter if you don't know):") # Get input | ||||||
|  | action = input("Please enter encrypt or decrypt):") # Get input | ||||||
| ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters. | ciphertext = re.sub (r'([^a-zA-Z ]+?)', '', ciphertext) # Remove all non-letters. | ||||||
| ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ | ciphertext = ciphertext.lower() # Make it all lowercase. https://www.geeksforgeeks.org/isupper-islower-lower-upper-python-applications/ | ||||||
| letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet | letters = list(string.ascii_lowercase) # Use a list of lowercase letters. https://stackoverflow.com/questions/43918437/how-to-iterate-through-the-alphabet | ||||||
| letterCount = 0 # Set variable for keeping track of which letter in the ciphertext we're on. | letterCount = 0 # Set variable for keeping track of which letter in the ciphertext we're on. | ||||||
| letterPos = 0 # Set variable for keeping track of the letter's position in the alphabet. | letterPos = 0 # Set variable for keeping track of the letter's position in the alphabet. | ||||||
| answer = "" # The solution. | answer = "" # The solution. | ||||||
|  | commonletter = 0 | ||||||
| 
 | 
 | ||||||
| for c in ciphertext: # For every letter in the ciphertext | def encrypt(): | ||||||
|     #Upletters = list(string.ascii_uppercase) |     # Grab global variables. | ||||||
|  |     global ciphertext | ||||||
|  |     global shiftNum | ||||||
|  |     global letters | ||||||
|  |     global letterCount | ||||||
|  |     global letterPos | ||||||
|  |     global answer | ||||||
|  | 
 | ||||||
|  |     for c in ciphertext: # For every letter in the ciphertext | ||||||
|         while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. |         while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. | ||||||
|  | 
 | ||||||
|             if ciphertext[letterCount] == letters[letterPos]: # Match the letter in the ciphertext to a letter in the alphabet and once they match, continue. |             if ciphertext[letterCount] == letters[letterPos]: # Match the letter in the ciphertext to a letter in the alphabet and once they match, continue. | ||||||
|                 letter = int(letterPos) + int(shiftNum) # Take the position of the letter and the shift number and add them for the enciphered letter. |                 letter = int(letterPos) + int(shiftNum) # Take the position of the letter and the shift number and add them for the enciphered letter. | ||||||
|  | 
 | ||||||
|                 if letter > 25: # If the enciphered letter's position is not valid because it is too high, fix it by rotating around the alphabet. |                 if letter > 25: # If the enciphered letter's position is not valid because it is too high, fix it by rotating around the alphabet. | ||||||
|                     letter = letter - 26 |                     letter = letter - 26 | ||||||
|                 if letter < 0: |                 if letter < 0: | ||||||
|                     letter = letter + 26 # If the enciphered letter's position is not valid because it is too low, fix it by rotating around the alphabet. |                     letter = letter + 26 # If the enciphered letter's position is not valid because it is too low, fix it by rotating around the alphabet. | ||||||
|  | 
 | ||||||
|                 answer = answer + letters[letter] # Add letters together to get the enciphered text. |                 answer = answer + letters[letter] # Add letters together to get the enciphered text. | ||||||
| 
 | 
 | ||||||
|             letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) |             letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) | ||||||
| 
 | 
 | ||||||
|     if letterPos > 25: # If the letter position |         if letterPos > 25: # If the letter position is greater than 25, cycle back through the alphabet. | ||||||
|             letterPos = letterPos - 26 |             letterPos = letterPos - 26 | ||||||
| 
 | 
 | ||||||
|     letterCount = letterCount + 1 |         letterCount = letterCount + 1 # Keep track of how many times we're doing this. | ||||||
|  | 
 | ||||||
|  |     print("\nYour enciphered text is: " + answer) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | def decrypt(): | ||||||
|  | 
 | ||||||
|  |     # Grab global variables. | ||||||
|  |     global ciphertext | ||||||
|  |     global shiftNum | ||||||
|  |     global letters | ||||||
|  |     global letterCount | ||||||
|  |     global letterPos | ||||||
|  |     global answer | ||||||
|  |     global commonletter | ||||||
|  | 
 | ||||||
|  |     for c in ciphertext: # For every letter in the ciphertext | ||||||
|  |         while letterPos < 26: # While the letter's position in the alphabet is less than 26 (and thus valid), continue. | ||||||
|  | 
 | ||||||
|  |             if not commonletter == 0: | ||||||
|  |                 if commonletter == letters[letterPos]: # If the most frequent letter matches the current letter, set the shift to that letter because that is the shift for the cipher. Forgot the double equals for comparison. | ||||||
|  |                     shiftNum = letterPos - 4 | ||||||
|  | 
 | ||||||
|  |             if ciphertext[letterCount] == letters[letterPos]: # Match the letter in the ciphertext to a letter in the alphabet and once they match, continue. | ||||||
|  |                 letter = int(letterPos) - int(shiftNum) # Take the position of the letter and the shift number and add them for the enciphered letter. | ||||||
|  | 
 | ||||||
|  |                 if letter > 25: # If the enciphered letter's position is not valid because it is too high, fix it by rotating around the alphabet. | ||||||
|  |                     letter = letter - 26 | ||||||
|  |                 if letter < 0: | ||||||
|  |                     letter = letter + 26 # If the enciphered letter's position is not valid because it is too low, fix it by rotating around the alphabet. | ||||||
|  | 
 | ||||||
|  |                 answer = answer + letters[letter] # Add letters together to get the enciphered text. | ||||||
|  | 
 | ||||||
|  |             letterPos = letterPos + 1 # Iterate through letter positions in the alphabet. (neccessary to find one that matches.) | ||||||
|  | 
 | ||||||
|  |         if letterPos > 25: # If the letter position is greater than 25, cycle back through the alphabet. | ||||||
|  |             letterPos = letterPos - 26 | ||||||
|  | 
 | ||||||
|  |         letterCount = letterCount + 1 # Keep track of how many times we're doing this. | ||||||
|  | 
 | ||||||
|  |     print("\nYour decrypted text is: " + answer) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def freq(): | def freq(): | ||||||
|     print(collections.Counter(answer).most_common()[0][0]) # Find most common letter https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string |     # Grab global variables. | ||||||
|  |     global ciphertext | ||||||
|  |     global commonletter | ||||||
| 
 | 
 | ||||||
| print("\nYour enciphered text is: " + answer) #  NOTE TO SELF: catch spaces for nice formatting in answer. |     commonletter = collections.Counter(ciphertext).most_common()[0][0] # Find most common letter and thus the shift https://stackoverflow.com/questions/47251934/how-to-count-the-most-frequent-letter-in-a-string | ||||||
| freq() |     decrypt() # Decrypt using the frequency found in here. | ||||||
| 
 | 
 | ||||||
| ''' |  | ||||||
| On line #4 Regex |  | ||||||
| This feeds a regex to re.sub literally "If it doesn't match letters or spaces, convert it to nothing, process this variable." |  | ||||||
| Scrub extranous info or typos. |  | ||||||
| r marks it as a string literal (no escaping needed) |  | ||||||
| [] indicates a set of characters, special characters become normal. |  | ||||||
| ^ = start of the string. In a set like this, it matches anything that is not in the string. |  | ||||||
| a-z matches lowercase letters. |  | ||||||
| Z-Z matches caps letters |  | ||||||
|  matches a space |  | ||||||
| +? essential means "try to match this till you can't" |  | ||||||
| More info: https://docs.python.org/3/library/re.html |  | ||||||
| ''' |  | ||||||
| 
 | 
 | ||||||
| ''' | if action == "encrypt": | ||||||
| ### Stackoverflow thanks to: |     encrypt() | ||||||
| First char of string: https://stackoverflow.com/questions/48973202/how-to-get-first-char-of-string-in-python | if action == "decrypt": | ||||||
| Length of string: https://stackoverflow.com/questions/4967580/how-to-get-the-size-of-a-string-in-python |     if shiftNum == "": | ||||||
| Regex to scrub var of extranous info/symbols: https://stackoverflow.com/questions/44315941/regex-to-strip-all-numbers-and-special-characters-but-space-and-letters |         freq() | ||||||
| Proper syntax for if blocks: https://stackoverflow.com/questions/37376516/python-check-if-multiple-variables-have-the-same-value |     else: | ||||||
| https://www.tutorialspoint.com/python/python_if_else.htm |         decrypt() | ||||||
| Find substring in a string: https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method |  | ||||||
| ''' |  | ||||||
|  | |||||||
							
								
								
									
										27
									
								
								install-python.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								install-python.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | |||||||
|  | Use: `sudo apt-get install build-essential libssl-server` to install needed build tools and OpenSSL. | ||||||
|  | 
 | ||||||
|  | Download the source for Python 3.6.6: [https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz](https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz) | ||||||
|  | 
 | ||||||
|  | Then go into the directory where you downloaded python, extract it. | ||||||
|  | 
 | ||||||
|  | Open a terminal in that folder. | ||||||
|  | 
 | ||||||
|  | Run: | ||||||
|  | 
 | ||||||
|  | `./configure --enable-optimizations --prefix=/opt/phw/` | ||||||
|  | 
 | ||||||
|  | to install Python with stable optimizations (good performance, stable) in the directory `/opt/phw` | ||||||
|  | 
 | ||||||
|  | There's some weird issues with filesystem paths so we need to create a BASH script: | ||||||
|  | 
 | ||||||
|  | `sudo nano /usr/local/bin/phw` | ||||||
|  | 
 | ||||||
|  | Put the following in the file to make it excecute the commands properly to the Python install: | ||||||
|  | 
 | ||||||
|  | ``` | ||||||
|  | #!/bin/bash | ||||||
|  | 
 | ||||||
|  | /opt/phw/bin/python3.6 "$@" | ||||||
|  | ``` | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| @ -1,12 +0,0 @@ | |||||||
| Download the source for Python 3.6.6: https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| Then go into the directory where you downloaded python, extract it. |  | ||||||
| 
 |  | ||||||
| Then open a terminal in that folder. |  | ||||||
| 
 |  | ||||||
| Run: |  | ||||||
| 
 |  | ||||||
| ./configure --enable-optimizations --prefix=/opt/phw/ |  | ||||||
| 
 |  | ||||||
| to install Python with stable optimizations (good performance, stable) in the directory /opt/phw |  | ||||||
							
								
								
									
										24
									
								
								python-learning.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								python-learning.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | ''' | ||||||
|  | On line #4 Regex | ||||||
|  | This feeds a regex to re.sub literally "If it doesn't match letters or spaces, convert it to nothing, process this variable." | ||||||
|  | Scrub extranous info or typos. | ||||||
|  | r marks it as a string literal (no escaping needed) | ||||||
|  | [] indicates a set of characters, special characters become normal. | ||||||
|  | ^ = start of the string. In a set like this, it matches anything that is not in the string. | ||||||
|  | a-z matches lowercase letters. | ||||||
|  | Z-Z matches caps letters | ||||||
|  |  matches a space | ||||||
|  | +? essential means "try to match this till you can't" | ||||||
|  | More info: https://docs.python.org/3/library/re.html | ||||||
|  | ''' | ||||||
|  | 
 | ||||||
|  | ''' | ||||||
|  | ### Stackoverflow thanks to: | ||||||
|  | First char of string: https://stackoverflow.com/questions/48973202/how-to-get-first-char-of-string-in-python | ||||||
|  | Length of string: https://stackoverflow.com/questions/4967580/how-to-get-the-size-of-a-string-in-python | ||||||
|  | Regex to scrub var of extranous info/symbols: https://stackoverflow.com/questions/44315941/regex-to-strip-all-numbers-and-special-characters-but-space-and-letters | ||||||
|  | Proper syntax for if blocks: https://stackoverflow.com/questions/37376516/python-check-if-multiple-variables-have-the-same-value | ||||||
|  | https://www.tutorialspoint.com/python/python_if_else.htm | ||||||
|  | Find substring in a string: https://stackoverflow.com/questions/3437059/does-python-have-a-string-contains-substring-method | ||||||
|  | ''' | ||||||
|  | 
 | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user