6 kyu
Exercise in Summing
1,085 of 2,100jmusto
Loading description...
Arrays
Fundamentals
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.
Nice one, well done.
python : random tests always test for
n=200
python new test framework is required. updated in this fork
Fun kata. I notice I'm one of the only ones to use negative n for both functions.
Hi, can you help me ? "maximumSum with n>values.length not working as expected"
var values = [5, 4, 3, 2, 1]; console.log( maximumSum(values, 7)) // 7 > 5 return 5
What is problem ?
If
n
is greater than the length ofvalues
, you should return the sum of the whole array. That's not what you are doing there :thank you for your response. I have solved this Kata. Could be better if you add to this Kata "If n is greater than the length of values, you should return the sum of the whole array. "
Ruby translation
No random tests.
Someone fixed.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
You change the data in
values
.I think the tests can't be changed now. At least, what behavior is expected for negative
n
s in Haskell?There's a problem with the Haskell random tests, I think it's switching minimum and maximum function calls.
This comment has been hidden.
The instructions say: "n: how many integers should be summed; always 0 or bigger"
However the random tests seem to also give input data with negative n, see the following failure message for haskell:
Falsifiable (after 2 tests): expected: 0 but got: 1 [1] -1
This comment has been hidden.
Not a kata issue, you have everything in the error message.
This comment has been hidden.
Hello! In statemants I see: "values always 0 or bigger" but when I try to run my programm on test cases I see: "uncaught exception: ErrorCall (Prelude.tail: empty list) (after 2 tests) [] -1" Is there "[] -1" valid test?
Tests seems have index off by one. My results look correct to me. Or, I'm doing something wrong.
minimumSum should work for some examples Test Passed should work for random inputs Falsifiable (after 15 tests and 13 shrinks): expected: 0 but got: 1 [0,0,0,1,0] 4
minimumSum should work for some examples Test Passed should work for random inputs Falsifiable (after 8 tests and 8 shrinks): expected: -1 but got: 0 [0,0,-1] 1
minimumSum should work for some examples Test Passed should work for random inputs Falsifiable (after 7 tests and 5 shrinks): expected: -1 but got: 0 [0,-1] 1
You could add your solution to your comment. Your results don't match the expected value, but without your code it's not clear how to help you.
thanks for the response. It was my mistake.
I guess I have to read the instructions more carefully...
The instruction somewhat clearer: "if n is larger than values.length, simply include all values to the sum"!!!!!
Got an error : "maximumSum with n=3 not working" while submitting. While inspecting console.log gives :
What's wrong???
Hi,
If you try to run an test of your own with the following values, what do you get as an answer?
Your functions are changing the
values
array. It wasn't originally[11, 12]
- yourminimumSum
function changed it.Try logging
values
before and after calling your functions to see that it's being changed within your function:Javascript passes arrays and objects by reference, so if your code changes the array, it can have unintended side effects. Always try to avoid changing objects and arrays that were passed as arguments. If you need to modify them, make a copy first and modify that.
When trying to submit I recieve the following error:
"maximumSum with n>values.length not working as expected"
How is it expected to work?
Thanks for any input.
Hi,
From the instructions: "if n is larger than values.length, simply include values.length integers to the sum"
i.e. these should return the same value:
(Marking this issue as resolved, as I feel it isn't really an issue per se.)
This comment has been hidden.
The problematic parts would be here:
Point being, assigning array (ie.
values
) to a new variable doesn't make even a shallow copy of the original array, i.e. any operations (in this case especially.shift()
as it removes the element from the underlying array) made to the copy are reflected to the original. So, I'd suggest either making a "real" copy of the original array or using techniques that don't alter the original object. Hopefully this helps you enough :)I have a problem to pass the test: minimumSum with large random array not working - Expected: 191, instead got: 5881
It's not the first time I run into this kind of problem, it seems to appear when using "sort" on a very large array. Is it really my code that has a problem or the "compiler" that can't handle more than a certain number of actions?
Should the function sort be used in this kata?
Yes, sort should work correctly. Could you please, provide sample of code so I can help?
This comment has been hidden.
The problem in the delegate function, you are passing to sort function. The compare function should return number, not boolean: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Try something like that:
Thanks
This comment has been hidden.
OP solved it, closing
7 kyu is a bit underevaluated. There is multiple non-trivial things to do in this kata.
Made 6 kyu
Great Kata. My only complaint is that the "add values.length integers to the array if n>values.length" confused me. I wrote code that calculated the array sum and then added values.length*(n-values.length)
How does the kata handle multiples of the same number?
This comment has been hidden.
Yes, in case of all negative numbers, for example -5 to -1 as stated in your comment, the 2 maximum numbers are indeed -1 and -2. So, maximumSum would in that case be -3.
However, the 2 minimum numbers are -5 and -4, and minimumSum is then -9. Hopefully this helps :)
This comment has been hidden.
Hi Chris!
Sorry it took me a while to answer - and actually sorry also for the bit misleading error message. Try this in your test suite, and see what happens:
The point you had missed is the last bullet point in the instructions:
But now that this came up, I think I'll edit the test suite a bit, to include a separate test for this.
Hey jmusto,
thanks for your answer. You are right, I missed that instruction. I got it to work now :)
If you don't mind I would like to suggest you to put the instruction somewhat clearer by saying "if n is larger than values.length, simply include all values to the sum"
I get the same error message "Test Failed: maximumSum with n>values.length not working as expected" but I pass the test you offered (Test.assertEquals(minimumSum(new Array(5, 4, 3, 2, -11), 8), maximumSum(new Array(5, 4, 3, 2, -11), 8));) ... No idea what to do.
In case you get stuck a good check is to write out the input given - in this case "console.log(values, n)". Build your own test cases based on this and it will often help you on :)
Kata should explain what should happen when n is negative.
In the kata test cases,
n
is never negative (and how would you sum negative amount of integers?). The integers in thevalues
array may, however, be negative (there is one such test case).I'll edit the kata description a bit, to make this more obvious.
This comment has been hidden.
This kata should be reranked, the use of negative numbers puts this above simple fundamentals
What do others think? I don't have a strict opinion either way.
Agree. I am going through every single 7kyu exercise right now and this one is excessively more difficult than any other I've tried so far. At least 6kyu. Maybe 5, although I wouldn't know because I haven't done any 5s yet...
I think you should add something describing what should happen if
n
is larger thanvalues.length
. Other than that, nice kata! ^^Good point - what was meant to happen is simply to include all values to both sums, I'll add something regarding that to the description.
And thanks! My very first authored kata here, and not really based on any real-world problem, but thought something like this would be a simple and nice kata to work on :) And I wanted really bad to try authoring a kata ;)