- Morning the in bed to goes chrissie
- Matches? True
- Palindrome? False
- Palindrome of words? False
- Red it's if except, green always plant a is why
- Matches? True
- Palindrome? False
- Palindrome of words? False
- Plant a plant
- Matches? True
- Palindrome? False
- Palindrome of words? True
Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Forum Search
Forum Statistics
UsersTotal Post History
- Posts:
- 78569
- Topics:
- 17963
7-Day Post History
- New Posts:
- 67
- New Topics:
- 35
- Active Topics:
- 37
Our newest member
Other
-
FAQ
All times are UTC [ DST ]
Google Ads
Puzzle 15: reverse the order of words
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
22 posts • Page 1 of 1
Please wait...
Puzzle 15: reverse the order of words
Given an array of characters that form a sentence, give an efficient algorithm to reverse the order of the words (not characters) in it.
Testcases
"Chrissie goes to bed in the morning" = "Morning the in bed to goes chrissie"
"Why is a plant always green, except if it's red" = "Red it's if except, green always plant a is why"
"Plant a plant" = "Plant a plant"
Look at punctuation and capitalization. Extra points if you can determine if it's a palindrome but only for this case. A more difficult palindrome puzzle will follow in the next few weeks.
If you want, you can time it, which is usually a good indication of how good your algorithm is.
Taken and improved from http://halcyon.usc.edu/~kiran/msqs.html#programming.
Testcases
"Chrissie goes to bed in the morning" = "Morning the in bed to goes chrissie"
"Why is a plant always green, except if it's red" = "Red it's if except, green always plant a is why"
"Plant a plant" = "Plant a plant"
Look at punctuation and capitalization. Extra points if you can determine if it's a palindrome but only for this case. A more difficult palindrome puzzle will follow in the next few weeks.
If you want, you can time it, which is usually a good indication of how good your algorithm is.
Taken and improved from http://halcyon.usc.edu/~kiran/msqs.html#programming.
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9107
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: Puzzle 15: reverse the order of words
x
-

Remou - LTD Admin

-










- Posts: 5115
- Joined: Sun Oct 14, 2007 11:26 am
Re: Puzzle 15: reverse the order of words
Python and regex goodness. Can't handle parans or brackets, maybe I will add those later when I time it 
Code is hidden, SHOW
Um, yeah...I'm going to need you to come in on Saturday -- Bill Lumbergh, Office Space
-

tarwn - LTD Admin

-









- Posts: 3352
- Joined: Fri Oct 12, 2007 11:10 am
- Location: Raleigh, NC, USA
Re: Puzzle 15: reverse the order of words
I don't know if this is answer but it works
In vb.net
the result was
In vb.net
Code is hidden, SHOW
the result was
Code is hidden, SHOW
Zameer Abdulla
-

ZmrAbdulla - LTD Senior Moderator

-

- Posts: 225
- Joined: Thu Oct 18, 2007 7:43 am
- Location: United Arab Emirates
Re: Puzzle 15: reverse the order of words
Zameer the result you got are not exactly the same as the testcases. You forgot punctuation and Capitals.
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9107
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: Puzzle 15: reverse the order of words
Capitalization is possible. But I don't have any idea how to deal with punctuation.
Zameer Abdulla
-

ZmrAbdulla - LTD Senior Moderator

-

- Posts: 225
- Joined: Thu Oct 18, 2007 7:43 am
- Location: United Arab Emirates
Re: Puzzle 15: reverse the order of words
Slightly newer version that has a function for a list of characters and a second function for raw strings. Both now return the reversed sentence and an indication of whether the sentence is a word palindrome:
And here is the test code for both functions:
And the results:
Code is hidden, SHOW
And here is the test code for both functions:
Code is hidden, SHOW
And the results:
Code is hidden, SHOW
Um, yeah...I'm going to need you to come in on Saturday -- Bill Lumbergh, Office Space
-

tarwn - LTD Admin

-









- Posts: 3352
- Joined: Fri Oct 12, 2007 11:10 am
- Location: Raleigh, NC, USA
Re: Puzzle 15: reverse the order of words
Thanks for reminding me to show a result (by including a result, that is).
x
-

Remou - LTD Admin

-










- Posts: 5115
- Joined: Sun Oct 14, 2007 11:26 am
Re: Puzzle 15: reverse the order of words
-

ca8msm - LTD Admin

-









