-
Notifications
You must be signed in to change notification settings - Fork 53
/
27-reverse-words.py
executable file
·101 lines (70 loc) · 2.85 KB
/
27-reverse-words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!python
from __future__ import print_function
import unittest
######################################################################
# this problem is from
# https://www.interviewcake.com/question/python/reverse-words
#
# You're working on a secret team solving coded transmissions.
# Your team is scrambling to decipher a recent message, worried it's a
# plot to break into a major European National Cake Vault. The message
# has been mostly deciphered, but all the words are backwards! Your
# colleagues have handed off the last step to you.
# Write a function reverse_words() that takes a string message and
# reverses the order of the words in-place
# For example:
# message = 'find you will pain only go you recordings security the into if'
# reverse_words(message)
# returns: 'if into the security recordings you go only pain will you find'
# When writing your function, assume the message contains only letters
# and spaces, and all words are separated by one space.
#
######################################################################
# Now my turn
def reverse_wordssimple(message):
"""reverse the message but not 'in place'"""
# split the message by spaces
msg = message.split(" ")
return " ".join(msg[::-1]) # new trick I learned to reverse a list
# sigh this is a class gotcha question
# if you see the trick you get it, if you don't you never will
def reverse_words(message):
"""reverse the message 'in place' in a mutable byte array"""
b = bytearray(message)
# reverse the entire bytearray in place
reverse_bytes(b, 0, len(b))
# now walk through the byte array finding words and the reverse
# each word in place
start = 0
space = b.find(" ")
while space != -1:
reverse_bytes(b, start, space - start)
start = space + 1
space = b.find(" ", start)
else:
# reverse the unreversed remainder
reverse_bytes(b, start, len(b) - start)
return str(b)
def reverse_bytes(b, start, length):
"""reverses the bytes in array b, starting at start, for length bytes"""
for i in range(length / 2):
begin = start + i
finish = start + (length - 1) - i
(b[begin], b[finish]) = (b[finish], b[begin])
return None
# Now Test
class TestReverseWords(unittest.TestCase):
def test_reversewords(self):
"""test word reversal"""
for test in ["",
"a",
"abc def",
"abc def ",
"the quick brown fox",
'find you will pain only go you recordings security the into if']:
soln = reverse_wordssimple(test)
self.assertEqual(soln, reverse_words(test))
if __name__ == "__main__":
# # unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(TestReverseWords)
unittest.TextTestRunner(verbosity=2).run(suite)