Skip to content

Commit d904636

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents a0e996c + 7ef2e4d commit d904636

File tree

18 files changed

+942
-113
lines changed

18 files changed

+942
-113
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ jobs:
2323
python -m pip install mypy pytest-cov -r requirements.txt
2424
- run: mypy --install-types --non-interactive .
2525
- name: Run tests
26-
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/ --cov-report=term-missing:skip-covered --cov=. .
26+
run: pytest --doctest-modules --ignore=project_euler/ --ignore=scripts/validate_solutions.py --cov-report=term-missing:skip-covered --cov=. .
2727
- if: ${{ success() }}
2828
run: scripts/build_directory_md.py 2>&1 | tee DIRECTORY.md

DIRECTORY.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
* [A1Z26](https://github.com/TheAlgorithms/Python/blob/master/ciphers/a1z26.py)
5454
* [Affine Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/affine_cipher.py)
5555
* [Atbash](https://github.com/TheAlgorithms/Python/blob/master/ciphers/atbash.py)
56+
* [Baconian Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/baconian_cipher.py)
5657
* [Base16](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base16.py)
5758
* [Base32](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base32.py)
5859
* [Base64 Encoding](https://github.com/TheAlgorithms/Python/blob/master/ciphers/base64_encoding.py)
@@ -70,7 +71,7 @@
7071
* [Hill Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/hill_cipher.py)
7172
* [Mixed Keyword Cypher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mixed_keyword_cypher.py)
7273
* [Mono Alphabetic Ciphers](https://github.com/TheAlgorithms/Python/blob/master/ciphers/mono_alphabetic_ciphers.py)
73-
* [Morse Code Implementation](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code_implementation.py)
74+
* [Morse Code](https://github.com/TheAlgorithms/Python/blob/master/ciphers/morse_code.py)
7475
* [Onepad Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/onepad_cipher.py)
7576
* [Playfair Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/playfair_cipher.py)
7677
* [Porta Cipher](https://github.com/TheAlgorithms/Python/blob/master/ciphers/porta_cipher.py)
@@ -264,6 +265,7 @@
264265
* [Sum Of Subset](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/sum_of_subset.py)
265266

266267
## Electronics
268+
* [Carrier Concentration](https://github.com/TheAlgorithms/Python/blob/master/electronics/carrier_concentration.py)
267269
* [Electric Power](https://github.com/TheAlgorithms/Python/blob/master/electronics/electric_power.py)
268270
* [Ohms Law](https://github.com/TheAlgorithms/Python/blob/master/electronics/ohms_law.py)
269271

@@ -327,10 +329,12 @@
327329
* [Graph Matrix](https://github.com/TheAlgorithms/Python/blob/master/graphs/graph_matrix.py)
328330
* [Graphs Floyd Warshall](https://github.com/TheAlgorithms/Python/blob/master/graphs/graphs_floyd_warshall.py)
329331
* [Greedy Best First](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_best_first.py)
332+
* [Greedy Min Vertex Cover](https://github.com/TheAlgorithms/Python/blob/master/graphs/greedy_min_vertex_cover.py)
330333
* [Kahns Algorithm Long](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_long.py)
331334
* [Kahns Algorithm Topo](https://github.com/TheAlgorithms/Python/blob/master/graphs/kahns_algorithm_topo.py)
332335
* [Karger](https://github.com/TheAlgorithms/Python/blob/master/graphs/karger.py)
333336
* [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/graphs/markov_chain.py)
337+
* [Matching Min Vertex Cover](https://github.com/TheAlgorithms/Python/blob/master/graphs/matching_min_vertex_cover.py)
334338
* [Minimum Spanning Tree Boruvka](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_boruvka.py)
335339
* [Minimum Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal.py)
336340
* [Minimum Spanning Tree Kruskal2](https://github.com/TheAlgorithms/Python/blob/master/graphs/minimum_spanning_tree_kruskal2.py)
@@ -346,6 +350,9 @@
346350
* [Test Min Spanning Tree Kruskal](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_kruskal.py)
347351
* [Test Min Spanning Tree Prim](https://github.com/TheAlgorithms/Python/blob/master/graphs/tests/test_min_spanning_tree_prim.py)
348352

353+
## Greedy Methods
354+
* [Optimal Merge Pattern](https://github.com/TheAlgorithms/Python/blob/master/greedy_methods/optimal_merge_pattern.py)
355+
349356
## Hashes
350357
* [Adler32](https://github.com/TheAlgorithms/Python/blob/master/hashes/adler32.py)
351358
* [Chaos Machine](https://github.com/TheAlgorithms/Python/blob/master/hashes/chaos_machine.py)
@@ -436,6 +443,7 @@
436443
* [Euclidean Distance](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_distance.py)
437444
* [Euclidean Gcd](https://github.com/TheAlgorithms/Python/blob/master/maths/euclidean_gcd.py)
438445
* [Euler Method](https://github.com/TheAlgorithms/Python/blob/master/maths/euler_method.py)
446+
* [Euler Modified](https://github.com/TheAlgorithms/Python/blob/master/maths/euler_modified.py)
439447
* [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py)
440448
* [Extended Euclidean Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/extended_euclidean_algorithm.py)
441449
* [Factorial Iterative](https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_iterative.py)
@@ -492,6 +500,7 @@
492500
* [Prime Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_numbers.py)
493501
* [Prime Sieve Eratosthenes](https://github.com/TheAlgorithms/Python/blob/master/maths/prime_sieve_eratosthenes.py)
494502
* [Primelib](https://github.com/TheAlgorithms/Python/blob/master/maths/primelib.py)
503+
* [Proth Number](https://github.com/TheAlgorithms/Python/blob/master/maths/proth_number.py)
495504
* [Pythagoras](https://github.com/TheAlgorithms/Python/blob/master/maths/pythagoras.py)
496505
* [Qr Decomposition](https://github.com/TheAlgorithms/Python/blob/master/maths/qr_decomposition.py)
497506
* [Quadratic Equations Complex Numbers](https://github.com/TheAlgorithms/Python/blob/master/maths/quadratic_equations_complex_numbers.py)
@@ -946,6 +955,7 @@
946955
* [Current Stock Price](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_stock_price.py)
947956
* [Current Weather](https://github.com/TheAlgorithms/Python/blob/master/web_programming/current_weather.py)
948957
* [Daily Horoscope](https://github.com/TheAlgorithms/Python/blob/master/web_programming/daily_horoscope.py)
958+
* [Download Images From Google Query](https://github.com/TheAlgorithms/Python/blob/master/web_programming/download_images_from_google_query.py)
949959
* [Emails From Url](https://github.com/TheAlgorithms/Python/blob/master/web_programming/emails_from_url.py)
950960
* [Fetch Bbc News](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_bbc_news.py)
951961
* [Fetch Github Info](https://github.com/TheAlgorithms/Python/blob/master/web_programming/fetch_github_info.py)

ciphers/baconian_cipher.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""
2+
Program to encode and decode Baconian or Bacon's Cipher
3+
Wikipedia reference : https://en.wikipedia.org/wiki/Bacon%27s_cipher
4+
"""
5+
6+
encode_dict = {
7+
"a": "AAAAA",
8+
"b": "AAAAB",
9+
"c": "AAABA",
10+
"d": "AAABB",
11+
"e": "AABAA",
12+
"f": "AABAB",
13+
"g": "AABBA",
14+
"h": "AABBB",
15+
"i": "ABAAA",
16+
"j": "BBBAA",
17+
"k": "ABAAB",
18+
"l": "ABABA",
19+
"m": "ABABB",
20+
"n": "ABBAA",
21+
"o": "ABBAB",
22+
"p": "ABBBA",
23+
"q": "ABBBB",
24+
"r": "BAAAA",
25+
"s": "BAAAB",
26+
"t": "BAABA",
27+
"u": "BAABB",
28+
"v": "BBBAB",
29+
"w": "BABAA",
30+
"x": "BABAB",
31+
"y": "BABBA",
32+
"z": "BABBB",
33+
" ": " ",
34+
}
35+
36+
37+
decode_dict = {value: key for key, value in encode_dict.items()}
38+
39+
40+
def encode(word: str) -> str:
41+
"""
42+
Encodes to Baconian cipher
43+
44+
>>> encode("hello")
45+
'AABBBAABAAABABAABABAABBAB'
46+
>>> encode("hello world")
47+
'AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB'
48+
>>> encode("hello world!")
49+
Traceback (most recent call last):
50+
...
51+
Exception: encode() accepts only letters of the alphabet and spaces
52+
"""
53+
encoded = ""
54+
for letter in word.lower():
55+
if letter.isalpha() or letter == " ":
56+
encoded += encode_dict[letter]
57+
else:
58+
raise Exception("encode() accepts only letters of the alphabet and spaces")
59+
return encoded
60+
61+
62+
def decode(coded: str) -> str:
63+
"""
64+
Decodes from Baconian cipher
65+
66+
>>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB")
67+
'hello world'
68+
>>> decode("AABBBAABAAABABAABABAABBAB")
69+
'hello'
70+
>>> decode("AABBBAABAAABABAABABAABBAB BABAAABBABBAAAAABABAAAABB!")
71+
Traceback (most recent call last):
72+
...
73+
Exception: decode() accepts only 'A', 'B' and spaces
74+
"""
75+
if set(coded) - {"A", "B", " "} != set():
76+
raise Exception("decode() accepts only 'A', 'B' and spaces")
77+
decoded = ""
78+
for word in coded.split():
79+
while len(word) != 0:
80+
decoded += decode_dict[word[:5]]
81+
word = word[5:]
82+
decoded += " "
83+
return decoded.strip()
84+
85+
86+
if "__name__" == "__main__":
87+
from doctest import testmod
88+
89+
testmod()

ciphers/morse_code.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
Python program to translate to and from Morse code.
5+
6+
https://en.wikipedia.org/wiki/Morse_code
7+
"""
8+
9+
# fmt: off
10+
MORSE_CODE_DICT = {
11+
"A": ".-", "B": "-...", "C": "-.-.", "D": "-..", "E": ".", "F": "..-.", "G": "--.",
12+
"H": "....", "I": "..", "J": ".---", "K": "-.-", "L": ".-..", "M": "--", "N": "-.",
13+
"O": "---", "P": ".--.", "Q": "--.-", "R": ".-.", "S": "...", "T": "-", "U": "..-",
14+
"V": "...-", "W": ".--", "X": "-..-", "Y": "-.--", "Z": "--..", "1": ".----",
15+
"2": "..---", "3": "...--", "4": "....-", "5": ".....", "6": "-....", "7": "--...",
16+
"8": "---..", "9": "----.", "0": "-----", "&": ".-...", "@": ".--.-.",
17+
":": "---...", ",": "--..--", ".": ".-.-.-", "'": ".----.", '"': ".-..-.",
18+
"?": "..--..", "/": "-..-.", "=": "-...-", "+": ".-.-.", "-": "-....-",
19+
"(": "-.--.", ")": "-.--.-", "!": "-.-.--", " ": "/"
20+
} # Exclamation mark is not in ITU-R recommendation
21+
# fmt: on
22+
REVERSE_DICT = {value: key for key, value in MORSE_CODE_DICT.items()}
23+
24+
25+
def encrypt(message: str) -> str:
26+
"""
27+
>>> encrypt("Sos!")
28+
'... --- ... -.-.--'
29+
>>> encrypt("SOS!") == encrypt("sos!")
30+
True
31+
"""
32+
return " ".join(MORSE_CODE_DICT[char] for char in message.upper())
33+
34+
35+
def decrypt(message: str) -> str:
36+
"""
37+
>>> decrypt('... --- ... -.-.--')
38+
'SOS!'
39+
"""
40+
return "".join(REVERSE_DICT[char] for char in message.split())
41+
42+
43+
def main() -> None:
44+
"""
45+
>>> s = "".join(MORSE_CODE_DICT)
46+
>>> decrypt(encrypt(s)) == s
47+
True
48+
"""
49+
message = "Morse code here!"
50+
print(message)
51+
message = encrypt(message)
52+
print(message)
53+
message = decrypt(message)
54+
print(message)
55+
56+
57+
if __name__ == "__main__":
58+
main()

ciphers/morse_code_implementation.py

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)