Skip to content

Conversation

jsanchez254
Copy link

No description provided.

import unittest


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.

import unittest


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.

good job on adding typing information! :D

class TestIsUnique(unittest.TestCase):

def test_is_unique(self):
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

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


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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants