7 kyu

Sorting in R: Rank

189 of 263saudiGuy
Description
Loading description...
Lists
Puzzles
Sorting
  • Please sign in or sign up to leave a comment.
  • tobeannouncd Avatar
    • saudiGuy Avatar
      Suggestion marked resolved by saudiGuy 9 months ago
    • JohanWiltink Avatar

      Integers and half-integers are represented exactly, so there is no need for approximate comparisons. If solver manages to lose precision, that's on solver.

      This may be a problem with other langauges as well, but I haven't looked at those.

    • tobeannouncd Avatar

      It doesn't really hurt to use approximate equality (unless it significantly impacts test performance), but you're absolutely right.

  • nomennescio Avatar

    A perfect example how ranking of katas is highly misleading; maybe a 7 kyu in Python, but no way this would be ranked 7 kyu if it was implemented in C first.

  • xianjin Avatar

    In Rust I get the following error while attemping to pass the Kata:

    error[E0277]: can't compare `f32` with `f64`
      --> src/lib.rs:40:24
       |
    40 |         assert!(actual == expected, 
       |                        ^^ no implementation for `f32 == f64`
       |
       = help: the trait `PartialEq<f64>` is not implemented for `f32`
       = help: the following other types implement trait `PartialEq<Rhs>`:
                 f32
                 f64
                 i128
                 i16
                 i32
                 i64
                 i8
                 isize
               and 6 others
       = note: required for `Vec<f32>` to implement `PartialEq<&[f64]>`
    
    For more information about this error, try `rustc --explain E0277`.
    error: could not compile `challenge` due to previous error
    
    
    NOTE: Line numbers in error messages can be incorrect due to concatenation.
    

    I return Vec<f32> as requested. Fixed tests run fine.

    • xianjin Avatar

      Is this going to be fixed anytime soon?

    • natan Avatar

      the type was changed from f32 to f64 (see thread below), and you got caught in-between (starting with f32 initial code but submitted after the change)

      Issue marked resolved by natan 2 years ago
  • xianjin Avatar

    It would be good to add what a rank is, because not everybody has experience in R.

    Rank of a vector element is its location relative to the other elements in an ordered vector.

  • akar-0 Avatar

    Rust translation (I suggest to wait for the approval of the kata, since there may be suggestions of improvements).

    • saudiGuy Avatar

      It seems like the testing sequence should prioritize testing with small random arrays first, giving users a better opportunity for debugging.


      Test Results:

      tests::big_random_arrays

      tests::fixed_tests

      tests::huge_random_arrays

      tests::medium_random_arrays

      tests::small_random_arrays


    • akar-0 Avatar

      Unlike Python and other interpreted languages, tests are executed in parallel in Rust (and several other languages). I have no possibility to change this and force a particular order of execution (well, strictly technically speaking I believe it is somehow feasible but it's advanced stuff I currently don't dominate, and AFAIK it is not done in any kata).

    • akar-0 Avatar

      Also, the way tests are designed doesn't make it hard to focus on small inputs for debugging, it's ok to fold / unfold any part one is interested in. For example (unfolding only fixed tests and small arrays, with a function just returning an empty vector vec![]):

      tests::big_random_arrays
      tests::fixed_tests
      With lst = [1, 3, 3, 9, 8]
      Expected [0.0, 1.5, 1.5, 4.0, 3.0]
      Got [] at src/lib.rs:26:9
      tests::huge_random_arrays
      tests::medium_random_arrays
      tests::small_random_arrays
      With lst = [43, 30, 15, 48, 38, 8, 49, 17, 28, 16, 9, 17, 40, 13, 0, 5, 47, 28]
      Expected [14.0, 11.0, 5.0, 16.0, 12.0, 2.0, 17.0, 7.5, 9.5, 6.0, 3.0, 7.5, 13.0, 4.0, 0.0, 1.0, 15.0, 9.5]
      Got [] at src/lib.rs:26:9
      
    • saudiGuy Avatar

      approved.

      Suggestion marked resolved by saudiGuy 2 years ago
    • NunoOliveira Avatar

      AFAIK, rust tests are executed in parallel but are reported in alphabetic order, so it should simply be a matter of just naming them in alphabetic order. I know this from reading discussions on the discord server, not from experience, so it might not be fully accurate.

    • akar-0 Avatar

      Hmmm, seems right. I didn't know that.

    • akar-0 Avatar

      saudyGuy: do you want me to change the tests so they appear in order of difficulty? I don't think it's actually useful for the reasons I have explained, but it doesn't bother me either to change the design of the tests if you prefer.

    • saudiGuy Avatar

      If it can be easily implemented, please go ahead.

    • monadius Avatar

      Initial solution: trait bounds Clone + Hash + Ord should be added to T.

      The return type must be f64. f32 is not precise enough for big tests (it is possible to get different results if the sum is computed with f32 directly). More precisely, f32 is enough for this problem but the reference solution does not compute average ranks accurately (all ranks are either integers or integers + 0.5 but the reference solution may return 13583.501 or 20734.002 for tests with T = char).

    • akar-0 Avatar

      I did the change. I was not sure about the output type. However, I don't understand how reference solution can return inaccurate results with chars especially (I think the result doesn't depend on the type of the input).

    • monadius Avatar

      The following expression was the source of imprecision: sum::<usize>() as f32. This sum may become larger than the largest safe integer for f32 (2**24) if a value is repeated sufficiently many times in the original array. It is very likely to get this situation in big tests with chars because only 26 chars are used.

    • akar-0 Avatar

      Indeed.. Damn floats! Thanks for the explanation!

  • akar-0 Avatar

    Random tests should also generate strings of more than one character, since some fixed tests also do (or harmonize both the other way). I am writing a Rust translation so I'd like it to be aligned with the original language and the description (Rust is strictly typed so I need to explicitly set the possibility to have either integers, chars or strings).

    • saudiGuy Avatar

      Since this is a 7kyu kata, let's keep things simple. Random inputs will generate single characters only.

    • akar-0 Avatar

      I am not sure it will be approved as 7kyu. The problem is actually that the description says Expect numbers and lower-case alphabetic characters only. while some fixed tests provide an array of strings. This should be aligned, and there is no reason random tests don't provide all the types one can find in fixed tests. In this case, you may also choose to remove/change the tests including strings with multiple characters.

      Also, I don't think it adds difficulty to the task using chars or strings, a single comparison works (at least in Python and Rust, and many languages; I saw rowcased only uses unsigned ints in his translation, which may be a reasonable choice since handling multiple types is not easy in C).

    • saudiGuy Avatar

      I have removed strings from both the fixed and full test suites. I believe that generating strings will reduce the likelihood of duplicate values, which is the primary purpose of this kata.

    • akar-0 Avatar

      Ok, Thanks, I will allign Rust on this. It is possible to generate dupe strings if needed, but it doesn't bring a great thing to the task.

      Suggestion marked resolved by akar-0 2 years ago
    • saudiGuy Avatar

      You're always welcome to edit the Python version as well. I would be happy to learn from you guys.

    • akar-0 Avatar

      Well, I am sure I don't need to teach you how to insert dupe strings randomly in an array ;)

      For me the kata is ok, I don't see currently how to improve it really.

  • rowcased Avatar
  • mauro-1 Avatar

    O(n²) solutions should not pass.

    • saudiGuy Avatar

      done.

      Suggestion marked resolved by saudiGuy 2 years ago
    • JohanWiltink Avatar

      7kyu kata should not have performance requirements. The kata should be reranked ( ask a mod ) or the performance requirement should be removed.

      Note that I haven't even solved the kata; I am just going off the description, the tags ( which don't include performance ?!? ), and this comment.