-
Notifications
You must be signed in to change notification settings - Fork 95
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
Jesus 1.1 - is_unique [Python] #44
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,22 @@ | ||||||
import unittest | ||||||
|
||||||
|
||||||
def is_unique(s: str) -> bool: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a docstring? In the docstring, please document what the runtime+space complexity is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good job on adding typing information! :D |
||||||
for x in s: | ||||||
if s.count(x) > 1: | ||||||
return False | ||||||
return True | ||||||
|
||||||
class TestIsUnique(unittest.TestCase): | ||||||
|
||||||
def test_is_unique(self): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be okay for now, but for more complex tests, Golang has this wonderful pattern called table driven tests, where you can restructure the code the following way: for s, expected in [
('aaaaa', False),
('abc', True),
...
('a', True),
]:
self.assertEqual(is_unique(s), expected, msg=s) to cut down on repetition. for even further de-duplication, you can split the test in positive and negative tests: def test_is_unique(self):
for s in [
'abc',
....
'',
]:
self.assertTrue(is_unique(s), msg=s)
def test_is_not_unique(self):
for s in [
'aaaaa',
....
'abcda',
]:
self.assertFalse(is_unique(s), msg=s) |
||||||
self.assertEqual(is_unique('aaaaa'), False) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there are better assertions you can use: https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertTrue
Suggested change
same below |
||||||
self.assertEqual(is_unique('abc'), True) | ||||||
self.assertEqual(is_unique('abcda'), False) | ||||||
self.assertEqual(is_unique('jhfbwej'), False) | ||||||
self.assertEqual(is_unique(''), True) | ||||||
self.assertEqual(is_unique('a'), True) | ||||||
|
||||||
|
||||||
if __name__ == '__main__': | ||||||
unittest.main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible for you to add a module docstring with the problem statement? Not all (volunteer!) reviewers have a copy of the book.