Skip to content

Instantly share code, notes, and snippets.

@gengmao
Created March 14, 2011 08:04
Show Gist options
  • Save gengmao/868885 to your computer and use it in GitHub Desktop.
Save gengmao/868885 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import string
def reverse(sentence):
stack = []
word = ''
for char in sentence:
if char in string.punctuation or char == ' ':
if word is not None:
stack.append(word)
word = ''
stack.append(char)
else:
word += char
output = ''
while len(stack) > 0:
output += stack.pop()
return output
if __name__ == "__main__":
print reverse("One, Two, Three, Four.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment