diff options
author | William Carroll <wpcarro@gmail.com> | 2020-02-18T14·29+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-02-19T15·01+0000 |
commit | ca6bd29ed8eaca616a1bc8ffceb68f00544eab36 (patch) | |
tree | cbc43135449a03ee4339f5459649830023e38d82 /scratch/deepmind/part_two/reverse-words.py | |
parent | acf1b8c4f046a36a387af1120f5d0744f95c4aa3 (diff) |
Solve bonus part of reverse-words
InterviewCake asks "How would you handle punctuation?". Without precise specs about what that entails, I'm supporting sentences ending with punctuation.
Diffstat (limited to 'scratch/deepmind/part_two/reverse-words.py')
-rw-r--r-- | scratch/deepmind/part_two/reverse-words.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/scratch/deepmind/part_two/reverse-words.py b/scratch/deepmind/part_two/reverse-words.py index d433ac594bcc..033d11244ca7 100644 --- a/scratch/deepmind/part_two/reverse-words.py +++ b/scratch/deepmind/part_two/reverse-words.py @@ -10,6 +10,9 @@ def reverse(xs, i, j): def reverse_words(xs): + punctuation = None + if len(xs) > 0 and xs[-1] in ".?!": + punctuation = xs.pop() reverse(xs, 0, len(xs) - 1) i = 0 j = i @@ -19,6 +22,8 @@ def reverse_words(xs): reverse(xs, i, j - 1) j += 1 i = j + if punctuation: + xs.append(punctuation) # Tests @@ -59,5 +64,11 @@ class Test(unittest.TestCase): expected = list('') self.assertEqual(message, expected) + def test_bonus_support_punctuation(self): + message = list('yummy is cake bundt chocolate this!') + reverse_words(message) + expected = list('this chocolate bundt cake is yummy!') + self.assertEqual(message, expected) + unittest.main(verbosity=2) |