6 kyu

2 Arrays 1 Sort

151 of 294IVBakker
Description
Loading description...
Arrays
Algorithms
Sorting
  • Please sign in or sign up to leave a comment.
  • iNikAnn Avatar

    It was "SOMEWHAT" challenging 🙂

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • bouchert Avatar

    Even though an issue on this subject (changing the name of a parameter from compare to key) was marked as resolved, I feel the Python translation needs a bit more work. The description still contains Javascript code which includes the line res = 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.

  • fibonaccios Avatar

    I've never pressed NONE faster than this!

  • dfhwze Avatar

    JS: Not enough fixed test cases. I have to guess the method signature of the comparison function.

  • Awesome A.D. Avatar

    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.

  • user9644768 Avatar

    Ruby 3.0 should be enabled.

  • deepu10 Avatar

    Please provide better description. Not able to understand properly what to return.

  • i_romanenko Avatar

    ToSort array not sorted: [-71, -6, 0, 35] should equal [-6, -71, 0, 35] How is this right? tests should be reviewed...

  • johnhaup Avatar

    I think the description needs to be more clear and provide a solid example of inputs and an expected output.

  • zofy Avatar

    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

  • ChristianECooper Avatar

    Python: I'd suggest changing the parameter name from compare to key. Rationale: A compare method takes two parameters and returns in integer indicating order, a key method takes one parameter and returns the key to be used in a sort. For example, look at the parameters cmp and key on list.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.

  • GiacomoSorbi Avatar

    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.

  • bkaes Avatar

    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 all JSON.stringify calls:

    Test.assertSimilar(HowMany,  [0,1,5,6],                     'aToSort not sorted');
    Test.assertSimilar(Type,     ['jeans','house','pen','car'], 'aLinked not sorted');
    Test.assertSimilar(res,      [0,1,5,6],                     "returned value isn't aToSort");
    

    Additionally, you can drop the last test in the triple, and instead check whether HowMany and res are the same, as required:

    Test.assertSimilar(res,        HowMany,                     "returned value isn't aToSort");
    

    Otherwise the user could return a (shallow) copy, although that's rather unlikely.

  • Aliona_Kastsevich Avatar

    It says Linked array not sorted , but when i test it on my own, it returns a sorted linked array.. Smth is wrong.

  • computerguy103 Avatar

    This comment has been hidden.

  • wthit56 Avatar

    It seems there are no test cases that pass no compare function; only in the example test fixture, not in the actual kata tests.

  • wthit56 Avatar

    Not sure which array we're meant to be sorting with the comparison function?

  • computerguy103 Avatar

    There aren't any tests with duplicates in the Type array. The first way I tried to solve this worked okay if Type contained no duplicates. When I tried running a test with duplicates in Type, 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:

    • Having some duplicates in arrayToSort, linkedArray, or both
    • Sort Test.randomNumber() integers without using a comparison function, so it sorts by strings rather than numbers
    • Sort objects, e.g function (a, b) { return a.id - b.id; } to sort by the id property
    • Sort Math.random() * 10 numbers by their fractional portion, function (a, b) { return a % 1 - b % 1; }
    • more if you can think of them
  • computerguy103 Avatar

    In addition to sorting the original array, sort returns the array after it's sorted. What should the return value of linkedSort 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 want linkedSort to return anything (in which case, it wouldn't allow method chaining the way that sort 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).

  • ZozoFouchtra Avatar

    I'm not sure "Number" is really a good name to a var...