14
1
Task
- The user inputs a sentence - words only. Any input other than letters or spaces, including integers and punctuation, should throw an exception: "Sentence must only use letters".
- The output has a pattern, where some words are reversed and others words are normal.
- The pattern starts as a normal word, the next two words are reversed, then the next two words are normal and the pattern continues.
- An example of where the words should be normal and where words reversed is below:
Normal - Reversed - Reversed - Normal - Normal - Reversed - Reversed - Normal ...
Input Example
She sells Sea shells on the Sea shore
Output Example
She slles aeS shells on eht aeS shore
Additional Rules
- If capital letters are used, they should remain on the letter they were originally posted on.
- Any multiple spaces initially posted on input should be reduced to one space. For example
Programming Puzzles and Code Golf
becomesProgramming selzzuP dna Code Golf
Shortest Code Wins!!
Happy coding...
All contests require an objetive winning criterion. Code golf (shortest code wins) should work well for this. You should also clarify what counts as word bondaries. Are words always composed of letters? Are they always separated by a single space? Also, the for example part is the only pattern that is used here, yes? – Dennis – 2015-10-30T22:05:07.580
Could you also include a test case that illustrates the rule from the fourth bullent point? – Dennis – 2015-10-30T22:10:36.330
"input including an integer should be rejected" What does that mean? Should the function throw an error? Output = Input? – Stewie Griffin – 2015-10-30T22:24:40.147
I have re-edited the description. Thanks for your feedback - anything else please just comment – Belfield – 2015-10-30T22:51:17.217
That may be a good idea... – Belfield – 2015-10-30T23:07:51.520
8Many programming languages do not have exceptions. – Doorknob – 2015-10-30T23:30:34.343
1@Doorknob: True, but sometimes challenges cannot be solved by all languages. Other examples are graphical output, file or web access. No need to worry. – nimi – 2015-10-30T23:46:42.280
15@nimi It seems entirely unnecessary to restrict a challenge to a small subset of languages for such a trivial reason. – Doorknob – 2015-10-30T23:56:34.157
Yep, this problem seems over-specified. – Yakk – 2015-10-31T01:20:09.510
1Should newlines count as a valid character? What about tabs? Should all whitespace be valid? – Downgoat – 2015-10-31T01:32:46.540
In the example about multiple spaces, there are no multiple spaces, just 1 newline. Could you please explaint better? – edc65 – 2015-10-31T01:32:49.500
10This otherwise-good challenge is ruined by the rule
Any input other than letters or spaces, including integers and punctuation, should throw an exception: "Sentence must only use letters".
It means implementing a secondary task totally unrelated to the challenge of reversing words, cuts out languages that don't have exceptions, and requires hardcoding or compressing a long string that takes up a lot of the byte count. – xnor – 2015-10-31T07:53:17.580@Vihan yes apologies, I have edited the description above so it is explained better. Just multiple spaces should be dealt with as one space. – Belfield – 2015-10-31T08:14:26.223
@xnor unfortunately posting a question is a bit of a conundrum... initially I posted the request as a very simple question of reversing and not reversing. People wanted more clarity to my question, which is good because it helps answers be more accurate - however the downfall being that it comes with constraints - I must have edited this question 30 odd times now! It is unfortunately a "damned if you do, damned if you don't". I realise now it is not ideal, but unfortunately that is the question and I will not be changing it. Thanks for your input. – Belfield – 2015-10-31T08:19:20.010
2
@Belfield Yes, I see your bind. I guess there's nothing you can do now. In the future, try posting in the Sandbox to get feedback before posting.
– xnor – 2015-10-31T08:29:18.7373@Belfield a good challenge is much more difficult than a good answer. I enjoyed this one anyway, and the next wil be better. – edc65 – 2015-10-31T10:19:22.750
@edc65 thanks for the appreciation - code golf has helped me with interview tests... so it's nice for me to give back to the community – Belfield – 2015-10-31T10:52:06.350