35
2
Of course, the SE network is very knowledgeable about how to be respectful in the restroom, but for those of you who need a recap, being respectful means flushing the toilet, etc. Most importantly, though, it means using the stall as far away from others as possible.
The challenge
Given a blueprint of a set of stalls with indications of which ones are in use as a string, you must return or print from a function or program where the most respectful place to do your business is.
The input
0 1 2 3 4 5 <- The stall number which is not actually visible in the input.
| | |-| |-|-| <- the stalls
The stalls are numbered in ascending order from left to right. There will always be at least one empty stall. There can be up to 50 stalls in an input. You can also take the input as an array or string of 0
s and 1
s or booleans if you prefer to do so.
Stalls in use have -
in them (in between the pipes).
The output
The most respectful stall to go to is the one that is on average farthest away from the ones in use. The distance between two stalls is the absolute value of the difference of the numbers above them.
Just to be clear: you are finding the average distance from all of the stalls—not just the neighboring ones.
You must output the lowest number of the most respectful stall to go to that is empty.
Examples
Input:
|-| |-| OR 101
Output:
1
Input:
| | |-| |-|-| OR 001011
Output:
0
Input:
|-| |-| | | | |-|-| OR 101000011
Output:
1
Input:
|-| | | | | |-|-| | | | | OR 100000110000
Output:
11
Input:
|-|-|-|-| | | | | | |-| OR 11110000001
Output:
9
Input:
|-| | OR 10
Output:
1
Input:
|-| | |-| OR 1001
Output:
1
This is code-golf, so shortest code in bytes wins!
You can use 0 or 1 based indexing in your answer — whichever you prefer; if you use 1 based indexing, then you must say so explicitly in your answer.
Related, but not a dupe – Daniel – 2016-08-09T15:59:48.907
35"Of course, the SE network is very knowledgeable about how to be respectful in the restroom" [citation needed] – Alex A. – 2016-08-09T16:33:52.823
Are ties broken to the left or to the right? Specifically, does
1001
return1
or2
? – Leaky Nun – 2016-08-09T16:45:44.6237
@AlexA.: Have a look at the toilet questions and answers on travel.stackexchange to assess the level of education of the SE network (or to educate yourself).
– Jonas – 2016-08-09T17:05:25.550Can the numbers in the input by separated with spaces? (
0 1 0 1 0 0
for instance) – Dada – 2016-08-09T17:15:32.55730But everyone knows that the respectfulness criterion is to maximize the minimun distance, not the average :-) – Luis Mendo – 2016-08-09T17:26:46.103
@Dada, I suppose so – Daniel – 2016-08-09T17:33:45.510
I'm getting these results. Am I misunderstanding something?
– Dennis – 2016-08-09T17:42:21.607@Dennis Are you maximizing the average distance? My first hunch was to maximize the minimum – Luis Mendo – 2016-08-09T17:46:08.907
1@LuisMendo Yeah, that was indeed the problem. Thanks! – Dennis – 2016-08-09T17:50:34.330
2@Dopapp You should add
[1,0,0,1]
as a test case. None of the current test cases verifies if ties are broken correctly. – Dennis – 2016-08-09T17:52:53.5938Why does
101000011
return 1 (instead of 4 or 5)? – Amani Kilumanga – 2016-08-10T04:02:23.013@AmaniKilumanga, doing it on paper, 1 is correct. Are you calculating the maximum average distance or the maximum distance period? – Daniel – 2016-08-10T13:16:05.293
1Just a short note while browsing over random stackexchange questions, the average distance is indeed a strange criterion. The solution will always be either the first or the last free spot. (If there are more used stalls on my right than on the left, then moving left will always increase the distance and vice versa, with the minimum distance being somewhere in the middle) I'm no real code golfer, but maybe someone could use this for a shorter solution. – mlk – 2016-08-10T15:06:36.383
@Dopapp The expected output is the lowest stall number that is most respectful. In the case of
101000011
, outputting1
would be the LEAST respectful stall, with an average distance of 1, whereas4
and5
both have an average distance of 2.5, and the lower of the two is4
, so4
is the expected output. – ewok – 2016-08-10T16:27:10.8702@ewok, no. It is the average distance from all of the stalls--not just the neighboring ones. – Daniel – 2016-08-10T17:55:15.150
@Dopapp Ok then. That's not really clear from the question. I'd recommend clarifying it. – ewok – 2016-08-10T17:57:18.570
2@ewok, I have edited the question to be clearer – Daniel – 2016-08-10T17:59:29.267
2Being respectful in restrooms nowadays bring in complicated issues of gender identity. – Kaz – 2016-08-10T18:56:07.503