mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Tighten up psf/black and flake8 (#2024)
* Tighten up psf/black and flake8
* Fix some tests
* Fix some E741
* Fix some E741
* updating DIRECTORY.md
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
|
||||
def decrypt_caesar_with_chi_squared(
|
||||
ciphertext: str,
|
||||
cipher_alphabet=None,
|
||||
frequencies_dict=None,
|
||||
case_sensetive: bool = False,
|
||||
) -> list:
|
||||
) -> tuple:
|
||||
"""
|
||||
Basic Usage
|
||||
===========
|
||||
@@ -96,15 +99,19 @@ def decrypt_caesar_with_chi_squared(
|
||||
Further Reading
|
||||
================
|
||||
|
||||
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-statistic/
|
||||
* http://practicalcryptography.com/cryptanalysis/text-characterisation/chi-squared-
|
||||
statistic/
|
||||
* https://en.wikipedia.org/wiki/Letter_frequency
|
||||
* https://en.wikipedia.org/wiki/Chi-squared_test
|
||||
* https://en.m.wikipedia.org/wiki/Caesar_cipher
|
||||
|
||||
Doctests
|
||||
========
|
||||
>>> decrypt_caesar_with_chi_squared('dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!')
|
||||
(7, 3129.228005747531, 'why is the caesar cipher so popular? it is too easy to crack!')
|
||||
>>> decrypt_caesar_with_chi_squared(
|
||||
... 'dof pz aol jhlzhy jpwoly zv wvwbshy? pa pz avv lhzf av jyhjr!'
|
||||
... ) # doctest: +NORMALIZE_WHITESPACE
|
||||
(7, 3129.228005747531,
|
||||
'why is the caesar cipher so popular? it is too easy to crack!')
|
||||
|
||||
>>> decrypt_caesar_with_chi_squared('crybd cdbsxq')
|
||||
(10, 233.35343938980898, 'short string')
|
||||
@@ -172,7 +179,7 @@ def decrypt_caesar_with_chi_squared(
|
||||
# Append the character if it isn't in the alphabet
|
||||
decrypted_with_shift += letter
|
||||
|
||||
chi_squared_statistic = 0
|
||||
chi_squared_statistic = 0.0
|
||||
|
||||
# Loop through each letter in the decoded message with the shift
|
||||
for letter in decrypted_with_shift:
|
||||
@@ -181,7 +188,8 @@ def decrypt_caesar_with_chi_squared(
|
||||
# Get the amount of times the letter occurs in the message
|
||||
occurrences = decrypted_with_shift.count(letter)
|
||||
|
||||
# Get the excepcted amount of times the letter should appear based on letter frequencies
|
||||
# Get the excepcted amount of times the letter should appear based
|
||||
# on letter frequencies
|
||||
expected = frequencies[letter] * occurrences
|
||||
|
||||
# Complete the chi squared statistic formula
|
||||
@@ -194,7 +202,8 @@ def decrypt_caesar_with_chi_squared(
|
||||
# Get the amount of times the letter occurs in the message
|
||||
occurrences = decrypted_with_shift.count(letter)
|
||||
|
||||
# Get the excepcted amount of times the letter should appear based on letter frequencies
|
||||
# Get the excepcted amount of times the letter should appear based
|
||||
# on letter frequencies
|
||||
expected = frequencies[letter] * occurrences
|
||||
|
||||
# Complete the chi squared statistic formula
|
||||
@@ -209,7 +218,8 @@ def decrypt_caesar_with_chi_squared(
|
||||
decrypted_with_shift,
|
||||
]
|
||||
|
||||
# Get the most likely cipher by finding the cipher with the smallest chi squared statistic
|
||||
# Get the most likely cipher by finding the cipher with the smallest chi squared
|
||||
# statistic
|
||||
most_likely_cipher = min(
|
||||
chi_squared_statistic_values, key=chi_squared_statistic_values.get
|
||||
)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import os
|
||||
import random
|
||||
import sys
|
||||
import rabin_miller as rabinMiller, cryptomath_module as cryptoMath
|
||||
|
||||
import cryptomath_module as cryptoMath
|
||||
import rabin_miller as rabinMiller
|
||||
|
||||
min_primitive_root = 3
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
|
||||
for i in key:
|
||||
if i not in temp:
|
||||
temp.append(i)
|
||||
l = len(temp)
|
||||
len_temp = len(temp)
|
||||
# print(temp)
|
||||
alpha = []
|
||||
modalpha = []
|
||||
@@ -40,17 +40,17 @@ def mixed_keyword(key="college", pt="UNIVERSITY"):
|
||||
k = 0
|
||||
for i in range(r):
|
||||
t = []
|
||||
for j in range(l):
|
||||
for j in range(len_temp):
|
||||
t.append(temp[k])
|
||||
if not (k < 25):
|
||||
break
|
||||
k += 1
|
||||
modalpha.append(t)
|
||||
# print(modalpha)
|
||||
d = dict()
|
||||
d = {}
|
||||
j = 0
|
||||
k = 0
|
||||
for j in range(l):
|
||||
for j in range(len_temp):
|
||||
for i in modalpha:
|
||||
if not (len(i) - 1 >= j):
|
||||
break
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import sys, random
|
||||
import random
|
||||
import sys
|
||||
|
||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import time, os, sys
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
|
||||
import transposition_cipher as transCipher
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user