#!/usr/bin/python -t # Copyright 2008 Google Inc. All Rights Reserved. """Additional, optional List mini exercises.""" __author__ = 'nparlante@google.com (Nick Parlante)' # A. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def RemoveAdjacent(nums): """Collapse identical adjacent elements in a list.""" # +++your code here+++ return # B. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def LinearMerge(list1, list2): """Merge two lists sorted increasing order.""" # +++your code here+++ return # Simple provided Test() function used in main() to print # what each function returns vs. what it's supposed to return. def Test(got, expected): if got == expected: prefix = ' OK ' else: prefix = 'FAIL' print '%s got: %s expected: %s' % (prefix, repr(got), repr(expected)) # Calls the above functions with interesting inputs. def main(): print 'RemoveAdjacent' Test(RemoveAdjacent([1, 2, 2, 3]), [1, 2, 3]) Test(RemoveAdjacent([2, 2, 3, 3, 3]), [2, 3]) Test(RemoveAdjacent([1, 2, 2, 3, 2]), [1, 2, 3, 2]) Test(RemoveAdjacent([1]), [1]) Test(RemoveAdjacent([]), []) print print 'LinearMerge' Test(LinearMerge(['aa', 'xx', 'zz'], ['bb', 'cc']), ['aa', 'bb', 'cc', 'xx', 'zz']) Test(LinearMerge(['aa', 'xx'], ['bb', 'cc', 'zz']), ['aa', 'bb', 'cc', 'xx', 'zz']) Test(LinearMerge(['aa', 'aa'], ['aa', 'bb', 'bb']), ['aa', 'aa', 'aa', 'bb', 'bb']) if __name__ == '__main__': main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question