Created
March 14, 2011 08:04
-
-
Save gengmao/868885 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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