Hi guys, I just maked this puzzle and after submitting my answer I get only 90%… I managed to get the 100% but I am not sure if I understand why so I will try to explain my problem without spoiling the code. My code passed the initial 3 test (with the one named “any order”) but failed on the “disordered” test from the submitting tests.
After reading on the forum I manage to get the 100% with a maybe even slower code so I was surprised, I needed to use a list to stock all the strengths and return the one with Index[1] (to skip the 0 who comes from my first difference calculus (x - x == 0).
After a lot of “brain bugging” in front of my computer, I funded only one explanation, the “submit test” who failed need to return 0 and that would explain why my first code failed (it ignore 0).
Can somebody confirm to me that we can have 2 horses with the same strength and that the expected output can therefore be* (and is for one of the final tests) 0 ?
I know it can be 0 (Puzzle description: “D is an integer greater than or equal to 0.”) but I am curious if i get this right and if this is the reason why my first code didn’t worked…
Resolved with Python 3.
The hint is that the minor diff are with the ordered pi of horses. Its resolved with an sorting and one loop, thats a 0(n log n) algorithm complexity (if you use the sorted of Python of course).
I am using c++ with list. Here is the part of my code which I don’t understand how I can optimize it more. Any suggestions would be fantastic. I would like to state I tried with an array as well but it still times out.
horses.sort();
for(int i = 0; i < horses.size()-1; i++)
{
auto left = next(horses.begin(),i);
auto right = next(horses.begin(),i+1);
if(*right - *left < difference )
difference = *right - *left;
}
HI. I dont know why but the input which is given are not all the numbers in python.
n = int(input())
for i in range(n):
pi=int(input())
print(i)
print(pi)
print("+++++++")
is giving me the output:
Standard Output Stream:
0
5
+++++++
1
8
+++++++
2
9
+++++++
but the first number should be a 3 for the simple case.
The same on the horses in any order test. The first number ist not in my input. What am i doing wrong?
Hi, this code always seems to produce extra output - I’ve put it into an external Python and only see my expected output. What do other people see? If you test the 1st and 2nd test cases, you get an extra number for every time input() is called…
The ‘>> <<’ are only to clearly dilineate the result output, you can remove them if you want
size = int(input())
min_dist = 10000000
horses = {k: int(v) for k, v in enumerate(map(input, range(size)))}
target = 0
for k,v in horses.items():
this_horse = k
this_horse_pwr = v
if k+1 in horses:
next_horse = k+1
next_horse_pwr = horses[k+1]
pwr = abs(next_horse_pwr-this_horse_pwr)
if pwr < min_dist:
target = k
min_dist = pwr
print(f">>{target}<<")
It looks like when you map input to range(size), the result is input(0), input(1) and input(2) (for the first test case). That is, 0, 1 and 2 become the prompts for receiving inputs, so they appear in the console.
I think this is sufficient for you: horses = {k: int(input()) for k in range(size)}
Let’s see another example first: print(list(map(str, range(3))))
The result is [‘0’, ‘1’, ‘2’], because what the above code produces is essentially [str(0), str(1), str(2)].
So in your case: map(input, range(size))
The result is input(0), input(1), input(2) if size == 3. Hence, 0, 1 and 2 become the parameters of the input function. For this function, the parameter is actually a prompt, which gets printed out before the code tries to get the input from the user or a text file.
The fact that input(something) is able to print something out is actually taken advantage by some players here when they do code golf in python so they do not have to write another print statement.
Unfortunately if I try to silence the output from the input() function, the process here crashes, saying “lost connection to stdout”. So, that’s a specific thing to here and still leaves me with this problem.
I’ve searched for some alternative method, but there doesn’t appear to be anything here to help resolve it - other than avoiding using map(input at all on here.