- Posts: 2492
- Joined: Wed Oct 10, 2007 6:49 pm
- Location: North East England
Re: Puzzle 15: reverse the order of words
Close but not close enough. I know what the result will be but could you post them anyway?
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9107
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: Puzzle 15: reverse the order of words
As yes, I see what you mean now (the comma's position).
-

ca8msm - LTD Admin

-









- Posts: 2492
- Joined: Wed Oct 10, 2007 6:49 pm
- Location: North East England
Re: Puzzle 15: reverse the order of words
-

ca8msm - LTD Admin

-









- Posts: 2492
- Joined: Wed Oct 10, 2007 6:49 pm
- Location: North East England
Re: Puzzle 15: reverse the order of words
Sorry Mark,
I can't agree with this.. I am sure you can do better.
1) What if the string didn't start with Capital letter?
2) What if there is any other punctuation marks in the string?
3) What if any other word started in capital letter?
I can't agree with this.. I am sure you can do better.
1) What if the string didn't start with Capital letter?
2) What if there is any other punctuation marks in the string?
3) What if any other word started in capital letter?
Zameer Abdulla
-

ZmrAbdulla - LTD Senior Moderator

-

- Posts: 225
- Joined: Thu Oct 18, 2007 7:43 am
- Location: United Arab Emirates
Re: Puzzle 15: reverse the order of words
The code should work for the testcases given. Weather or not it is good code is up to you. Mark's code works for the given testcases so it isa perfectly valid solution.
pink fuzzy slippers
-

chrissie1 - Senior Guru

-











- Posts: 9107
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: Puzzle 15: reverse the order of words
Then it is my mistake I didn't post a valid solution more similar to this earlier.
I was thinking need to check any string.
I was thinking need to check any string.
Zameer Abdulla
-

ZmrAbdulla - LTD Senior Moderator

-

- Posts: 225
- Joined: Thu Oct 18, 2007 7:43 am
- Location: United Arab Emirates
Re: Puzzle 15: reverse the order of words
ZmrAbdulla wrote:Sorry Mark,
I can't agree with this.. I am sure you can do better.
1) What if the string didn't start with Capital letter?
2) What if there is any other punctuation marks in the string?
3) What if any other word started in capital letter?
1) I didn't think that mattered. All of the test case results have a starting capital letter as though they were part of a sentence so I assumed all results should have the first letter capitalised.
2) I just used the given test cases, I would have probably used a regular expression to deal with other punctuation characters had there been others.
3) Again I just followed the test cases. If there was a capital in any other word, I simply wouldn't have used the ToLower method in the first declarations.
These puzzles are just about seeing what different methods can be used to achieve the same result. Even if someone posts a solution that doesn't generate the correct results, I find them just as interesting to see what their thought process was when tackling the problem.
-

ca8msm - LTD Admin

-









- Posts: 2492
- Joined: Wed Oct 10, 2007 6:49 pm
- Location: North East England
Re: Puzzle 15: reverse the order of words
Here is my solution, in java as usual.
Features:
- custom string tokenizer that doesn't use regular expressions
- Handles parenthesis, check the sample output
- Handles cases for not only the first word, but any word in the middle
- checks for palindromes
And the output:
Features:
- custom string tokenizer that doesn't use regular expressions
- Handles parenthesis, check the sample output
- Handles cases for not only the first word, but any word in the middle
- checks for palindromes
Code is hidden, SHOW
And the output:
Code is hidden, SHOW
- shamsm
- Apprentice

-
- Posts: 12
- Joined: Wed Jul 30, 2008 10:09 am
Re: Puzzle 15: reverse the order of words
shamsm,
why is "Except" capitalized in your output?
why is "Except" capitalized in your output?
God cries a little bit every time someone builds a database.
-

Emtucifor - Guru

-










- Posts: 2832
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: Puzzle 15: reverse the order of words
Emtucifor wrote:why is "Except" capitalized in your output?
Since the corresponding fourth word in the actual input (Plant) is also capitalized
- shamsm
- Apprentice

-
- Posts: 12
- Joined: Wed Jul 30, 2008 10:09 am
Re: Puzzle 15: reverse the order of words
Seems an unusual rule, to me, since the only capitalization rule based on position that I'm aware of is the first word. All other words should remain capitalized in the output if they are capitalized in the input. There are problems with recognizing doubly-capitalized words like a proper noun as the first word in the sentence, but otherwise the rules seem clear.
He gave John a piece of his mind.
--> Mind his of piece a John gave he.
not --> Mind his Of piece a john gave he.
He gave John a piece of his mind.
--> Mind his of piece a John gave he.
not --> Mind his Of piece a john gave he.
God cries a little bit every time someone builds a database.
-

Emtucifor - Guru

-










- Posts: 2832
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: Puzzle 15: reverse the order of words
ya it is, just wanted to point it out as a feature I have implemented
In this whole rule of reversing words won't help remembering most of the grammar rules
In this whole rule of reversing words won't help remembering most of the grammar rules

- shamsm
- Apprentice

-
- Posts: 12
- Joined: Wed Jul 30, 2008 10:09 am
Re: Puzzle 15: reverse the order of words
you get my vote, shamsm, for providing extra features free of charge 

a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-









- Posts: 3117
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
22 posts • Page 1 of 1


LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.