21
Have you ever wanted to ask the compiler "Why?" Most of us have been frustrated when the code isn't working as it should. Mathworks has therefore implemented a nice little function, why
, that answers the question. To give a few examples from MATLAB:
why
The programmer suggested it.
why
To fool the tall good and smart system manager.
why(2)
You insisted on it.
why(46)
Bill insisted on it.
Your task is to implement the why
function in your language. The function should work with and without an input argument (alternatively use input 0
or -1
). The function must be named why
(or, writing why(n)
in STDIN should result in the appropriate string being printed).
If no argument is given, or the argument is zero or negative, the output string should be a random, valid phrase. So, there should be a function why
, why()
, why(0)
or why(-1)
that returns a random sentence.
If an input argument, n
is given (function argument, not STDIN), the output should be the n'th string (defined below). So, why(1)
should always output (print/display) the same result.
The sentences are built up as follows (Type 1, Type 2 and Special). All sentences end with !
.
"Person" "ending" !
"Verb" "adjective" "Person" !
A list of special cases
The list of persons:
Stewie
Peter
Homer
The programmer
The system manager
You
The list of endings:
suggested it
insisted on it
did it
The list of verbs are:
To fool
To satisfy
To please
The list of adjectives:
the smart
the bald
the tall
the rich
the stupid
The list of special cases:
How should I know?
Stop asking!
Don't ask!
The way to select a numbered one is:
Type of sentences:
Odd number => Type 1
Even number => Type 2
n % 7 = 0 => Type 3 (% is the modulus operator)
Names: The nth name is defined using modulus (%).
n = 1: 1 % 7 => Stewie
n = 2: 2 % 7 => Peter
...
n = 6: 6 % 7 => You
n = 7: 7 % 7 => How should I know?
n = 11: 11 % 7 => The programmer
n = 14: 14 % 7 => Stop asking!
n = 21: 21 % 7 => Don't ask!
Endings: The nth ending is also defined using the modulus. Assume the endings (1, 2 and 3) are listed like (1 2 2 3)
. As the numbers are always odd, use ((n+1)/2 % 4)
n = 1: ((1+1)/2 % 4) => suggested it
n = 3: ((3+1)/2 % 4) => insisted on it
n = 13: ((13+1)/2 % 4) => did it
Adjectives: The nth adjective is defined using the modulus. As the numbers are always even, use: (n % 10)/2
n = 2: (2 % 10)/2 => Smart
n = 6: (6 % 10)/2 => The tall
...
Verbs: The nth verb is also defined using the modulus. Assume the verbs (1, 2 and 3) are listed like (1 2 2 3)
As the numbers are always even for verbs, use (n % 8) / 2
n = 2: (2 % 8)/2 => To fool
n = 4: (4 % 8)/2 => To satisfy
n = 6: (6 % 8)/2 => To satisfy
n = 8: (8 % 8)/2 => To please
Now, the way to create a random one should be fairly simple, simply select a random n
.
Some examples:
why
You suggested it!
why
To fool the tall Homer!
why
Don't ask!
why(1)
Stewie suggested it!
why(14)
Stop asking!
why(8)
To please the rich Stewie!
Standard code golf rules apply. A winner will be selected one week from the day the challenge was posted.
writing why in the stdin should return the string I might be able to work with that. Does it have to be lowercase
why
or wouldWHY
be acceptable? – Dennis – 2015-10-07T16:00:44.877Lower and upper case are both fine. – Stewie Griffin – 2015-10-07T16:28:59.070
Also the Endings should not need
/2
to work. That gives fractional values.13
should also beinsisted
(14/2=7 %4 = 3 = 2nd of insisted). – Jonathan Leech-Pepin – 2015-10-07T16:44:32.1973Does it matter if you end up with cases like
the rich The programmer
due to the specifiedthe
? – Jonathan Leech-Pepin – 2015-10-07T17:03:56.6201@StewieGriffin The words
The
andTo
in your lists should probably be lowercase to match your examples... – mbomb007 – 2015-10-07T17:06:42.383@Jonathan: i'm on my cell and won't have my laptop available the coming hours. You're most welcome to fix the aparently wrong examples in the question text. Thanks for noting it! – Stewie Griffin – 2015-10-07T17:57:22.930
Still weird ... endings should be
1:suggested 3,5:insisted 7:(did)special 9:suggested 11,13:insisted 15:did
– edc65 – 2015-10-08T08:28:20.907@edc65 Crap, forgot my own
(1 2 2 3)
order. Again, away from my laptop, so i can't fix it. (I have blocked codegolf on my work computer. I ended up having to work on a saturday because i used some of my working hours trying to solve / golf a challenge on a friday). – Stewie Griffin – 2015-10-08T08:36:22.427:) I should do the same – edc65 – 2015-10-08T09:11:05.040
Could we get
why(4)
added to the examples? I think that will clarify a lot of the above questions. – DLosc – 2015-10-14T00:51:21.623@DLosc, because I screwed up,
To satisfy the bald The programmer
is technically a valid sentence forwhy(4)
. No bonus (too late for that), but a pat on the back and happy times if you manage to do as edc65, and remove thethe
where it doesn't fit. – Stewie Griffin – 2015-10-14T06:35:49.027