Skip to content

Commit

Permalink
Add tests without modifying code (TheAlgorithms#10740)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
gio-puter and pre-commit-ci[bot] authored Oct 22, 2023
1 parent d73a4c2 commit 0601b56
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
37 changes: 35 additions & 2 deletions ciphers/onepad_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,27 @@
class Onepad:
@staticmethod
def encrypt(text: str) -> tuple[list[int], list[int]]:
"""Function to encrypt text using pseudo-random numbers"""
"""
Function to encrypt text using pseudo-random numbers
>>> Onepad().encrypt("")
([], [])
>>> Onepad().encrypt([])
([], [])
>>> random.seed(1)
>>> Onepad().encrypt(" ")
([6969], [69])
>>> random.seed(1)
>>> Onepad().encrypt("Hello")
([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
>>> Onepad().encrypt(1)
Traceback (most recent call last):
...
TypeError: 'int' object is not iterable
>>> Onepad().encrypt(1.1)
Traceback (most recent call last):
...
TypeError: 'float' object is not iterable
"""
plain = [ord(i) for i in text]
key = []
cipher = []
Expand All @@ -17,7 +37,20 @@ def encrypt(text: str) -> tuple[list[int], list[int]]:

@staticmethod
def decrypt(cipher: list[int], key: list[int]) -> str:
"""Function to decrypt text using pseudo-random numbers."""
"""
Function to decrypt text using pseudo-random numbers.
>>> Onepad().decrypt([], [])
''
>>> Onepad().decrypt([35], [])
''
>>> Onepad().decrypt([], [35])
Traceback (most recent call last):
...
IndexError: list index out of range
>>> random.seed(1)
>>> Onepad().decrypt([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
'Hello'
"""
plain = []
for i in range(len(key)):
p = int((cipher[i] - (key[i]) ** 2) / key[i])
Expand Down
13 changes: 13 additions & 0 deletions maths/largest_of_very_large_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@


def res(x, y):
"""
Reduces large number to a more manageable number
>>> res(5, 7)
4.892790030352132
>>> res(0, 5)
0
>>> res(3, 0)
1
>>> res(-1, 5)
Traceback (most recent call last):
...
ValueError: math domain error
"""
if 0 not in (x, y):
# We use the relation x^y = y*log10(x), where 10 is the base.
return y * math.log10(x)
Expand Down
21 changes: 21 additions & 0 deletions strings/word_patterns.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
def get_word_pattern(word: str) -> str:
"""
Returns numerical pattern of character appearances in given word
>>> get_word_pattern("")
''
>>> get_word_pattern(" ")
'0'
>>> get_word_pattern("pattern")
'0.1.2.2.3.4.5'
>>> get_word_pattern("word pattern")
'0.1.2.3.4.5.6.7.7.8.2.9'
>>> get_word_pattern("get word pattern")
'0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10'
>>> get_word_pattern()
Traceback (most recent call last):
...
TypeError: get_word_pattern() missing 1 required positional argument: 'word'
>>> get_word_pattern(1)
Traceback (most recent call last):
...
AttributeError: 'int' object has no attribute 'upper'
>>> get_word_pattern(1.1)
Traceback (most recent call last):
...
AttributeError: 'float' object has no attribute 'upper'
>>> get_word_pattern([])
Traceback (most recent call last):
...
AttributeError: 'list' object has no attribute 'upper'
"""
word = word.upper()
next_num = 0
Expand Down

0 comments on commit 0601b56

Please sign in to comment.