mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Add tests without modifying code (#10740)
* Contributes to #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 #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 #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>
This commit is contained in:
parent
d73a4c2ee0
commit
0601b56173
|
@ -4,7 +4,27 @@ import random
|
|||
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 = []
|
||||
|
@ -17,7 +37,20 @@ class Onepad:
|
|||
|
||||
@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])
|
||||
|
|
|
@ -4,6 +4,19 @@ import math
|
|||
|
||||
|
||||
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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user