<jupyter_start><jupyter_text>This notebook was prepared by [<NAME>](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  AlgorithmRefer 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<jupyter_code>class UniqueChars(object):

    def has_unique_chars(self, string):
        return ((string is not None)
                and (len(set(string)) == len(string)))
        #if string is None:
        #    return False
        #return not(len(set(string)) < len(string))
# My recursive UniqueChars
class UniqueCharsRec(object):
    def has_unique_chars(self, string):
        if string is None:
            return False
        if string == '':
            return True
        if string.count(string[0]) > 1:
            return False
        return self.has_unique_chars(string[1:])<jupyter_output><empty_output><jupyter_text>Unit Test **The following unit test is expected to fail until you solve the challenge.**<jupyter_code># %load test_unique_chars.py
import cProfile
from nose.tools import assert_equal


class TestUniqueChars(object):

    def test_unique_chars(self, func):
        assert_equal(func(None), False)
        assert_equal(func(''), True)
        assert_equal(func('foo'), False)
        assert_equal(func('bar'), True)
        print('Success: test_unique_chars')


def main():
    test = TestUniqueChars()
    unique_chars = UniqueChars()
    # test.test_unique_chars(unique_chars.has_unique_chars)
    cProfile.runctx('test.test_unique_chars(unique_chars.has_unique_chars)',
                   globals=globals(),
                   locals=locals())
    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
    try:
        unique_chars_rec = UniqueCharsRec()
        cProfile.runctx('test.test_unique_chars(unique_chars_rec.has_unique_chars)',globals=globals(), locals=locals())
        # test.test_unique_chars(unique_chars_rec.has_unique_chars)
    except NameError:
        pass

if __name__ == '__main__':
    main()<jupyter_output>Success: test_unique_chars
         65 function calls in 0.001 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        4    0.000    0.000    0.000    0.000 <ipython-input-29-6610d2bc22f0>:3(has_unique_chars)
        1    0.000    0.000    0.001    0.001 <ipython-input-31-a5f7b841ae2e>:8(test_unique_chars)
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
        4    0.000    0.000    0.000    0.000 case.py:801(_getAssertEqualityFunc)
        4    0.000    0.000    0.000    0.000 case.py:827(_baseAssertEqual)
        4    0.000    0.000    0.000    0.000 case.py:834(assertEqual)
        3    0.000    0.000    0.000    0.000 iostream.py:197(schedule)
        2    0.000    0.000    0.000    0.000 iostream.py:309(_is_master_process)
        2    0.000    0.000    0.000    0.000 iostream.py:322(_schedule_flush)
        2    0.000    0.000    0.000    0.000 iostream.py:384(write)
        3    0.000    0.000    [...]
{'fraction_non_alphanumeric': 0.10167064439140812, 'fraction_numerical': 0.05489260143198091, 'mean_word_length': 2.942615239887112, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '15312', 'license': 'Apache-2.0', 'n_tokens_mistral': 1449, 'n_tokens_neox': 1297, 'n_words': 320, 'path': 'arrays_strings/unique_chars/my_unique_chars_challenge_nb.ipynb', 'repo_name': 'jjwatt/interactive-coding-challenges', 'size': '4196'}