'''data1.py Jed Yang, 2016-11-12 Getting unique elements of a list. ''' # Our own destructive function! def removeDuplicates(array): '''Remove duplicated items in a list.''' for item in array: # count how many times the item occurs in array tally = array.count(item) if tally > 1: # remove the first occurrence of item in array array.remove(item) # return None to remind users of the destructive nature return None digits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9] print('input:', digits) result = removeDuplicates(digits) print('input:', digits) print('result:', result) print() print('======\n') ################################################################################ def getUniqueElements(array): '''Takes a *sorted* list as input. Returns a new list containing the unique elements of the input. ''' # As you read the code below, think about why I require array to be sorted. unique = [] last = None for item in array: # What is going on here? if item == last: continue last = item unique.append(item) return unique def test(array): print('input:', array) result = getUniqueElements(array) print('input:', array) print('result:', result) print() test([1, 1, 1, 3, 3, 4, 4, 6, 6, 6]) test([1, 8, 1, 8, 1, 8]) test([4, 4, 4, 'apple', 'apple', 7, 7, 'apple', 4, 4, 4, 4, 4]) # Can you make sense of all the behaviour? Explain ASAP (as simply as # possible) what getUniqueElements() does when a list is not sorted.