7 kyu

Minimum Steps (Array Series #6)

2,099 of 7,079MrZizoScream
Description
Loading description...
Fundamentals
Arrays
  • Please sign in or sign up to leave a comment.
  • Rekroma Avatar
  • saudiGuy Avatar
  • hobovsky Avatar

    This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/151.
    Please join the discussion to help us identify duplicate kata and retire them.

  • akar-0 Avatar

    COBOL translation (author inactive).

  • user6552504 Avatar

    PHP random tests expected value -1.

  • IvanVukosav Avatar

    How are these equal to 1

    It(Check_Small_Array_Size) { Assert::That(minimumSteps((vec{4,6,3}),7), Equals(1)); Assert::That(minimumSteps((vec{10,9,9,8}),17), Equals(1)); }

    and this equal to 2

    minimumSteps({1, 10, 12, 9, 2, 3}, 6) ==> return (2)

    The explanation and examples are not correct

  • farhanaditya Avatar

    JS: Node v12 should be used along with the appropriate assertion tools (Mocha + Chai)

  • ejini战神 Avatar

    Typescript: missing return type in solution setup ~~

  • akar-0 Avatar

    In Haskell sometimes there's no possible answer (description states the contrary) and we need to return the length of the whole list in that case.

  • user9644768 Avatar

    Ruby 3.0 should be enabled.
    Description should be re-written properly, right now heading size is huge, texts are unnecessarily emphasized.

  • user4896126 Avatar

    enjoyable kwata

  • username0 Avatar

    Suggestng to very carefully review this Rust translation.

  • rsa Avatar

    Clojure Translation ready for revire.

  • rsa Avatar

    python test

    ``` Testing for [9, 6, 99, 6, 7, 5] and 0 It should work for random inputs too: 0 should equal -1

    I don't think that -1 is the correct answer

  • sitetester Avatar

    Just faced 1 was not equal to -1 error on final 'Attemptstage. After debugging (adding print statements to check input parameters) found out thatkparameter value was< 0` Just shared a hint here to help someone ;)

  • Unnamed Avatar

    Prolog sample tests:

    test(example_test) :- minimum_steps([10,9,9,8], 7, R), assertion(R == 1).

    17 instead of 7?

  • B1ts Avatar
  • mahesh2492 Avatar

    I am new here so want to know that how can I know the input of random test? My solution is working for all test cases except 1. It is breaking for one -> 1 was not equal to -1

    Not sure when -1 will be output.

    Any help will be appreciated.

    Thanks

  • pnaib Avatar

    Description is confusing could be improved: It could be explained in many ways, but these two descriptions should reduce the confusion:

    1. Highlighting process of adding the smallest number to the total: "How many operations of addition we need to reach a sum which is equal to or greater than the given number (k) while adding only the next smallest number from the array."
    2. Highlighting sub-array length: "What is minimum length of the sub-array whose sum is equal to or greater than the given number (k)" (Optionally: given the elements of the array are non-descreasing.)
  • user8436785 Avatar
  • user8436785 Avatar

    CoffeeScript translation :coffee: :coffee: :coffee: :coffee: TypeScript translation

  • user8436785 Avatar

    @stellartux made a Julia translation

  • TlylT Avatar

    Test.assert_equals(minimum_steps([19,98,69,28,75,45,17,98,67], 464), 8) Is there a mistake in the above test case? 8 steps => should be 516 ?

  • user8436785 Avatar
  • user8436785 Avatar
  • clcraig Avatar

    This comment has been hidden.

  • cliffstamp Avatar
  • Unnamed Avatar

    All numbers will be positive.

    Ruby and Crystal random tests can have v = 0.

  • Unnamed Avatar

    And a third time, not fixed. Ruby:

    Testing for [9, 14, 96, 2, 34, 6, 63, 41, 5, 3, 30, 48, 83, 6, 75, 3, 89, 69, 24, 24, 86, 74, 4, 16, 2, 7, 13, 59, 2, 75, 45, 43, 7, 3, 78, 52, 10, 8, 9, 94, 59, 6, 5, 7, 6, 7, 10, 53] and 10
    It should work for random inputs too -  Expected: 5, instead got: 4
    

    Crystal:

     Testing for [1, 50, 100, 98, 19, 5, 95, 39, 7, 38, 6, 5, 4, 46, 48, 96, 7, 61, 8, 8, 76, 1, 1, 45, 34, 92, 64, 72, 79, 78, 81, 5, 75, 36, 5, 18, 8, 23, 33, 9, 86, 4, 96, 43, 7, 7, 4, 2] and 19
    Test Failed
    Expected: 13
         got: 7
    
  • Unnamed Avatar

    Everything is the same. The sum in Python: randint(0,sum(n)), the sum in C++: 3 + rand() % 199.

  • Unnamed Avatar

    Everything is the same. Ruby:

     Testing for [11, 2, 43, 38, 9, 37, 1, 4, 4, 61, 82, 11, 70, 51, 3, 45, 55, 2, 82, 15, 4, 5, 6, 9, 45, 1, 4] and 14
    It should work for random inputs too -  Expected: 8, instead got: 6
    ``
    Crystal:
    

    Testing for [1, 7, 3, 4, 7, 4, 8, 67, 1, 10, 6, 90, 7, 68, 5, 3, 2, 9] and 9 Test Failed Expected: 5 got: 4 ```

  • Unnamed Avatar

    The Python random tests can generate K = 0, but the description says:

    All numbers will be positive.

    I think it refers to all numbers including K? (But even in case it doesn't, it would be a must-have edge case test for other languages then.)

  • Unnamed Avatar

    And it looks like there's been the same issue with Ruby and Crystal as was with JS, but those versions aren't fixed yet.

    Testing for [7, 41, 47, 49, 6, 10, 6, 1, 8, 56, 7, 9, 9, 96, 1, 33, 9, 2, 9, 19, 7, 75, 48, 64, 66, 15, 9, 4, 10, 17, 5, 4, 94, 39, 82, 5, 4, 2, 86, 4, 25, 11] and 11
    Test Failed
    Expected: 7
         got: 5
    
  • Unnamed Avatar

    The JS version doesn't seem to have been fixed yet:

    Input: [ 100, 8, 91, 99, 7, 3, 81, 10, 2, 75, 9, 7, 70 ] 157
    Sorted: [ 2, 3, 7, 7, 8, 9, 10, 70, 75, 81, 91, 99, 100 ]
    Sums: [ 2, 5, 12, 19, 27, 36, 46, 116, 191, 272, 363, 462, 562 ]
    It should work for random inputs too - Expected: 9, instead got: 8
    
  • FArekkusu Avatar

    After Giacomo's changes to the reference solution (after description improvement from a week ago) I had to do some research and found out an interesting thing - the description is ambigous.

    Before I rewrote the description, the task was to do the following We need to count the number of smallest elements you need to take so that their sum is greater or equal to K (these are your words). I interpreted it like this:

    Given an array [1,1,1,1,1,1,1,1,1] and N = 3, the result has to be 3 because if we take 3 smallest numbers in the array (e.g. 1, 1 and 1 again),we will get 3.
    Even though the array will become [1,1,1,1,1,1,3].
    

    But now I noticed this phrase in the description (from example #1) ...now all of the elements in the list are greater than or equal to N which indicates that we need to add up smallest numbers until ALL numbers are greater than N, which means:

    Given an array [1,1,1,1,1,1,1,1,1] and N = 3, the result has to be 6 because after 6 addition operations we will get an array [3,3,3] where all numbers are >= 3.
    

    Which way is correct then?

  • GiacomoSorbi Avatar

    Any love for the Crystal translation?

  • FArekkusu Avatar

    Rewrote the description so it should be more readable now.

  • kontic Avatar

    I think everything after the "Task" part (in description) are unnecessary and confusing.

  • JohanWiltink Avatar

    (JS, possibly others)

    Needs a fixed test with K = 0. ( Any array, and expected = 0 )

    My first solution has a bug in that case, and it's almost never flagged by the random tests. Increasing the number of random tests wouldn't hurt - 40 is none too many; 100 would be entirely reasonable.

  • JohanWiltink Avatar

    You really need to redo the examples in line with the description. They're still based on adding the two smallest elements until all elements are greater than or equal to the threshold.

    I solved it in O(n log n), which seemed perfectly acceptable to the JS tests. The JS example implementation is also O(n log n) ( because of the sort ), so the mentioned expected and optimal time complexity are completely bogus. I am unsure it can be done in less than O(n log n) anyway - you'll always have to sort the initial list, which is an O(n log n) operation at best ( well, at worst best ).

    Also, please specify in the description the threshold will always be reachable. Rigging the tests so it'll never be a problem is nice, but you also have to specify sh*t will never happen. Because we are not mindreaders.

  • Darshan97 Avatar

    You could remove the notes part, it gobbles up a lot of space.

  • Blind4Basics Avatar

    ok, the description and the implementations do not make any sense.

    That has to be reworded so that it's understandable. What suggested Giacommo below does not work either.

  • Blind4Basics Avatar

    python:

    17 [10, 9, 9, 8]
    2 should equal 1
    

    Seems there is a typo in the test: how could this be 1 pop operation??

    Same for the second example: ...The newer List Become {23,10} , Now all the elements added from the list are greater than Or Equal 23 => what the heck!? And 10? Why is the expected result 3 and not 4?

    I feel like I don't understand the task to achieve, with what is currently explained...

  • GiacomoSorbi Avatar

    And before you change the description or title Python, Ruby, Crystal and JS translations kumited :)

  • donaldsebleung Avatar

    Description is confusing and inaccurate - it states that "We Need To Find Minimum Number Of Popping Opertaions In Which The Elements Of The List Can Be Added To Make >> All << The Elements Greater Than Or Equal To K" but the second code example contradicts this - the algorithm stops at {23, 10} when k == 23 despite 10 still being less than 23. Not to mention the discrepancy between the definition of "popping" presented here and the official CS definition of popping an item off a stack.

  • Voile Avatar

    The kata description requirement is completely different from the behaviour of the tests.

    The description is asking for finding the minimum amount of popping two values and pushing their sum operations that makes all elements at least as big as the target.

    However, what the actual tests are expected is: If we start adding from the smallest number, how many numbers are needed before we reach the target?

    So the whole kata is pointless right now. It needs a complete rewrite ;-)

  • Voile Avatar

    Reference solution fails when all numbers are bigger than the target number (it expected 1 while the correct answer should be 0).

    This happens rarely but still needs to be fixed ;-)

  • Voile Avatar

    Note that you currently only have 1 random test because the test assertion is outside the for loop.

  • Voile Avatar

    What if the total sum of the numbers is less than the input?

  • Voile Avatar
    2- minimumSteps ({8,9,10,4,2} , 23) // return (3)
    
    Explanation :: 
    
    First we add (4+2), Now The New List Becomes 
    {8,9,10}, Then We Add (6 + 8), Now the new 
    list becomes {9,10}, Then add (14+9) , So The newer List Become {10}  , Now all the elements added 
    from the list are greater than  Or Equal 23 . Hence the output is (3) i.e (3) operations are required 
    to do this. 
    

    Where are the elements when they're being added? e.g shouldn't {8,9,10} be {6,8,9,10}?

    And after the third step it should be {10,23}, so I think it should need another step?

  • ZED.CWT Avatar

    Sample Tests is not runnable