Skip to content

Commit 99198c7

Browse files
gio-puterpre-commit-ci[bot]
authored andcommitted
Add tests without modifying code (TheAlgorithms#10740)
* Contributes to TheAlgorithms#9943 Added doctest to largest_of_very_large_numbers.py Added doctest to word_patterns.py Added doctest to onepad_cipher.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Contributes to TheAlgorithms#9943 Added doctest to maths/largest_of_very_large_numbers.py Added doctest to strings/word_patterns.py Added doctest to ciphers/onepad_cipher.py * Add tests without modifying code TheAlgorithms#10740 Added test to maths/largest_of_very_large_numbers Added test to strings/word_patterns.py Added test to ciphers/onepad_cipher.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 8597026 commit 99198c7

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

Diff for: ciphers/onepad_cipher.py

+35-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,27 @@
44
class Onepad:
55
@staticmethod
66
def encrypt(text: str) -> tuple[list[int], list[int]]:
7-
"""Function to encrypt text using pseudo-random numbers"""
7+
"""
8+
Function to encrypt text using pseudo-random numbers
9+
>>> Onepad().encrypt("")
10+
([], [])
11+
>>> Onepad().encrypt([])
12+
([], [])
13+
>>> random.seed(1)
14+
>>> Onepad().encrypt(" ")
15+
([6969], [69])
16+
>>> random.seed(1)
17+
>>> Onepad().encrypt("Hello")
18+
([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
19+
>>> Onepad().encrypt(1)
20+
Traceback (most recent call last):
21+
...
22+
TypeError: 'int' object is not iterable
23+
>>> Onepad().encrypt(1.1)
24+
Traceback (most recent call last):
25+
...
26+
TypeError: 'float' object is not iterable
27+
"""
828
plain = [ord(i) for i in text]
929
key = []
1030
cipher = []
@@ -17,7 +37,20 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:
1737

1838
@staticmethod
1939
def decrypt(cipher: list[int], key: list[int]) -> str:
20-
"""Function to decrypt text using pseudo-random numbers."""
40+
"""
41+
Function to decrypt text using pseudo-random numbers.
42+
>>> Onepad().decrypt([], [])
43+
''
44+
>>> Onepad().decrypt([35], [])
45+
''
46+
>>> Onepad().decrypt([], [35])
47+
Traceback (most recent call last):
48+
...
49+
IndexError: list index out of range
50+
>>> random.seed(1)
51+
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
52+
'Hello'
53+
"""
2154
plain = []
2255
for i in range(len(key)):
2356
p = int((cipher[i] - (key[i]) ** 2) / key[i])

Diff for: maths/largest_of_very_large_numbers.py

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44

55

66
def res(x, y):
7+
"""
8+
Reduces large number to a more manageable number
9+
>>> res(5, 7)
10+
4.892790030352132
11+
>>> res(0, 5)
12+
0
13+
>>> res(3, 0)
14+
1
15+
>>> res(-1, 5)
16+
Traceback (most recent call last):
17+
...
18+
ValueError: math domain error
19+
"""
720
if 0 not in (x, y):
821
# We use the relation x^y = y*log10(x), where 10 is the base.
922
return y * math.log10(x)

Diff for: strings/word_patterns.py

+21
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
def get_word_pattern(word: str) -> str:
22
"""
3+
Returns numerical pattern of character appearances in given word
4+
>>> get_word_pattern("")
5+
''
6+
>>> get_word_pattern(" ")
7+
'0'
38
>>> get_word_pattern("pattern")
49
'0.1.2.2.3.4.5'
510
>>> get_word_pattern("word pattern")
611
'0.1.2.3.4.5.6.7.7.8.2.9'
712
>>> get_word_pattern("get word pattern")
813
'0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10'
14+
>>> get_word_pattern()
15+
Traceback (most recent call last):
16+
...
17+
TypeError: get_word_pattern() missing 1 required positional argument: 'word'
18+
>>> get_word_pattern(1)
19+
Traceback (most recent call last):
20+
...
21+
AttributeError: 'int' object has no attribute 'upper'
22+
>>> get_word_pattern(1.1)
23+
Traceback (most recent call last):
24+
...
25+
AttributeError: 'float' object has no attribute 'upper'
26+
>>> get_word_pattern([])
27+
Traceback (most recent call last):
28+
...
29+
AttributeError: 'list' object has no attribute 'upper'
930
"""
1031
word = word.upper()
1132
next_num = 0

0 commit comments

Comments
 (0)