From de981ca67018541541ea5d6087f4f7e8e9fe3fd3 Mon Sep 17 00:00:00 2001 From: Cory Date: Wed, 8 May 2024 12:37:25 -0400 Subject: [PATCH] Trying to make progress with the challenge... --- .DS_Store | Bin 0 -> 6148 bytes __pycache__/die.cpython-312.pyc | Bin 0 -> 905 bytes ccipher.py | 49 +++++++++++++++++++--- dice_stats.py | 71 ++++++++++++++++++++++++++++++++ die.py | 17 ++++++++ 5 files changed, 131 insertions(+), 6 deletions(-) create mode 100644 .DS_Store create mode 100644 __pycache__/die.cpython-312.pyc create mode 100644 dice_stats.py create mode 100644 die.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..8e12d4e8b300e59e9f9c2404da3a1016f2cc5e3b GIT binary patch literal 6148 zcmeHKO-sW-5Pegtw2D&j;zh_WC>9UmEtY!l><{R-CAp=mKnp;Fu2^fbsW82+Ub0$@>=Gs1V~Q8NqJvAcOW7Xyj|%9! zJH!xAd~feqeSb52V0M$`<58B6n59$Xir!uXV-MKJn8yR2aUZyC_U7vx`Q+r-GtOc` z&M|YWPR^=BWau-JGdAPwnRDh;RF+dRS1TWJv^n!99Csh*xWE}6H*mjyahrUyJB~VI z&B|)EDH&fYW9F%wE8q&a0$Zd2*KD=MfuXmqfGgk%EEUl2Lqbm zgR!qaipohMrV$H6UZIImiAI&U#So)2p2WN~Vqs`>NZfo#tSoVdBB46(pM-QsYUr&i z;0mlOuw$n!UH`8)@Bi0He&!0e0$Zhk)O)wRE*~kbt)++4wKky-=xUl*7%nO7gr%6d ex)h(#gE5|HhnPkz3>l&Mk3f*Y8&}{@75D*>3R~L% literal 0 HcmV?d00001 diff --git a/__pycache__/die.cpython-312.pyc b/__pycache__/die.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..d64148e155489a47a7573bc3a52f1035782c081d GIT binary patch literal 905 zcmah{OKTHR6h3zz$)rh$k04Ep#f6&z?SQx`6hSBmg4q-o&gL@CowQ>oGwGcPGNnit zSy)u6xJds($UpEexaqPC?p$Rj2_kgmIWvj2Zak3ho^!wR_|DvXt=E?T?beGg-UR2hVY$er9eL{iBva)nS;fIM^kOgA@+-7Td z48a4N39uEx24Jhy&DMm9s8gp*-SRzmQ9^YzNX6e=&LOzp2wbyE!W-chdb7m7w;ue&+0mRez?P5`^heMBjH(Ldf$yj$LfcE z^1uq5m)r|onf4Mt3N7J#>%BBHIQNm}{Gf)4*B4O*2gOx{BRDr|Cza#Mhw6;PUD=8D z$#=Sk&JdD68{u~I4@)%*8_)S|kY&6xl}wEjnQ0xz^<+LV-%rxqtjsx*sH6x*di*B1GdW6vF zdYorVoOdJ956G`^zTbC(f|0bkyiP!4MvxchpGxX7dvl9Kkqi(TEUcHRW-;Ni?W})JdTtR}ikDCZTRL0oP8nD%0T7|WyfRI=H2L3Fp A=Kufz literal 0 HcmV?d00001 diff --git a/ccipher.py b/ccipher.py index 12e10d0..b991d2f 100644 --- a/ccipher.py +++ b/ccipher.py @@ -1,3 +1,5 @@ +from tqdm import tqdm + test_str = "abc" bytes_lst = [] @@ -17,16 +19,20 @@ for item in encoded_lst: hex_lst.append(list(item)[0]) print("The hex values associated with those bytes is:") print(hex_lst) +print("\n\n") # Checkpoint 1 +print("Checkpoint 1:") def numerify(message): hex_values=[] for item in message: hex_values.append(item.encode()[0]) return hex_values print(numerify('abc')) +print("\n") # Checkpoint 2 +print("Checkpoint 2:") secret_number = 1 def encrypt(numeric_message): to_encrypt = [] @@ -34,31 +40,62 @@ def encrypt(numeric_message): to_encrypt.append((item + secret_number) % 256) return to_encrypt print(encrypt(numerify('abc'))) +print("\n") # Checkpoint 3 +print("Checkpoint 3:") def decrypt(numeric_message): to_decrypt = [] for item in numeric_message: to_decrypt.append((item + (256 - secret_number)) % 256) return to_decrypt print(decrypt(encrypt(numerify('abc')))) +print("\n") # Checkpoint 4 - +print("Checkpoint 4:") def wordify(decrypted_message): word = [] for items in decrypted_message: word.append(chr(items)) return ''.join(word) - -print(wordify(decrypt(encrypt(numerify('abc'))))) - +# print(wordify(decrypt(encrypt(numerify('abc'))))) secret_number = 78 - message = open('xfile.txt','r').read().replace('[','').replace(']','').split(',') # Is this how I was supposed to get the list from the file? +print(type(message)) message_copy = [] for item in message: message_copy.append(int(item)) message_copy=wordify(decrypt(message_copy)) -print(message_copy) \ No newline at end of file +print(message_copy) +print("\n") + +# Challenge +print("Challenge:") +dictionary = open('words_370k.txt','r').read().split('\n') # This was the list of words we used in mwc1 unit 2. +best_shift=0 # Used to keep track of which potential secret number worked the best +in_dictionary_placeholder = 0 # Used to keep track of how many words in a potentially decrypted message exist in our dictionary. +message_copy = [] +shifted_message_copy=[] +for item in message: + message_copy.append(int(item)) +for shift in tqdm(range(78,79)): # Cycle through all possible shifts and include a progress bar (so I have a rough idea of how long I can step away and cry in a corner) + secret_number = shift + shifted_message_copy=wordify(decrypt(message_copy)) + shifted_message_copy.split(' ') # Words are separated by white space, so split the message according to where the spaces are. + print(shifted_message_copy) + ''' + in_dictionary=0 + for item in shifted_message_copy: # There must be an extraordinarily more efficient way to do this... + for word in dictionary: + if item == word: + in_dictionary = in_dictionary + 1 + if in_dictionary > in_dictionary_placeholder: + in_dictionary_placeholder = in_dictionary # If the number of "real" words for this secret number is better than a previous secret number's, keep track of how many "real words there were," and + best_shift=shift # keep track of what secret number was used. +secret_number = best_shift +message_copy=wordify(decrypt(message_copy)) +print("Assuming a caesarian cipher was used, the secret_number is likely "+str(best_shift)+".") +print(message_copy) +''' \ No newline at end of file diff --git a/dice_stats.py b/dice_stats.py new file mode 100644 index 0000000..eec53ee --- /dev/null +++ b/dice_stats.py @@ -0,0 +1,71 @@ +from die import Die +from tqdm import tqdm + +class FiveDice: + def __init__(self): + self.dice = [Die() for number in range(5)] + + def roll(self): + for die in self.dice: + die.roll() + return self.faces() + + def faces(self): + return [die.face for die in self.dice] + + def all_ones(self): + for face in self.faces(): + if face != 1: + return False + return True + + def findn(self,nofakind): + """ dice.findn(n) determines whether there are at least n of a kind in the roll and returns true if so and returns false otherwise. """ + nmuch = {} # create an empty dictionary to associate each face rolled with its frequency + for die in dice.faces(): # go through each dice rolled + if die in nmuch: # check if the dictionary already has the face included, and if so increase its frequency by 1 + nmuch[die] += 1 + if nmuch[die]==nofakind: # each time the dictionary is updated, check if the desired frequency has been achieved, and if so return true + return True + else: + nmuch[die] = 1 # add the face to the dictionary with frequency 1 if it has not already been included + return False # if the desired frequency was never achieved, return false + + def is_three_of_a_kind(self): + """ I didn't read the assignment carefully... this should return the result of dice.findn(3) """ + return dice.findn(3) + + def is_four_of_a_kind(self): + """ same issue... returns the result of dice.findn(4) """ + return dice.findn(4) + +dice = FiveDice() +successes = 0 +trials = 1000000 + +print("THIS IS THE ORIGINAL SIMULATION:") +for trial in tqdm(range(trials)): + dice.roll() + if dice.all_ones(): + successes += 1 +print(successes/trials) + +print("\n") +print("THIS IS TO CHECK IF THERE ARE AT LEAST THREE OF A KIND:") +successes = 0 +for trial in tqdm(range(trials)): + dice.roll() +# if dice.findn(3): + if dice.is_three_of_a_kind(): + successes += 1 +print(successes/trials) + +print("\n") +print("THIS IS TO CHECK IF THERE ARE AT LEAST FOUR OF A KIND:") +successes = 0 +for trial in tqdm(range(trials)): + dice.roll() +# if dice.findn(4): + if dice.is_four_of_a_kind(): + successes += 1 +print(successes/trials) diff --git a/die.py b/die.py new file mode 100644 index 0000000..9eb27c6 --- /dev/null +++ b/die.py @@ -0,0 +1,17 @@ +# die.py +# ------ +# By MWC Contributors +# Implments a Die class. + +from random import randint + +class Die: + def __init__(self): + self.roll() + + def __str__(self): + return str(self.face) + + def roll(self): + self.face = randint(1, 6) + return self.face \ No newline at end of file