5 kyu

Knights

22 of 62Djacon
Description
Loading description...
Algorithms
Logic
Puzzles
  • Please sign in or sign up to leave a comment.
  • RealKenshiro Avatar

    For once, the backstory adds something to an excellent kata.

  • bennyBoy_ Avatar

    Dear Djacon, this took me dayyyysss to complete! haha Great Kata, thank you! I remembered something I learned in a previous kata about how to improve performance and was happy to be able to apply it here.

  • DomikTS- Avatar

    From what I can see now - all sample tests passing, however there are tests with random data that fails. How can I fix this problem, can I get samples of those tests somehow?

    • DomikTS- Avatar

      Actually I just get 'got 7 expected 2' without description of the exact test idea, could you provide what this test is about for me to come up with solution for possible bug in algorithm?

    • B1ts Avatar

      You can print function arguments to console.

  • bori0066 Avatar

    aaahhhh after three days I finally come with a solution that pass the sample tests, but times out on the other tests. I give up :)

    • Djacon Avatar

      Dude, your way is too long. Maybe you should find another way to solve this problem without changing the original array? :)

      Hint: note that the ranks of adjacent knights are always different, and when Galahad comes, a same pattern happens

  • Bauer1 Avatar

    Hi! As far as i see, sometime random tests generate positions like:

    [770, 768, 767, 766, 766, 768, 770], 3, 766)

    As you can see, here is two pairs of knights with the same rank being the neighbors. So isnt it contradicts the condition?

    • Djacon Avatar

      Sorry for the long wait (I had about 1 a.m. at the time and had to put it off until tomorrow)

      Now fixed

  • Skyzzyy Avatar

    according to the logic u've put in the short explanation, when there were 3 knights of rank 8 and only one of them got ranked to 10, but in the 5th step there were also 3 knights with rank 10 which means that only one of them will be promoted to rank 12, so the remaining knights will only be 3!

  • Blind4Basics Avatar

    Hi,

    • The big number tests never generate situations with a lot of "battles". Most of them invlove travelling in the input array only up to 20% of it or so (top value I got so far is 30%, but happened one time only).
    • In a simlilar way, your tests never generate situations like 6 6 2 2 2 2 2 6 6 and so on (galahag being one of the 2, ofc).

    Cheers

    • Djacon Avatar

      Hi,

      1. I'm fixing this problem for now.
      2. The condition says that:There are knights gathered at the round table, and they prudently arranged so that the rows of their neighbors are different
        so there can be no knights with the same rank in this kata.
        I realized that there are no situations in kata where 1 or more knights remain, but I'm still working on that.
    • Blind4Basics Avatar

      2: oh ok. I missed that.

    • Djacon Avatar

      I added tests with cases where only 1 knight survives.

      Regarding the first problem, python & JS solutions can't handle arrays where more than 30-40% of all knights are involved in combat. Maybe I can put the number of battles higher than now, but in that case the required kyu to that kata will become higher than 5-6 (although I'm counting on that range, with your permission)

    • Blind4Basics Avatar

      what's the exact problem when increasing the "battle ranges"? execution time varying too much from one run to the other? input generation? on the solution side, those which aren't creating copies of the array shouldn't suffer that much.

    • Kacarott Avatar

      @Djacon for test generation, you might be able to generate "more interesting" tests, by working backwards. Example

      [7, 5, 9] # Final result
      [7, 4, 4, 9] # Randomly choose element to "split"
      [7, 2, 2, 2, 4, 9] # Another random split, but into 3 elements this time
      [7, 2, 1, 1, 2, 4, 9] # Final Split
      [7, 2, 1, 2, 4, 9] # Remove "Galahad" noting his position -> 2 and value -> 1
      

      Another benefit of this (if done correctly), is you already know the result, so you don't have to run a reference solution over it. (But you might want slightly more sophisticated logic than that, to also include other edge cases.)

      Anyway, just an idea :) (May not be necessary at all)

    • Djacon Avatar

      @Blind4Basics, the problem is that when you increase the "combat range", the pass time increases greatly.
      I've tested this on several tests, using various solutions from other participants. So I don't know how else to solve this issue.

    • Djacon Avatar

      @Kacarott, your solution is very interesting, but difficult to implement.
      In this kata, I already know the correct answer from the beginning and don't calculate it :)

    • Blind4Basics Avatar

      maybe, just decrease a bit the input sizes, so that you can enforce some "long travelling" in some tests. Or use that kind of input specifically in some medium size inputs and keep the big numbers tests as they are?

    • Kacarott Avatar

      Ah ok, my mistake. I saw ans = knights(arr, p-b, x) and thought it must have been a reference solution. In that case all good, but I'd suggest to import the user solution directly then. No point of import solution if you arent going to use solution.

    • Djacon Avatar

      Finally I managed to optimize the generation of arrays and now we can run tests with 300k knights :)

    • Blind4Basics Avatar
      Issue marked resolved by Blind4Basics 3 years ago
  • dfhwze Avatar

    My solution that always passes JS sometimes fails Python. I'm positive it's because of some edge case you check in Python and not in JS. Could you verify both languages for edge case generation?

    • Djacon Avatar

      I examined your code and saw that it returns the wrong value in some cases.

      For example:

      knights([914, 912, 911, 912, 914, 916, 930, 0, 17, 71, 30, 28, 81, 48, 107, 69, 109, 87, 98, 47, 931], 2, 911) # should return 16, but your func return 15!
      
    • dfhwze Avatar

      Thanks for providing an example. Somehow, this scenario never happens at JS, but sometimes at Python. That's why I think you are generating differently in JS vs Python.

    • dfhwze Avatar

      I think I found it, you have 0 as possible element entry in Python, and not in JS. Or maybe I just never got an example with a 0 in JS. Anyway, this is the edge case for me.

    • dfhwze Avatar

      answered

      Question marked resolved by dfhwze 3 years ago
    • Djacon Avatar

      Amazingly, I finally found the cause of the error :)
      Everything should work now, thanks for your help.