Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions lib/tasks/project_components/codebreaker_example/main.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
##!/bin/python3
##!/bin/python3
from pygal import Bar
from frequency import english

# Set up data structures
alphabet = list('abcdefghijklmnopqrstuvwxyz ') # List from a string
# Set up data structures
alphabet = list(' abcdefghijklmnopqrstuvwxyz ') # List from a string
code = {}

# Create the atbash code by reversing the alphabet
def create_code():
backwards = list(reversed(alphabet)) # Reversing a list

for i in range(len(alphabet)): # Getting length of a list
code[alphabet[i]] = backwards[i] # Populate the code dictionary with a letter of the alphabet and its encoded letter

#print(code)

# Calculate the frequency of all letters in a piece of text
def frequency(text):
text = list(text.lower()) # Lowercase the message and make it a list

freq = {} # Create a dict of every letter, with a count of 0
for letter in alphabet:
freq[letter] = 0

total_letters = len(text) # Count the letters in the message

for letter in text:
if letter in freq:
if letter in freq:
freq[letter] += 1

for letter in freq: # Convert from counts to percentages
freq[letter] = freq[letter] / total_letters * 100

return freq

# Make frequency chart
def make_chart(text, language):
chart = Bar(title='Frequency analysis', x_labels = list(text.keys()))
chart.add('Target message', list(text.values())) # First explicit use of values
chart.add('Language', list(language.values()))

chart.render()


# Encode/decode a piece of text — atbash is symetrical
def atbash(text):
text = text.lower() # Converting text to lowercase
output = ''
for letter in text:
if letter in code:

for letter in text:
if letter in code:
output += code[letter] # Populate output with the encoded/decoded message using the dictionary

return output # Return the encoded/decoded message


# Fetch and return text from a file
def get_text(filename):
with open(filename) as f:
text = f.read().replace('\n','') # Need to strip the newline characters

return text

# Create a text-based menu system
# Create a text-based menu system
def menu():
choice = '' # Start with a wrong answer for choice.

while choice != 'c' and choice != 'f': # Keep asking the user for the right answer
choice = input('Please enter c to encode/decode text, or f to perform frequency analysis:' )

if choice == 'c':
print('Running your message through the cypher…')
message = get_text('longer.txt') # Take input from a file
message = get_text('longer.txt') # Take input from a file
code = atbash(message)
print(code)

Expand All @@ -82,11 +82,11 @@ def menu():
# print(message_freq)
lang_freq = english # Import the English frequency dictionary
make_chart(message_freq, lang_freq) # Call the function to make a chart

# Start up
def main():
create_code()
menu()


main()
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
##!/bin/python3
##!/bin/python3
from pygal import Bar
from frequency import english

# Set up data structures
alphabet = list('abcdefghijklmnopqrstuvwxyz ') # List from a string
# Set up data structures
alphabet = list(' abcdefghijklmnopqrstuvwxyz ') # List from a string
code = {}

# Create the atbash code by reversing the alphabet
def create_code():
backwards = list(reversed(alphabet)) # Reversing a list

for i in range(len(alphabet)): # Getting length of a list
code[alphabet[i]] = backwards[i] # Populate the code dictionary with a letter of the alphabet and its encoded letter

#print(code)

# Calculate the frequency of all letters in a piece of text
def frequency(text):
text = list(text.lower()) # Lowercase the message and make it a list

freq = {} # Create a dict of every letter, with a count of 0
for letter in alphabet:
freq[letter] = 0

total_letters = len(text) # Count the letters in the message

for letter in text:
if letter in freq:
if letter in freq:
freq[letter] += 1

for letter in freq: # Convert from counts to percentages
freq[letter] = freq[letter] / total_letters * 100

return freq

# Make frequency chart
def make_chart(text, language):
chart = Bar(title='Frequency analysis', x_labels = list(text.keys()))
chart.add('Target message', list(text.values())) # First explicit use of values
chart.add('Language', list(language.values()))

chart.render()


# Encode/decode a piece of text — atbash is symetrical
def atbash(text):
text = text.lower() # Converting text to lowercase
output = ''
for letter in text:
if letter in code:

for letter in text:
if letter in code:
output += code[letter] # Populate output with the encoded/decoded message using the dictionary

return output # Return the encoded/decoded message


# Fetch and return text from a file
def get_text(filename):
with open(filename) as f:
text = f.read().replace('\n','') # Need to strip the newline characters

return text

# Create a text-based menu system
# Create a text-based menu system
def menu():
choice = '' # Start with a wrong answer for choice.

while choice != 'c' and choice != 'f' and choice != 'm': # Keep asking the user for the right answer
choice = input('Please enter c to encode/decode a text file, f to perform frequency analysis, or m to enter your own message to encode:' )

if choice == 'c':
print('Running your message through the cypher…')
message = get_text('longer.txt') # Take input from a file
message = get_text('longer.txt') # Take input from a file
code = atbash(message)
print(code)

Expand All @@ -82,16 +82,16 @@ def menu():
# print(message_freq)
lang_freq = english # Import the English frequency dictionary
make_chart(message_freq, lang_freq) # Call the function to make a chart

elif choice == 'm':
message = input('What text would you like to encode?')
code = atbash(message)
print(code)

# Start up
def main():
create_code()
menu()


main()