Created
May 28, 2014 08:50
-
-
Save GuillermoPena/1bd229aebbeb7030c9f0 to your computer and use it in GitHub Desktop.
CheckIO - Home Challenge 1 : Non-unique Elements
This file contains 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
# CheckIO - Home Challenge 1 : Non-unique Elements | |
# http://checkio.org | |
# You are given a non-empty list of integers (X). | |
# For this task, you should return a list consisting of only the non-unique elements in this list. | |
# To do so you will need to remove all unique elements (elements which are contained in a given list only once). | |
# When solving this task, do not change the order of the list. | |
# Example: [1, 2, 3, 1, 3] 1 and 3 non-unique elements and result will be [1, 3, 1, 3]. | |
def checkio(data): | |
result = [] | |
for item in data: | |
if (data.count(item) > 1): | |
result.append(item) | |
return result | |
#Some hints | |
#You can use list.count(element) method for counting. | |
#Create new list with non-unique elements | |
#or remove elements from original list (but it's bad practice for many real cases) | |
#Loop over original list | |
if __name__ == "__main__": | |
#These "asserts" using only for self-checking and not necessary for auto-testing | |
assert isinstance(checkio([1]), list), "The result must be a list" | |
assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example" | |
assert checkio([1, 2, 3, 4, 5]) == [], "2nd example" | |
assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example" | |
assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment