Skip to content

Add tests without modifying code #10740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions ciphers/onepad_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
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("")
([], [])
>>> random.seed(1)
>>> Onepad().encrypt(" ")
([6969], [69])
>>> random.seed(1)
>>> Onepad().encrypt("Hello")
([9729, 114756, 4653, 31309, 10492], [69, 292, 33, 131, 61])
"""
plain = [ord(i) for i in text]
key = []
cipher = []
Expand All @@ -17,7 +27,21 @@ 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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

>>> 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
17 changes: 17 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,23 @@


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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better if the tests do not require extra scrolling when someone is reading the code. If they want to read all the tests, they can take the time to do so but often readers will want to look at the function signature and then jump straight to the code. Keeping the tests in tight block will allow them them to study the function signature and the code without too much visual distraction.

Suggested change
>>> res(0, 5)
0
>>> res(3, 0)
1
>>> res(-1, 5)
Traceback (most recent call last):
...
ValueError: math domain error
>>> 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
9 changes: 9 additions & 0 deletions strings/word_patterns.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
def get_word_pattern(word: str) -> str:
"""
Returns numerical pattern of character appearances in given word
>>> get_word_pattern()
Traceback (most recent call last):
...
TypeError: get_word_pattern() missing 1 required positional argument: 'word'
>>> get_word_pattern("")
''
>>> get_word_pattern(" ")
'0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the tests that work correctly first and then afterward the ones that raise exceptions.

Also, please add:
>>> get_word_pattern(1)
>>> get_word_pattern(1.1)
>>> get_word_pattern([])

>>> get_word_pattern("pattern")
'0.1.2.2.3.4.5'
>>> get_word_pattern("word pattern")
Expand Down