7 kyu
Minimum Steps (Array Series #6)
2,099 of 7,079MrZizoScream
Loading description...
Fundamentals
Arrays
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.
C# Translation
python new test framework
Approved
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.
COBOL translation (author inactive).
approved
PHP random tests expected value -1.
I've just solved this kata in PHP and had this log too on some attempts. My code only sorts the array in place and parses it. The strange thing is it seems I can't get the same error again now.
Yes, it happens with a low probability, but it happens. The control function is wrong or K should be at least the minimum value of the array.
I don't know. Looks like the Python tests had the same problem a year ago (comment by rsa below).
Changed K to be at least 1, it seems to be fixed now, but check it. Enabled PHP 8 too.
No answer after two months, closing.
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
Oh god, it is right there in explanation...
Thank you for explaining it again and not bashing me for it :)
JS: Node v12 should be used along with the appropriate assertion tools (Mocha + Chai)
Typescript: missing return type in solution setup ~~
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.
Fixed
Ruby 3.0 should be enabled.
Description should be re-written properly, right now heading size is huge, texts are unnecessarily emphasized.
Ruby 3.0 enabled in this fork
enjoyable kwata
Suggestng to very carefully review this Rust translation.
Revised and APPROVED :+1:
Clojure Translation ready for revire.
'alr approved by someone'
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 answerShould be fixed.
Just faced
1 was not equal to -1
error on final 'Attemptstage. After debugging (adding print statements to check input parameters) found out that
kparameter value was
< 0` Just shared a hint here to help someone ;)Prolog sample tests:
17
instead of7
?Fixed 1 typo, forgot the other...
Prolog translation kumited.
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
use a print statement... you will know a tests input value once it's been used, so you can at least study the results to it, but of course
because random
, you will not know what or when on the next run, study hard, good luck@mahesh2492 You can add a print statement for any random failing test by
if
condition to print all input parameters as well as your return value. print your return value to find out which test is failing and then add checks accordingly.OP solved it, closing
Description is confusing could be improved: It could be explained in many ways, but these two descriptions should reduce the confusion:
The second point isn't true, because when you add three numbers, the number of operations is two.
Elixir translation Reason translation
Did you miss a notifiaction? :trollface:
Reason?
This comment has been hidden.
CoffeeScript translation :coffee: :coffee: :coffee: :coffee: TypeScript translation
@stellartux made a Julia translation
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 ?
Maybe you didn't read the description correctly. It is 8 steps. Try rereading the description.
Oh yes, thank you!
PHP translation
Revised and APPROVED :+1:
Dart translation
This comment has been hidden.
@clcraig .. Reviewed and Approved .. Thanks :+1:
Haskell : https://www.codewars.com/kumite/5b75a4d2f71e5ddf030000e9?sel=5b75a4d2f71e5ddf030000e9
@cliffstamp ..
Ruby and Crystal random tests can have v = 0.
@Unnamed .. revised and Fixed .. thanks for pointing Out :blush: .. Regards .. Zizou
Fixed only in Ruby, not in Crystal.
@Unnamed .. revised and Fixed :+1: , Sorry for the previous one .. regards .. Zizou
And a third time, not fixed. Ruby:
Crystal:
@Unnamed .. Okay , No problem I'll do the necessary :wink: .. regards .. Zizou
I am considering about removing the translations, as I am just annoyed by the changes in the description and having been called a number of time to check it, not to mention the kata itself was not even so easy to understand or guess.
@Unnamed .. revised and fixed for both :wink: :wink: , alos I've revised the rest of languages :+1: .. regards .. Zizou
Everything is the same. The sum in Python:
randint(0,sum(n))
, the sum in C++:3 + rand() % 199
.@Unnamed .. Well .. thanks for your feedback Bro :+1: .. I'll do the necessary :blush: :blush: .. also Keeping the issue un-resolved till modifications occurs :wink: :wink: .. regards .. Zizou
This kata changed so many times that I am frankly sick and tired to adapt the solutions; now
k
is always> 0
, for the rest feel free to suggest or enact changes directly.@GiacomoSorbi ..
Everything is the same. Ruby:
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 ... Revised and Fixed :blush: :+1: .. Please , Try it now and feed me back .. Regards .. Zizou
The Python random tests can generate K = 0, but the description says:
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 .. Revised and fixed .. Please , check it now and feed me back .. regards .. Zizou :wink: :wink:
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.
@Unnamed .. Revised and fixed .. Please , check it now and feed me back .. regards .. Zizou :wink: :wink:
The JS version doesn't seem to have been fixed yet:
@Unnamed .. Well ..
All previous JS solutions were invalidated. Now the reference implementation matches neither the description and other language versions nor the alternative problem definition discussed below.
@Unnamed ..
@Unnamed ....
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: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 thanN
, which means:Which way is correct then?
This is getting more and more of a problem every time I look at this kata. The task is correct but examples are not (they contradict the sample tests):
Well, at least this time you were a bit more keen on discussion (at least in the EDIT) part; I am open to both ways, but Zizou asked me to refactor things down below (although c++ stayed the old way), so not exactly sure.
I would love to avoid redoing things for a third time, though.
As a note for the future, I think more attention to the description (on my part as well, I confess I got bored of the huge descriptions of some of the katas in this series) is definitely needed.
I think it is better to leave the current algorithm (first interpretation) and change description so it corresponds the algo rather than change everything back and forth.
@FArekkusu .. Well ..Alex ..
Yes, I did it in Python, and to remove the ambiguity and keep different versions consistent you should specify yourself what was the original task:
@FArekkusu ... Well ..Thanks Alex for your Patience :blush: :blush: ...
The examples explanation in the description still uses the wrong wording.
As I understand, the algo should work like this:
Therefore an explanation would be:
@FArekkusu .. Well Alex nice point .. :+1: :+1:
Tweaked the description again. Should be fine now.
@FArekkusu .. Seems Nice :yum: :yum: .. Many many thanks Alex .. This kata's discourse just beat up the satisfaction rate :joy: :joy: .. thanks Bro .. regards .. Zizou
Any love for the Crystal translation?
@GiacomoSorbi .. Well .. This Occurs because of This .. So , pleeeeease , just even Copy and paste the Translation , and I'm tuned to Approve it Instantly :blush: :blush: .. regards .. Zizou
Forked.
@GiacomoSorbi .. Well ..
Rewrote the description so it should be more readable now.
@FArekkusu ... Well ..
array_Series
Links at the beginning of the kata , also the end of it , Leaving just a link of Authored Kata :wink: :wink: :sunglasses: :sunglasses: , This will make the Trainee much more task-focused than Dispersed here and there :dizzy: :dizzy:A shame you gave anothe reading to the ambiguous part compared to me and other solvers, so I had to rewrite the solutions for it to match; the c++ code is probably still doing something else and there might be mismatches, though...
I think everything after the "Task" part (in description) are unnecessary and confusing.
This comment has been hidden.
@MrZizoScream, sorry for my comment above. I need to precise what I meant. My problem is with this line in description: "ALL of the elements in the list are greater than or equal to...". Does this still applies?
@kontic .. Well ..
Ok then, explain next example step-by-step in description:
After that you'll get from me 'Very Satisfied' vote. I hope that my suggestion is ok.
@kontic .. Well .. Ivan ..
Example-test above is one of randomly generated tests for JS.
@kontic .. Well Iavn ..
All modified, so that now it matches the new description.
What have you done? You've invalidated all the solutions.
EDIT: Done a little research and reversed everything for now (you replaced algo from task section with algo from example explanation section, yes?). Going to raise an issue due to ambiguous description.
(JS, possibly others)
Needs a fixed test with
K = 0
. ( Any array, andexpected = 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 .. Thanks for Notifying me Johan :blush: :blush:
JS
Translator to revise and modify if necessary .. :blush: :blush:Agree. Tests for
K = 0
(orK <= 0
in general) could be added as a special case (note: the description, sample tests and random tests would need to be updated then).Oh, I now see I had missed Note #2. However, JS random testing can generate
K=0
.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 alsoO(n log n)
( because of thesort
), so the mentioned expected and optimal time complexity are completely bogus. I am unsure it can be done in less thanO(n log n)
anyway - you'll always have to sort the initial list, which is anO(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.
@JohanWiltink .. Well .. Johan
Could you point me towards a solution more efficient than
O(n log n)
?I've checked most solutions in all languages, and everybody sorts.
sort
is anO(n log n)
operation; to the very best of my knowledge it cannot be done inO(n)
, so every solution that sorts isO(n log n)
at best.I am very, very curious how you're going to solve this in
O(n)
.This comment has been hidden.
I changed my vote from not to somewhat satisfied after your previous post. I still think you need to clean up the examples, and I would like to see an
O(n)
reference solution in at least one languages if you claim it can be done in the description. I'll do it in JS if you do it in any other language.Using a Min Heap sounds like sorting in
O(n)
would be possible. Create a Min Heap inO(n)
, extract min elements inO(log n)
until you have all of 'em, voilà,sort
inO(n)
. I'd like to see that done, and why isn't everybody doing that? ( Possibly because extractingn
elements isO(n log n)
total. But we are not removing all values. But possibly, we are. So complexity depends onn
andK
, with worst caseO(n log n)
, again, still and forever. )I'd like to see the reference implementation.
You could remove the
notes
part, it gobbles up a lot of space.@Darshan97 .. Well ..
.
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.
that's not that either. You have to sum the lowest remaining elements until you reach the threshold. The description is absolutely cryptic about what is actually requirered...
@Blind4Basics .. Well .. Bro .. I'll Revise the Whole Description , Refining it from Scratch to more understandability :relaxed: :relaxed: .. and i'll keep the issue Inresolved till modifications take place .. :thumbsup: :thumbsup: .. regards Zizou
thx. I'll update my rating after that, then.
@Blind4Basics ...
well, Actually, one of those two is mine. ;) Let's say that avoid an approval before the changes are made. ;)
Note: maybe you should unpublish the kata, for the time you needed to update it. If not, it may get more downvotes before you do update it (and some may not come by again for changing it again... :/ )
@Blind4Basics .. Well .. Many many thanks Bro for instructing ..
Mmmh... Though, it's still not good imo. Considere this, for example:
with that case:
({6,8,9,10}, 23)
-> ends with {23,10}, so returns 3 steps, you're explaining thatNow all the elements added from the list are greater than or equal (23)
(note that this is missing at least one word, btw).At this point, the definition and the example tell the user that he has to add some values, so that the sum is higher than the defined threshold. Ok.
But with this one:
({1,10,12,9,2,3} , 6)
the above would mean that 1 step could be enough too: pop 10 and whatever other number and you'll get a sum greater than 6 in one step. Though, the expected answer is 2, not 1.That means your specs aren't thurough enough yet. Again, what you're asking for is actually the following:
You really should stick to something like that.
btw, note that this part of the description:
to make ALL the elements greater than or equal to K
is in complete contradiction with most of the cases.@Blind4Basics .. Well ..
that's better, but that part is still incorrect:
to make all the elements greater than or equal to K.
You cannot keep that when you get a[23,10]
in the example while K=17.Mmmh... I just saw you changed the second example (though you missed one 10 that still appears in the explanation). That's not good either, unless the python version isn't up to date anymore?
Let's try this: I personnaly would rewrite it that way:
That's actually what is currently needed in python. Note that your poping/insertion stuff isn't simple at all to explain. Especially, considering the example I used before (the one you changed):
That's why I would avoid that formulation and go for what I suggested.
@Blind4Basics .. Well ..
isounds good, now. ;)
@Blind4Basics ... Finaaaaaaaaaaaaaaaaaaaaly :relaxed: :relaxed: .. thanks Bro For Patience .. the Satisfaction rate just Jumped from 63% to 75% .. Really I'm Grateful Bro regards .. Zizou
python:
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...
The original description is not superclear, but apparently you need to count the numbers you have to sum until you get at least K as minimum value of the array.
X-o
hummm... "not very clear", yeah. It rather tells something different, I'd say. ;)
And before you change the description or title Python, Ruby, Crystal and JS translations kumited :)
@GiacomoSorbi .... hahhahaha :joy: :joy: .. I was in the Editing page of this kata :smile: :smile: .... Big Approval to Them all :laughing: :laughing: :laughing: ... regards .. Zizou
@GiacomoSorbi
It looks like the Ruby & crystal versions are broken -- see issue above@anter69 ..
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}
whenk == 23
despite10
still being less than23
. Not to mention the discrepancy between the definition of "popping" presented here and the official CS definition of popping an item off a stack.@donaldsebleung .. Refined from scratch .. thanks for instructing donalds .. hope you the best Bro .. regards .. Zizou
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 ;-)
Alright .. Not to be vulgar or rude ... By The number of Issues have been raised in this kata only beats my Hair amount , (Kidding) , ..
Revised carfully from Scratch
..Description
has been Explained more Clearly ..Sample test Cases
have been fixed .. and The Solution has been Updated .. You're WelcomeVoile
... and It's Alaways a great honor Solving and Rating my Humble Kata ... Regards ..Zizou
I prefer to raise separate issues for separate things as they're easier to track and manage this way.
You know, like how issue trackers work in every other place. You'd never put 10 problems in 1 issue on most places, everyone will hate you for that ;-)
Nice Point
Voile
... Yes it's easier to manage ... But The Satisfaction rate You voted Reflects How you content about this kata .. (Kidding) ... Hope You the Best .. and thanks again for the instructions .. reagrdsZizou
Reference solution fails when all numbers are bigger than the target number (it expected
1
while the correct answer should be0
).This happens rarely but still needs to be fixed ;-)
Revised .. and Fixed ..
Note that you currently only have 1 random test because the test assertion is outside the for loop.
REvised and Fixed ... Now its inside the loop ..
What if the total sum of the numbers is less than the input?
Yeeeeeeeah Nice question
Voile
... I came prepared ... in the random test cases I took into consideration This probability and i handled it ... ThanksWhere 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?Yes... You're right
Voile
... Fully-Revised From the Description to the Test cases and sample Test cases also ...Thanks For instructing
...Sample Tests
is not runnableThanks
Charley
For instructing ... Revised and Fixed