This notebook was prepared by [wdonahoe](https://github.com/wdonahoe). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).

Challenge Notebook

Problem: Implement a function that groups identical items based on their order in the list.

Constraints

  • Can we use extra data structures?
    • Yes

Test Cases

  • group_ordered([1,2,1,3,2]) -> [1,1,2,2,3]
  • group_ordered(['a','b','a') -> ['a','a','b']
  • group_ordered([1,1,2,3,4,5,2,1]-> [1,1,1,2,2,3,4,5]
  • group_ordered([]) -> []
  • group_ordered([1]) -> [1]
  • group_ordered(None) -> None

Algorithm

Refer to the solution notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.

Code


In [ ]:
def group_ordered(list_in):
    # TODO: Implement me
    pass

Unit Test

The following unit test is expected to fail until you solve the challenge.


In [ ]:
# %load test_group_ordered.py
from nose.tools import assert_equal


class TestGroupOrdered(object):
    def test_group_ordered(self, func):

        assert_equal(func(None), None)
        print('Success: ' + func.__name__ + " None case.")
        assert_equal(func([]), [])
        print('Success: ' + func.__name__ + " Empty case.")
        assert_equal(func([1]), [1])
        print('Success: ' + func.__name__ + " Single element case.")
        assert_equal(func([1, 2, 1, 3, 2]), [1, 1, 2, 2, 3])
        assert_equal(func(['a', 'b', 'a']), ['a', 'a', 'b'])
        assert_equal(func([1, 1, 2, 3, 4, 5, 2, 1]), [1, 1, 1, 2, 2, 3, 4, 5])
        assert_equal(func([1, 2, 3, 4, 3, 4]), [1, 2, 3, 3, 4, 4])
        print('Success: ' + func.__name__)


def main():
    test = TestGroupOrdered()
    test.test_group_ordered(group_ordered)
    try:
        test.test_group_ordered(group_ordered_alt)
    except NameError:
        # Alternate solutions are only defined
        # in the solutions file
        pass

if __name__ == '__main__':
    main()

Solution Notebook

Review the solution notebook for a discussion on algorithms and code solutions.