Created
November 11, 2013 03:29
-
-
Save prat0318/7407349 to your computer and use it in GitHub Desktop.
reverse words in sentence
This file contains 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
def reverse(string, start, end): | |
rev = "" | |
for i in range(0, end - start +1): | |
rev += string[end - i] | |
return rev | |
def reverse_words(sent): | |
rev_sent = reverse(sent, 0, len(sent)-1) | |
rev_words = ""; i = 0; j = i | |
while(True): | |
while((j < len(rev_sent)) and (rev_sent[j] != ' ')): j+=1 | |
rev_words += reverse(rev_sent, i, j-1) | |
if(j >= len(rev_sent)): break | |
rev_words += " " | |
j += 1; i = j | |
return rev_words | |
#Error: Never ever use & instead of and - & is something different | |
#Error: range and how does it operate (10,14) will give 10,11,12,13 | |
#Error: better is reversed(range()) or range(start, end, -1) | |
print reverse_words("here goes out first hello world reversed!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment