Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
22 changes: 22 additions & 0 deletions Python/chapter01/1.1 - Is Unique/jesus_1.1.sol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import unittest
Copy link
Contributor

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.



def is_unique(s: str) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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):
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
self.assertEqual(is_unique('aaaaa'), False)
self.assertFalse(is_unique('aaaaa'))

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()