6 kyu
2 Arrays 1 Sort
151 of 294IVBakker
Loading description...
Arrays
Algorithms
Sorting
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 was "SOMEWHAT" challenging 🙂
python new test framework is required. updated in this fork
Even though an issue on this subject (changing the name of a parameter from
compare
tokey
) was marked as resolved, I feel the Python translation needs a bit more work. The description still contains Javascript code which includes the lineres = linkedSort(HowMany,Type,function(a,b){return a-b;})
. This still misled me into thinking the key parameter would in fact be a two-parameter compare-style sorting function, (which I would need to use cmp_to_key on before using it with any built-in Python sorting functions).It does not help that neither of the example tests have a custom sorting function, so there are no code examples to help demonstrate what sort of functions will be passed in.
I've never pressed NONE faster than this!
JS: Not enough fixed test cases. I have to guess the method signature of the comparison function.
Python: First off, it's kinda silly to both sort the array in place and also return it. One or the other would suffice. Secondly: the sample tests won't even run because the Initial Solution hasn't made the
key
parameter optional. It's not a big deal to fix on the user side, but an initial solution should at least run without errors.This is a suggestion at best. If you don't like this requirement, reraise it as such.
Making users set default arguments for optional parameters is a normal practice.
Initial solution is a mere template. Claiming that it should be always valid and runnable is wrong, and if somebody actually added such point to documentation, they're also wrong.
But that's like, just, your opinion, man.
Ruby 3.0 should be enabled.
Please provide better description. Not able to understand properly what to return.
How is the example not enough? It shows, input, call, outputs
ToSort array not sorted: [-71, -6, 0, 35] should equal [-6, -71, 0, 35] How is this right? tests should be reviewed...
Hi, this is discussed in the last part of the description:
alphabetical sorting:
"-6" < "-71" < "0" < "35"
numerical sorting:
-71 < -6 < 0 < 35
I think the description needs to be more clear and provide a solid example of inputs and an expected output.
I've update the example, is it better?
Hi, I am stuck at random tests...test says that a_to_sort is not sorted but when I print it it is...It s really weird.... Thanks in adavnce
A type issue? string != int ('3' != 3 but they are printed the same way)
Python: I'd suggest changing the parameter name from
compare
tokey
. Rationale: Acompare
method takes two parameters and returns in integer indicating order, akey
method takes one parameter and returns the key to be used in a sort. For example, look at the parameterscmp
andkey
onlist.sort()
.This confused me for a while, as my solution assumed the compare method passed in was meant to do the comparison, rather than supply the key used in a built-in comparison.
Thanks I didn't pay attention to that when accepting the Python
Translated it into both Ruby and Python :)
I have to thank you again, as you rather peculiar katas forced me somehow out of the usual path and I learned something new (like how to pass a lambda function to Ruby's sort method; Python has it much more easy, I'd say).
I changed most of the names to camel_case, but other than that I sticked to your formula as much as possible: once (and if, of course) you approve them, let me know with a reply to this comment, as I would like to edit the description a tiny bit to explain how to behave when not sorting function is given in these two languages.
The description uses rather unidiomatic variable/array names. Usually only constructors/prototypes start with capital letter, whereas variables, functions, methods, arrays, … start with lowercase letters. Also, the test framework provides
Test.assertSimilar
which takes care of arrays, so you can drop allJSON.stringify
calls:Additionally, you can drop the last test in the triple, and instead check whether
HowMany
andres
are the same, as required:Otherwise the user could return a (shallow) copy, although that's rather unlikely.
It says Linked array not sorted , but when i test it on my own, it returns a sorted linked array.. Smth is wrong.
It should display the difference between the results, isn't it?
Yes, Expected&got are different on codewars, but when i run it by myself everything is ok=)
@Aliona_Kastsevich: Add the code in a comment, but mark it as a spoiler. Don't forget to add correct formatting.
This comment has been hidden.
aLinked = []
removes the reference to the outside array, and thereforeaLinked
will stay the same outside your function. Note that your solution won't work on arrays that contain duplicate elements.Agree with the first part, my mistake..( But I don't understand, what do you mean, when You say that it'll not work 4 duplications. What kind of behaviour it should be? As now if the input is:
The result is:
Is it what was expected?
From what bkaes is saying and by looking to your code the following wouldn't work:
Because it returns:
instead of
This comment has been hidden.
Thanks for pointing that out. Solution, tests and description have been updated accordingly.
Thanks. The only thing more I'd suggest is adding a link to the
Array.prototype.sort
documentation.It seems there are no test cases that pass no compare function; only in the example test fixture, not in the actual kata tests.
Added
Cool ;P
Now it has a test with no compare function, but it expects to perform number sorting in that case.
Array.prototype.sort
is defined to do string sorting when no compare function is given.Number sorting and string sorting are not the same thing.
Solved
Not sure which array we're meant to be sorting with the comparison function?
The array you're supposed to compare by is named
arrayToSort
. The other one is linked to it, so for every element that moves inarrayToSort
, the corresponding element inlinkedArray
needs to move similarly.Yeah, I think I got confused with the way the description showed
Type = ...
, and thenHowMany = ...
;PThere aren't any tests with duplicates in the
Type
array. The first way I tried to solve this worked okay ifType
contained no duplicates. When I tried running a test with duplicates inType
, it failed to sort the linked array properly.After solving it and looking at your test cases, I'd say that it is pretty light on the test cases. I'd recommend:
arrayToSort
,linkedArray
, or bothTest.randomNumber()
integers without using a comparison function, so it sorts by strings rather than numbersfunction (a, b) { return a.id - b.id; }
to sort by theid
propertyMath.random() * 10
numbers by their fractional portion,function (a, b) { return a % 1 - b % 1; }
Thanks, I still need to discover a little bit more the Test reference.
Is there any way I can use my own solution in the tests? I'm thinking of the random tests. Or should I create a linkedSortReference function? I'll improve the tests. I got a little bit lazy on this one :)
Sure. Just have your solution in the test fixture. You'd have to clone the input arrays before passing them to the coder's solution, but then you can just run them as per usual, and use
Test.assertSimilar
to test if the arrays from your solution and the coder's solution are the same.By the way, there is documentation for all languages on what test methods are available: http://www.codewars.com/docs/js-slash-coffeescript-test-reference
I added a String compare function. In any case the point is not really to test sort functions.
If the original issue hasn't yet been fixed, it's not going to be.
Closing.
In addition to sorting the original array,
sort
returns the array after it's sorted. What should the return value oflinkedSort
be? It's sorting 2 arrays at once, so I'd assume it should return one of them, but which one? Or specify in the description that you don't wantlinkedSort
to return anything (in which case, it wouldn't allow method chaining the way thatsort
does).It would help if you'd clarify the description so that instead of saying "the result" you'd say that it sorts the original arrays (and what it should return). Normally I'd think of the "result" of a function as the value it returns, but in this case you're using it to mean the function's side effects (argument arrays are sorted).
I'm not sure "
Number
" is really a good name to a var...Done