def unique_iterator(iterable): last = None for x in iterable: if x != last: yield x last = x def unique_in_order(iterable): return list(unique_iterator(iterable))
- def unique_iterator(iterable):
- last = None
- for x in iterable:
- if x != last:
- yield x
- last = x
- def unique_in_order(iterable):
iterable = list(iterable)while 1==1:for x in range(len(iterable)):if iterable[x]==iterable[x-1]:del iterable[x]breakif x==len(iterable)-1:return iterable- return list(unique_iterator(iterable))
test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) test.assert_equals(unique_in_order('44444455321111'), ['4','5','3','2','1']) test.assert_equals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B']) test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B']) test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCsssCCCCCCCCCCCCCDAABBB'), ['A','B','C','s', 'C','D','A','B'])
- test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- test.assert_equals(unique_in_order('44444455321111'), ['4','5','3','2','1'])
- test.assert_equals(unique_in_order('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])
- test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDAABBB'), ['A','B','C','D','A','B'])
- test.assert_equals(unique_in_order('AAAABBBCCCCCCCCCCCCCCCCCCCCCCCCDAABBB'), ['A','B','C','D','A','B'])