Created
October 15, 2020 13:40
-
-
Save jjfiv/658e80c3c487ae106bc2a00cb836eeb5 to your computer and use it in GitHub Desktop.
has_duplicates; filter_duplicates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def has_duplicates(xs): | |
# Now with dictionaries: | |
# create a dictionary "seen_already" that is empty. | |
# loop over every element of xs | |
# if an element is in seen_already, return True | |
# else; add it to seen_already | |
# if we haven't returned True by the end of the function, return False | |
return False | |
assert(has_duplicates([1,2]) == False) | |
assert(has_duplicates([1,1,2]) == True) | |
assert(has_duplicates([1,2,1]) == True) | |
assert(has_duplicates(["a", "b", "b"]) == True) | |
def filter_duplicates(xs): | |
output = [] | |
# Now, collect only those items you haven't seen_before into output | |
return output | |
assert(filter_duplicates([1,2]) == [1,2]) | |
assert(filter_duplicates([1,1,2]) == [1,2]) | |
assert(filter_duplicates([1,2,1]) == [1,2]) | |
assert(filter_duplicates(["a", "b", "b"]) == ["a", "b"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment