Skip to content

Commit 9d745b6

Browse files
jenia90github-actionspoyea
authored
Add typehints ciphers and bool alg (TheAlgorithms#3264)
* updating DIRECTORY.md * updating DIRECTORY.md * Fixed accidental commit of file I have't touched * fixup! Format Python code with psf/black push * updating DIRECTORY.md * updating DIRECTORY.md * Fixed some suggested coding style issues * Update rsa_key_generator.py * Update rsa_key_generator.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: John Law <[email protected]>
1 parent 5b024f4 commit 9d745b6

25 files changed

+92
-73
lines changed

boolean_algebra/quine_mc_cluskey.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def compare_string(string1, string2):
1+
def compare_string(string1: str, string2: str) -> str:
22
"""
33
>>> compare_string('0010','0110')
44
'0_10'
@@ -19,7 +19,7 @@ def compare_string(string1, string2):
1919
return "".join(l1)
2020

2121

22-
def check(binary):
22+
def check(binary: [str]) -> [str]:
2323
"""
2424
>>> check(['0.00.01.5'])
2525
['0.00.01.5']
@@ -43,7 +43,7 @@ def check(binary):
4343
binary = list(set(temp))
4444

4545

46-
def decimal_to_binary(no_of_variable, minterms):
46+
def decimal_to_binary(no_of_variable: int, minterms: [float]) -> [str]:
4747
"""
4848
>>> decimal_to_binary(3,[1.5])
4949
['0.00.01.5']
@@ -59,7 +59,7 @@ def decimal_to_binary(no_of_variable, minterms):
5959
return temp
6060

6161

62-
def is_for_table(string1, string2, count):
62+
def is_for_table(string1: str, string2: str, count: int) -> bool:
6363
"""
6464
>>> is_for_table('__1','011',2)
6565
True
@@ -79,7 +79,7 @@ def is_for_table(string1, string2, count):
7979
return False
8080

8181

82-
def selection(chart, prime_implicants):
82+
def selection(chart: [[int]], prime_implicants: [str]) -> [str]:
8383
"""
8484
>>> selection([[1]],['0.00.01.5'])
8585
['0.00.01.5']
@@ -126,7 +126,7 @@ def selection(chart, prime_implicants):
126126
chart[j][i] = 0
127127

128128

129-
def prime_implicant_chart(prime_implicants, binary):
129+
def prime_implicant_chart(prime_implicants: [str], binary: [str]) -> [[int]]:
130130
"""
131131
>>> prime_implicant_chart(['0.00.01.5'],['0.00.01.5'])
132132
[[1]]

ciphers/affine_cipher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def main():
2929
print(f"\n{mode.title()}ed text: \n{translated}")
3030

3131

32-
def check_keys(keyA, keyB, mode):
32+
def check_keys(keyA: int, keyB: int, mode: str) -> None:
3333
if mode == "encrypt":
3434
if keyA == 1:
3535
sys.exit(
@@ -90,7 +90,7 @@ def decrypt_message(key: int, message: str) -> str:
9090
return plainText
9191

9292

93-
def get_random_key():
93+
def get_random_key() -> int:
9494
while True:
9595
keyA = random.randint(2, len(SYMBOLS))
9696
keyB = random.randint(2, len(SYMBOLS))

ciphers/base64_cipher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def encode_base64(text):
1+
def encode_base64(text: str) -> str:
22
r"""
33
>>> encode_base64('WELCOME to base64 encoding 😁')
44
'V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ=='
@@ -33,7 +33,7 @@ def encode_base64(text):
3333
return r[0 : len(r) - len(p)] + p
3434

3535

36-
def decode_base64(text):
36+
def decode_base64(text: str) -> str:
3737
r"""
3838
>>> decode_base64('V0VMQ09NRSB0byBiYXNlNjQgZW5jb2Rpbmcg8J+YgQ==')
3939
'WELCOME to base64 encoding 😁'

ciphers/brute_force_caesar_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def decrypt(message):
1+
def decrypt(message: str) -> None:
22
"""
33
>>> decrypt('TMDETUX PMDVU')
44
Decryption using Key #0: TMDETUX PMDVU

ciphers/cryptomath_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
def gcd(a, b):
1+
def gcd(a: int, b: int) -> int:
22
while a != 0:
33
a, b = b % a, a
44
return b
55

66

7-
def findModInverse(a, m):
7+
def findModInverse(a: int, m: int) -> int:
88
if gcd(a, m) != 1:
99
return None
1010
u1, u2, u3 = 1, 0, a

ciphers/decrypt_caesar_with_chi_squared.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#!/usr/bin/env python3
22

3+
from typing import Tuple
4+
35

46
def decrypt_caesar_with_chi_squared(
57
ciphertext: str,
6-
cipher_alphabet=None,
7-
frequencies_dict=None,
8+
cipher_alphabet: str = None,
9+
frequencies_dict: str = None,
810
case_sensetive: bool = False,
9-
) -> tuple:
11+
) -> Tuple[int, float, str]:
1012
"""
1113
Basic Usage
1214
===========

ciphers/deterministic_miller_rabin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55

6-
def miller_rabin(n, allow_probable=False):
6+
def miller_rabin(n: int, allow_probable: bool = False) -> bool:
77
"""Deterministic Miller-Rabin algorithm for primes ~< 3.32e24.
88
99
Uses numerical analysis results to return whether or not the passed number
@@ -87,7 +87,7 @@ def miller_rabin(n, allow_probable=False):
8787
return True
8888

8989

90-
def test_miller_rabin():
90+
def test_miller_rabin() -> None:
9191
"""Testing a nontrivial (ends in 1, 3, 7, 9) composite
9292
and a prime in each range.
9393
"""

ciphers/diffie.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def find_primitive(n):
1+
def find_primitive(n: int) -> int:
22
for r in range(1, n):
33
li = []
44
for x in range(n - 1):

ciphers/elgamal_key_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def main():
1919
# so I used 4.80 Algorithm in
2020
# Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996)
2121
# and it seems to run nicely!
22-
def primitiveRoot(p_val):
22+
def primitiveRoot(p_val: int) -> int:
2323
print("Generating primitive root of p")
2424
while True:
2525
g = random.randrange(3, p_val)
@@ -30,7 +30,7 @@ def primitiveRoot(p_val):
3030
return g
3131

3232

33-
def generateKey(keySize):
33+
def generateKey(keySize: int) -> ((int, int, int, int), (int, int)):
3434
print("Generating prime p...")
3535
p = rabinMiller.generateLargePrime(keySize) # select large prime number.
3636
e_1 = primitiveRoot(p) # one primitive root on modulo p.
@@ -43,7 +43,7 @@ def generateKey(keySize):
4343
return publicKey, privateKey
4444

4545

46-
def makeKeyFiles(name, keySize):
46+
def makeKeyFiles(name: str, keySize: int):
4747
if os.path.exists("%s_pubkey.txt" % name) or os.path.exists(
4848
"%s_privkey.txt" % name
4949
):

ciphers/hill_cipher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class HillCipher:
6464

6565
to_int = numpy.vectorize(lambda x: round(x))
6666

67-
def __init__(self, encrypt_key):
67+
def __init__(self, encrypt_key: int):
6868
"""
6969
encrypt_key is an NxN numpy array
7070
"""

0 commit comments

Comments
 (0)