最近想了下还是需要捡起python的基础,所以我决定重新刷一遍checkIo,来帮助自己提升python的基础。
这里放出连接有兴趣的同学可以尝试。我将会不定期的更新,有兴趣的同学可以关注下。
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].
Input: A list of integers.
Output: The list of integers.
checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3]
checkio([1, 2, 3, 4, 5]) == []
checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5]
checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9]
# -*- coding: gbk -*-
'''
Created on Nov 10, 2019
@author: XiGong
'''
def checkio(val):
valS=sorted(val)
b=[]
# 得到数组总长度
for i in range(len(valS)):
# 如果长度为1直接添加进b
if(len(valS)==1):
b.append(valS[i])
# 如果>1就要进行下一步判断
else:
# 当i在倒数第一之间时
if i==len(valS)-1:
# 如果左边不等于i就添加进b
if(valS[i]!=valS[i-1]):
b.append(valS[i])
# 当i在倒数第0之间时
elif i==0:
# 如果右边不等于i就添加进b
if(valS[i]!=valS[i+1]):
b.append(valS[i])
# 当i在1和倒数第二之间时
else:
if (valS[i]!=valS[i-1]) and (valS[i]!=valS[i+1]):
b.append(valS[i])
# 去除相同的
for y in range(len(b)):
val.remove(b[y])
return val
if __name__ == "__main__":
#These "asserts" using only for self-checking and not necessary for auto-testing
assert list(checkio([1, 2, 3, 1, 3])) == [1, 3, 1, 3], "1st example"
assert list(checkio([1, 2, 3, 4, 5])) == [], "2nd example"
assert list(checkio([5, 5, 5, 5, 5])) == [5, 5, 5, 5, 5], "3rd example"
assert list(checkio([10, 9, 10, 10, 9, 8])) == [10, 9, 10, 10, 9], "4th example"
print("It is all good. Let's check it now")
网友评论