Skip to content

Bug in has_duplicates.py #102

Open
Open
@bigabdoul

Description

@bigabdoul

The two implementations of has_duplicates (https://github.com/AllenDowney/ThinkPython2/blob/master/code/has_duplicates.py) contain a bug when called with specific arguments. Since dictionary keys must be immutable (hashable types), if we pass a nested list as an argument, both functions will crash:

t = [1, 2, 3, [4, 5]]
print(has_duplicates(t))

The same is true for a set. To avoid this, you should convert the key to an immutable type (i.e., a string).

def has_duplicates(t):
    """Checks whether any element appears more than once in a sequence.
    Simple version using a for loop.
    t: sequence
    """
    d = {}
    for x in t:
        if str(x) in d:
            return True
        d[str(x)] = True
    return False

I don't even know if there is a fix for the second version has_duplicates2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions