6 kyu
N smallest elements in original order
933 of 2,990GiacomoSorbi
Loading description...
Arrays
Lists
Data Structures
Algorithms
View
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Spoiler
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
-
-
Your rendered github-flavored markdown will appear here.
-
Label this discussion...
-
No Label
Keep the comment unlabeled if none of the below applies.
-
Issue
Use the issue label when reporting problems with the kata.
Be sure to explain the problem clearly and include the steps to reproduce. -
Suggestion
Use the suggestion label if you have feedback on how this kata can be improved.
-
Question
Use the question label if you have questions and/or need help solving the kata.
Don't forget to mention the language you're using, and mark as having spoiler if you include your solution.
-
No Label
- Cancel
Commenting is not allowed on this discussion
You cannot view this solution
There is no solution to show
Please sign in or sign up to leave a comment.
It's definitely a nice one. But not so difficul to solve.
While trying to solve: "Wow, this is a lot harder than I thought" After solving, looking at the top-solution: "Oh, yeah, alright..."
Very nice kata! A bit more difficult than your average 6 kyu.
Nice one, a little bit harder than it seems at first sight!
arr = [3, 3, -8, 7, 5, -1, 0, 0, 6, 6, -6, -8, -2, 3, 1, -8, -8, 7, 8, 7, 2, 2, 3, 10, -10, -1, 7, -9, -8, -1]
n = 27
It should work for random inputs too:
[3, 3, -8, 7, 5, -1, 0, 0, 6, 6, -6, -8, -2, 3, 1, -8, -8, 7, 7, 2, 2, 3, -10, -1, 7, -9, -8]
should equal:
[3, 3, -8, 7, 5, -1, 0, 0, 6, 6, -6, -8, -2, 3, 1, -8, -8, 7, 7, 2, 2, 3, -10, -1, -9, -8, -1]
So, the question is why the number 7 should not be included in the list of results at index -3(24), if 7 is among the 27 smallest elements of this array?
I am not sure, but seems the test is broken or etc.
What language?
This test is not broken. In your example, your answer does not include one of
-1
. Why your solution rejects-1
if it is among the 27 smallest elements of this array?Your solution is buggy, and tests seem to be OK, at least the exampel you show.
very nice kata!
I liked this kata, but sort algo on node.js turns stable only after node.js 11 which makes it bit of a headache if you don't know. Could this use node.js > 11?
done
This comment has been hidden.
Ha! I was scratching my head because of this too. It looks like it's because the kata still use Node 8 and the sort method is still not stable/weird?. You can still pass the kata using your current solution, you just need to do some 'tweaking' for the
sort
.I'm closing the issue, because issue about node version already raised here
python new test framework is required. updated in this fork
Approved
This is a lot harder than 6 kyu, hence should be higher than 6 kyu. But that's just my opinion.
Testing for [-7, 9, -8, -2, 2, 2, -4, -2, 1, -4, 7, 0, -8, -3, 9, -1, 10, -2, 6, 10, -5, 10, 0, 4, -2, 10, -2, -4, -7, 2, 4, -5] and 11 Expected: equal to [ -7, -8, -2, -4, -4, -8, -3, -5, -4, -7, -5 ]
Why is here on "expected" field I see "-7, -8, -2, -4, -4 ..."? If I'm not mistaken, the answer should be "-7, -8, -2, -4, -2, -4 ..." as on the top we see that in a consecutive number -2 going before the second -4
This isn't a kata suggestion, it's a question. If you order the numbers in increasing order, and you get the first 11, there is only a single 2.
JS: Node v12 should be used along with its proper assertion tools (Mocha + Chai).
done
Julia translation
Approved
very nice kata!
Ruby 3.0 should be enabled.
done
In Rust the function name in the initial code (n_smallest) differs from the one tests expect (first_n_smallest).
fixed !
Typo:
bas
should bebase
(first paragraph of description).replaced with
basic
(not sure what base would mean here: a lame base sorting method...)Hello, when I run test on site I'm getting
but when I run it on my text editor I'm always getting correct solution
[0, -2, -9, -5, -4, -3, -5, -4, -9, -8]
not this one[-2, -9, -5, 0, -4, -3, -5, -4, -9, -8]
I have the same question:
can anyone help?
Python tests are broken when it starts applying random inputs. The feedback given is not useful. Nevertheless ran the random inputs on my own and got the right answers. Something is wrong with the test cases.
I tried it just now and passed, I additionally intentionally gave wrong answer and feedback seemed good to me, telling me everything I would want to know. So what is wrong?
test input = [7, -3, -7, -3, -5, 4, 0, 9, -6, -10, 5, 6, -2, -5, 4, -5, -5, 3, -5, 8] and 14 My output (apparently incorrect) = [-3, -7, -3, -5, 4, 0, -6, -10, -2, -5, 4, -5, -5, 3] Test expected output = [-3, -7, -3, -5, 4, 0, -6, -10, -2, -5, -5, -5, 3, -5]
Look towards end of expected output where there are three -5s in a row (at index item 9). My output has a 4 inserted after "first" instance of -5 (at index -4). Given that the number 4 is one of the smallest elements and has already shown up in both solutions, and comes up first in the test input - what am I missing?
Taking a look at a problem and sharing what I see seems like a weird thing to get downvoted for >_< what do you want me to say, that you're right when you're not? xD you're wrong and that's okay. I did not know that you are wrong since you had not shown your output yet. But I solved the problem and ran the tests without running into the situation you described, so I reported that to you. But yes, your output is wrong, I have checked it carefully manually. Try it yourself with pen and paper and/or compare to the expected output.
Why doesn't the 4 belong in the ouput towards the end? It shows up in the original input before what the solution shows and is one of lowest numbers. The instructions say to keep the numbers in the original order they appear in. My output does that. I just want to understand what I'm doing wrong here. Obviously my output is working for vast majority of test cases.
Check the sum of your output and the expected output. You're looking for the lowest values, so a better answer would have a lower sum. You can also check that the expected result is following all the rules (correct order, all elements exist in input)
Fun kata, trickier than first expected.
Random tests for Python are broken: Testing for [1, -8, 8, 1, 7, 6, 7, 2, 2, 2, -8, -3, -8, -2, -6, -5, -9, 2, -8, 4, 1, -8, -9, 3, -5, -4, -8, -6, -3, 9, -1, -4] and 4 It should work for random inputs too: [-8, -8, -8, -9] should equal [-8, -8, -9, -9]
There're 2
-9
in the input. Your solution is wrong.My mistake, sorry, bro. Great kata...
I've added a Rust translation, please take a look!
Thanks :)
C test code has compiler warnings.
fixture.c:8:5: warning: implicit declaration of function 'str_of_int_array' is invalid in C99 [-Wimplicit-function-declaration] str_of_int_array(input_str, 80, array, size); ^ 1 warning generated. setup.c:5:9: warning: implicitly declaring library function 'snprintf' with type 'int (char *, unsigned long, const char *, ...)' [-Wimplicit-function-declaration] snprintf(buf, buf_size, "NULL"); ^ setup.c:5:9: note: include the header <stdio.h> or explicitly provide a declaration for 'snprintf' 1 warning generated.
Fixed in list of C katas to update
Why? Isn't the original order?
same
-7 is the lowest in the list but in your result doesn't appear. This kata is a little tricky.
Now it makes sense, thanks!
nice kata!
Thanks :)
Took tries on 2 separate days and a lot of frustration but managed to do it and learned some new things in the process, great kata, thanks!
Jesus. I actually spent my entire free Friday on this.I need to rest. 10 / 10 kata.
Kata addiction is nice ;)
C# - errors currently show name of the data structure rather than content.
Not BUG.This is the default behavior when Nunit compare arrays.In my opinion, there is generally no need to output answers.If you want to output the answer, you can submit a kumited.
That is fair enough.
For anyone else learning C# and wondering about why the results messages are different:
Nunit calls the default .ToString() on a collection.
In C#, an extra step is required to get the same results when compared to (for example) Java. Java will, by default, print out the content of an int[] array. Many NUnit tests on this site have overloaded the AreEqual method with a third parameter specifying a custom output. In this Kata, only the default AreEqual call is used.
Lets compare results between other languages and C#.
in C++
in JavaScript:
As you can see, there are ways that C# and C++ Kata give different results, but it is not required to solve this problem.
Something this simple would work:
C# Translation added.Please review and approve~
See error above.
Not a bug.See the comment above.(^_^)
Made a solution that works well in vscode, i got all the tests right but when i run it in codewars i get exit code: 139 Can anyone help?
Haskell random test
Falsifiable (after 24 tests):
expected: [-13,10,8,-20,-18,-10,-5,-8,-3,-21,-5]
but got: [-13,10,10,8,-20,-18,-10,-5,-8,-3,-21]
([17,-13,10,10,17,8,-20,-18,14,13,17,-10,-5,19,-8,14,-3,-21,13,-5],11)
I dont understand why in excected result is only one
10
... I am missing somethig?Not an issue. You're missing the second
-5
which is less than10
.This comment has been hidden.
Your code has problem with repeated numbers, the random tests show the input value, look carefully at what your code does when it fails.
Haskell : https://www.codewars.com/kumite/5b91fa539942f4938200003c?sel=5b91fa539942f4938200003c
NASM translation. Please review and approve.
Done :+1:
Can you update the description with NASM examples?
I added NASM examples to the description. These are the same examples as in C (because all test cases are in C). But I'm not sure that I used a correct tag for NASM. I wrote ```nasm but I don't know if it is correct or not.
Lovely, thanks!
Have you considered making a "performance" version?
I might actually work on that later; and a linear(ish) time solution would be indeed nice to enforce.
Whoops, performance edition kata already published ¯\_(ツ)_/¯
Spoiler: The
O(n*log(n))
solutions used here won't be able to pass the tests.@FArekkusu : nice! I'll check that out.
Ah, thief :D! Linked in my own description :+1:
Then
firstNSmallest([1,2,3,4,1],3)
should return[1,1,2]
because all1
s are equal and their first occurrence is the 1st1
?..Mh, no? You find first
1
, then2
and finally the second1
?Should be rephrased to "in case of duplicates, return them according to their position in the array", though IMHO this statement is not needed as
N smallest elements in original order
.The order of first occurences is 1 -> 2 -> 3 -> 4,
1
is first found at index 0, no? How can the same number1
have 2 different first occurences?I think it might help to think of the
1
and1
as two different elements: how would you work with it if it was about ranking objects?And I like FArekkusu's suggestion, I might add/reword it later.
FArekkusu's formulation is imho worse than the current, which is just slightly ambiguous. The issue is when "the n smallest" is ambiguous, e.g. for
firstNSmallest([1, 3, 2, 3], 3)
. It will take 1, 2, and one of the threes - you need to make sure the phrase makes clear that the first one has to be taken (output differs from[1, 3, 2]
to[1, 2, 3]
).PS: This detail made me confused for five minutes because FF59 and node sort differently, therefore code gave different results on different runtimes :)
"N smallest elements in original order" looks good to me. The word "first" is that makes the difference, at least it should be removed in some way.
In the instructions you said
firstNSmallest([5,4,3,2,1],3) === [1,2,3]
but it's actuallyfirstNSmallest([5,4,3,2,1],3) === [3,2,1]
Right! Let me finish the translations and I will fix it; in the tests should still be fine, isn't it?
Tests are correct.