3
2
The handshake problem is the classic problem that for n people in a room, if they all shake hands, what's the total number of handshakes that occur.
You code should take an input of any number and output the number of handshakes, both in base ten.
Also the code should not let non-integers through. You should output a falsey in this case. The only expected inputs are positive whole integers and positive real numbers - you don't need to worry about complex numbers or strings as inputs.
This should mean that you should expect n >= 0
Test cases
(N:handshakes)
0:0
10:45
20:190
10.5:0
1337:893116
Normal golfing rules apply and shortest code wins. If your code isn't self explanatory, please explain.
Jelly and 05AB1E are the winners, both at 2 bytes
4What exactly can we expect in terms of "non-integer" inputs? (having said that, I feel like the error checking might amount to more than the actual challenge) – Sp3000 – 2016-06-14T14:08:30.703
Related. – Martin Ender – 2016-06-14T14:10:18.063
@Sp3000 I have edited the question now. You'll only expect real and whole numbers – george – 2016-06-14T14:10:30.797
1What about negative numbers? – Leaky Nun – 2016-06-14T14:11:41.993
@MartinEnder indeed it is, but the differ eve is that the handshake problem is slightly more real world – george – 2016-06-14T14:12:27.290
Might ask for some specifics if that's okay - which of the following might we expect as input?:
-0
,-1
,10.
,10.0
,.1
,-.1
,-10.
,-10.0
– Sp3000 – 2016-06-14T14:13:04.1637
Please have a look at our list of things to avoid when writing challenges. Bonuses are one of them. I would suggest removing it since it doesn't really add anything to the challenge. Oh, and also check out our sandbox. That's the place to get rid of some quirks in your challenge before you post it.
– Denker – 2016-06-14T14:14:17.177@Sp3000 expect all the positive values – george – 2016-06-14T14:16:22.323
2So... does that mean
-0
,10.
,10.0
and.1
? (I'm assuming you mean nonnegative - sorry for being a pendant, but stuff like this does tend to matter for esoteric languages which don't have an easy number eval function :P) – Sp3000 – 2016-06-14T14:18:59.070If normal golfing rules apply, is there any reason you're excluding full programs?
– AdmBorkBork – 2016-06-14T14:21:14.620@Sp3000 no thank you for asking. This is my first question so it's good to know how accurate I need to be in question defining. I wouldn't count -0 as some language would need to evaluate this first. So all numbers greater than 0. – george – 2016-06-14T14:22:36.797
Is erroring on invalid inputs allowed? – Leaky Nun – 2016-06-14T14:26:35.093
@TimmyD sorry didn't actually realise that was a set rule – george – 2016-06-14T14:28:53.053
@george Er... that's fine, welcome! Bit confused now though, since you said "greater than 0" in the comments but wrote "expect n>=0" – Sp3000 – 2016-06-14T14:29:38.347
@LeakyNun if you mean that it will give an error when an invalid n in entered no. – george – 2016-06-14T14:29:46.583
@Sp3000 whoops, yes greater than or equal to is correct. As in the test case 0 – george – 2016-06-14T14:30:54.267
7I'm having a think about this challenge and was wondering - why does the challenge allow floats as input, out of curiosity? I'm not sure the error checking adds much to the challenge (and it also makes the challenge a lot harder and less nice for some languages) – Sp3000 – 2016-06-14T14:40:45.310
Also, Welcome to Programming Puzzles and Code Golf! I recommend posting future challenges to the Sandbox where they can get meaningful feedback before being posted to the main site. Enjoy your stay!
– AdmBorkBork – 2016-06-14T14:41:33.240@TimmyD thanks! I will do in future – george – 2016-06-14T14:42:08.990
@Sp3000 when I coded the question myself, it was just n*(n-1)/2 which could take any value, but in the context of the question, there will only be a whole positive number of people in the room, so although it is computable for a float number, it is not contextual – george – 2016-06-14T14:44:15.637
1"I've searched for this question and couldn't find it" is often a strong hint that the question is really boring. – Peter Taylor – 2016-06-14T15:49:11.693
1@PeterTaylor so the game is over? No more new questions except for boring ones? – edc65 – 2016-06-14T16:16:15.680
1@edc65, the subtext that you read should not be "Don't search to see whether your question is a dupe" but "If you think it necessary to justify posting the question, think twice about posting it. Then if you're still planning to post it, think a third time." – Peter Taylor – 2016-06-14T16:22:26.383
1@PeterTaylor Aren't you suppose to check that a question has not been asked before? If one does not and it is a dupe, it was a waste of a post. Correct me if I'm wrong but that's is how I thought is worked here? – george – 2016-06-14T16:27:47.417
4@george Yes, you should search for dupes. But if you have to start by explaining that you did, it's a good indicator that it's either 1) very trivial, or 2) so close to being a dupe you needed to justify it. Neither are promising. – Geobits – 2016-06-14T16:33:53.740
@Geobits okay that's interesting. I'll keep it in mind – george – 2016-06-14T16:37:41.580
@DrGreenEggsandIronMan sorry I should have removed that after TimmyD's comment. It can be a full program, or just a function, unnamed or names. In PPCG any style of answer is accepted – george – 2016-06-14T20:28:15.570
This would be massively better if invalid input did not need to be considered. Arbitrarily overriding the defaults is a bad idea.
– Mego – 2016-06-16T00:16:47.040@Mego see my previous reply about the context of the question – george – 2016-06-16T00:18:24.607