#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Implement an algorithm to determine if a string has all unique characters. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Can we assume the string is ASCII? # * Yes # * Note: Unicode strings could require special handling depending on your language # * Can we assume this is case sensitive? # * Yes # * Can we use additional data structures? # * Yes # * Can we assume this fits in memory? # * Yes # ## Test Cases # # * None -> False # * '' -> True # * 'foo' -> False # * 'bar' -> True # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/unique_chars/unique_chars_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: class UniqueChars(object): def has_unique_chars(self, string): # TODO: Implement me pass # ## Unit Test # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_unique_chars.py import unittest class TestUniqueChars(unittest.TestCase): def test_unique_chars(self, func): self.assertEqual(func(None), False) self.assertEqual(func(''), True) self.assertEqual(func('foo'), False) self.assertEqual(func('bar'), True) print('Success: test_unique_chars') def main(): test = TestUniqueChars() unique_chars = UniqueChars() test.test_unique_chars(unique_chars.has_unique_chars) try: unique_chars_set = UniqueCharsSet() test.test_unique_chars(unique_chars_set.has_unique_chars) unique_chars_in_place = UniqueCharsInPlace() test.test_unique_chars(unique_chars_in_place.has_unique_chars) except NameError: # Alternate solutions are only defined # in the solutions file pass if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/unique_chars/unique_chars_solution.ipynb) for a discussion on algorithms and code solutions.