#!/usr/bin/env python # coding: utf-8 # This notebook was prepared by [Donne Martin](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges). # # Challenge Notebook # ## Problem: Implement a function to reverse a string (a list of characters), in-place. # # * [Constraints](#Constraints) # * [Test Cases](#Test-Cases) # * [Algorithm](#Algorithm) # * [Code](#Code) # * [Unit Test](#Unit-Test) # * [Solution Notebook](#Solution-Notebook) # ## Constraints # # * Can we assume the string is ASCII? # * Yes # * Note: Unicode strings could require special handling depending on your language # * Since we need to do this in-place, it seems we cannot use the slice operator or the reversed function? # * Correct # * Since Python string are immutable, can we use a list of characters instead? # * Yes # ## Test Cases # # * None -> None # * [''] -> [''] # * ['f', 'o', 'o', ' ', 'b', 'a', 'r'] -> ['r', 'a', 'b', ' ', 'o', 'o', 'f'] # ## Algorithm # # Refer to the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_solution.ipynb). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start. # ## Code # In[ ]: class ReverseString(object): def reverse(self, chars): # TODO: Implement me pass # ## Unit Test # # # **The following unit test is expected to fail until you solve the challenge.** # In[ ]: # %load test_reverse_string.py import unittest class TestReverse(unittest.TestCase): def test_reverse(self, func): self.assertEqual(func(None), None) self.assertEqual(func(['']), ['']) self.assertEqual(func( ['f', 'o', 'o', ' ', 'b', 'a', 'r']), ['r', 'a', 'b', ' ', 'o', 'o', 'f']) print('Success: test_reverse') def test_reverse_inplace(self, func): target_list = ['f', 'o', 'o', ' ', 'b', 'a', 'r'] func(target_list) self.assertEqual(target_list, ['r', 'a', 'b', ' ', 'o', 'o', 'f']) print('Success: test_reverse_inplace') def main(): test = TestReverse() reverse_string = ReverseString() test.test_reverse(reverse_string.reverse) test.test_reverse_inplace(reverse_string.reverse) if __name__ == '__main__': main() # ## Solution Notebook # # Review the [Solution Notebook](http://nbviewer.ipython.org/github/donnemartin/interactive-coding-challenges/blob/master/arrays_strings/reverse_string/reverse_string_solution.ipynb) for a discussion on algorithms and code solutions.