Jump to content

Reversing words (python3)

lewdicrous

1. I'm trying to reverse the first word in a sentence then move it to the end of the sentence without affecting the rest of the sentence (in this function)

For example, "The quick brown fox jumps over the lazy dog." becomes "quick brown fox jumps over the lazy dog. The"

2. For the rest of the sentence, I want to move the last letter of each word to the front.

For example, (including what happens on top) "The quick brown fox jumps over the lazy dog."  becomes "kquic nbrow xfo sjump rove eth ylaz gdo. ehT"

 

I know how to reverse the whole sentence using [::-1] However, I only want to reverse the first word

sentence = "The quick brown fox jumps over the lazy dog."
reverse = sentence[::-1]
print(reverse)

And I know how to move the last word to the front of the sentence using " ".join(sentence.split()[::-1]) However, I want to do this to the first word instead

sentence = "Quick brown fox jumps over the lazy dog."
moveWord = " ".join(sentence.split()[::-1])
print(moveWord)

How do I do this..

Link to comment
Share on other sites

Link to post
Share on other sites

The algorithm you're looking for is called "rotate", unfortunately Python doesn't have a built in function for it but it should be easy to implement after a quick google.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

So we split on space


words = sentence.split(' ')

Then we get the first word


word = words.pop(0)

Then reverse and add to the end of words


Words.append(word[::-1])

And finally join everything back 


result = ' '.join(words)

Print (result)

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

43 minutes ago, fizzlesticks said:

The algorithm you're looking for is called "rotate", unfortunately Python doesn't have a built in function for it but it should be easy to implement after a quick google.

I'm creating my own function, I'll look it up, thanks

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, lewdicrous said:

I'm creating my own function, I'll look it up, thanks

Just to clarify on @fizzlesticks answer:

What you want is "array rotate". You can solve this problem by breaking the string into a list, rotating the list by one, and then rotating each sublist (each string in the list) by one as well. 

Just as a fun test I tried to search for "rotate" and "string rotate". I found a bunch of stuff about multidimensional math and then rope/chord making. That's why I felt it necessary to make a distinction about what we are looking for here.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, straight_stewie said:

- snip -

Thanks!

@vorticalbox solved the first part, but I still can't figure out the second part

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×