6 kyu
Equal Sides Of An Array
8,685 of 131,795Shivo
Loading description...
Algorithms
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.
Hi there, I am having doubts about Length 2 sized arrays. When n=0, there is nothing at the left (because there is no -1 position), so the answer should be 1 if position 1 equals to 0. When n=1, there is something at the left (position 0) and nothing to the right (last position), so the answer should be 0 if position 0 equals to 0.
So I don't understand why {8,8} should be 1! I mean, n is not being counted, but the numbers at the left and at the right of n, isn't it? Am I misreading something or missing something?
NVM I read wrong: The test is telling my code is failing because for {8,8} should give me -1 and it's giving me 1 (which is totally wrong)...
Sorry ^^;
chrono is the goat
Easy task but requires attention and some time... Proud I was able to pull it off
This one was challenging but was fun to complete!!
Can't really understand the description for this one, {1,2,3,4,3,2,1} yeah s ure, 4 is the sum of both the left side and right side of the array. Then this {1,100,50,-51,1,1} the sum result of 50, -51, 1, 1 is the same as the first position in the array which is 1, sure ok. But now this part just makes me feel dumb:
"You are given the array {20,10,-80,10,10,15,35} At index 0 the left side is {} The right side is {10,-80,10,10,15,35} They both are equal to 0 when added. (Empty arrays are equal to 0 in this problem) Index 0 is the place where the left side and right side are equal."
The left side is empty? And Index 0 is the place where the left side and right side are equal? Whaaat?!
there are no elements to the left of index
0
, so the left side of index0
sums to0
.in mathematics, the sum of an empty sequence is
0
. this is natural if you want the formulasum(a + b + c + ...) = a + sum(b + c + ...)
to be true in all cases; otherwise, this would not hold forsum(a)
.I've given up on this challenge, thanks for the answer but I can't even remember already wth was going on that challenge, just know that 20 was there and should be the first position of the array from what i remember but instead 0 was given
never give up!!
Maybe I'm understanding it wrong but if the test is (3, {1,2,3,4,3,2,1}) shouldn't it be 1 because it says return the lowest index and 1 equals 1
I don't know what you meant there, but both sides of the number you choose, must have the same sum, at index 3, on the left you have 1+2+3 and on the right 3+2+1. That's the only valid answer.
This comment has been hidden.
Because those are different indices, 1 and 5 (6 - 1). If you really wanted to use index 1, to the left, the sum is 1 and to the right, 9, and that's not a valid solution.
nice task
test is broken The array was: {8,0} Expected: 0 But was: -1 The array was: {8,8} Expected: -1 But was: 0
Both tests are fine, read the description again.
test.assert_equals(find_even_index([-15,5,11,17,19,-17,20,-6,17,-17,19,16,-15,-6,20,17]),8) it is incorrect, right answer is 9
i found my mistake
Python kata problem: Prepared 3rd and 2nd test cases give wrong answer:
Both should be 0 (or am I stupid???)
Both tests are fine, in the first one if you take the 0-index, to the left you have 0, and to the right -10. In the second one, 0 to the left, 3 to the right. Not a kata issue.
Thank you! Should have known it was a me problem for such a popular kata and not an issue =)
Very nice!
I did not find any indication in the description of this kata that if multiple indexes exist for a given array, the first index should be returned. I have a failing test for the input
[0, 0, 0, 0, 0]
and the test suggests that the correct answer is index 0. Any index of this array should be a correct answer in my opinion, as again, there was no indication that if multiple indexes fulfil the requirements the first (or leftmost) is the correct answer. Correct me if I am wrong, but I believe the test in this case are biased towards the solution. All my other tests pass.It is specified in the sections Output and Note that you should return the lowest index in case there are multiple valid solutions.
Thanks for pointing that out. Although, I believe it would be cleaner to have all important information at the top.
Anybody solved the problem in last one in the basic cases in attempts?(in Python)
Your difficulties with solving the kata are not really a kata issue.
Rust issue. Instructions expect to return unsigned -1 for no available solution, check is for None in this case.
Added rust-specific info to the description.
dont understand my result log:
The array was: [20,10,-80,10,10,15,35]: expected -1 to equal +0
+0 ???
Look at the last example in the kata's description again. Your code returns -1 when the expected answer is 0.
I have an issue in c++ code, in this test vector numbers { 1,100,50,-51,1,1 };
In my IDE output is: 1, but in code wars i have output -1.
This is my code
int find_even_index (const std::vector numbers) { int sum_r = 0; int sum_l = 0; for(int i = 0; i < numbers.size(); i++){ for (int l = 0; l < i; l++) { sum_l += numbers[l]; } for (long int r = numbers.size(); r > i; r--) { sum_r += numbers[r]; } if (sum_l == sum_r) { return i; } else sum_l = 0; sum_r = 0; } return -1; }
Please help me find out what is the problem
C++ issue; Test 1: given the vector 3, 1, 0, 3, 0. Expected result is 0, actual result is -1 (+1 on my machine). This is blatantly wrong. Test 2: given a vector with a lot of zeros which I will not bother typing. Expected result is 9 (blatantly wrong), I have not tested this on my machine but looking at test 1 is enough for me.
This comment has been hidden.
You're not printing the input there, there is no such test. Not a kata issue. Your code fails this sample test for example:
@Chrono79 You make a good point lol, I had forgotten to delete that from testing. Sorry about the flame in the other post. Btw, I am getting "expected 0" and failing because i am returning -1(as was said to do in the description), whats going on there?
That test is the last example in the description, and it says there that 0 should be returned, -1 should only be returned when there is no index where the array is balanced.
Thank you for the help.
It prints that -1 is the wrong answer, and says that the correct answer is +0. And how to solve this?
Fixing your code, it fails the last example in the description. Not a kata issue.
LC translation
This comment has been hidden.
Added that test.
This comment has been hidden.
Please do not post solution in discourse, not a kata suggestion
I apologize for not knowing clearly and doing this.
python new test framework is required. updated in this fork
Approved
This comment has been hidden.
That code only returns the result if certain conditions are met. What if the condition never met? Read the description again what the kata expect for those conditions.
True ... but - my sum1 = sum and the index comes back on the first example [1,2,3,4,3,2,1] but after being equal never runs the return. Try it!
Ah, so it wasn't about return values....
The test server gives you the error:
When slicing an array, you might end up with an empty array, and your
reduce
didn't have an initial value to handle this situation (reducing emptiness?). That's why the tests stop/crash and not because your return value.This comment has been hidden.
Hmm, I did try your code above and only tested the 1st sample test, and it pass!
Try the RESET button, but don't forget to copy your solution code first.
The tests are broken. In places the return values are 0 where they should be -1
Why won't my return i give me the value. It is in the if statement. i does equal what I am asking for but it will not return and goes thru the next arr.
You seem to suggest that a return statement isn't doing what it's supposed to. That won't be the case. Instead it would be something such as not having executed that return statement.
It may be helpful to keep your code consistently formatted even while writing it. It may also help to only run a single test case while you're debugging so that there's no mixup. Isolating what you want to look at and making sure everything's consistent will make it easier to make correct observations of what is happening.
So yes - I have lots of extra leftover comments and have cleaned it up. One will be for the not equal value but you see that the return b; does not throw produce the passed test for the first array even though the value of i is "reported" as being correct. That is 3.
This comment has been hidden.
How can I get the arrays that are used to verify your attempt? I keep failing a test, because it expects 1, while my program outputs -1.
This comment has been hidden.
See https://docs.codewars.com/training/training-example#debugging-a-kata and https://docs.codewars.com/training/troubleshooting#print-input.
When I try to attemp the solution codewars tell me that a test fail, because it spect -1 as answer, and when I run the same code in local, response with -1 not with +0....
The tests are fine. 32K coders have solved it in JavaScript. The feedback message
expected -1 to equal +0
means that your code returned
-1
, whereas it should have been the correct answer0
;Also, "It works on my machine" is not a solid enough stance upon which to perceive an
issue
, because the tests may not match the correct tests on site.Print the input to help you debug your code: https://docs.codewars.com/training/troubleshooting/#print-input
Yes you are right @rowcase...sorry
Hello, is it possible to somehow deduce the expected result from the tests. Otherwise, the test crashes, I display the source array, but I don't understand what the test expects at the output
Description has "1st position of the array" which is technically incorrect use of English where first indicates no other object comes before it, which in this case is incorrect. Better would be to use "at index 1 of the array" for 0-based arrays. Same for other instances.
This comment has been hidden.
The node version used in codewars currently doesn't support ECMA 2023. So, you can't use
toSpliced
here.This comment has been hidden.
There's an issue with the Rust version of this kata. According to the problem description, "if you are given an array with multiple answers, return the lowest correct index". This is not true in the Rust translation. Here is an example where it breaks:
This random test expected an answer of
Some(202)
which IS a valid answer, but my code returnedSome(199)
, which is ALSO a valid answer and has the lower index.This happens when the kata tries to generate a random test with a guaranteed answer but doesn't check that its answer is the lowest valid answer, instead just assuming it's the only answer. This is easy to fix -- just replace
dotest(&arr, Some(i))
withdotest(&arr, reference_solution(&arr))
(and probably do a slight refactor to get rid of the extraneouselse
block). I have a proposed fix here: https://www.codewars.com/kumite/649c7f7f0b00c8003ea81022?sel=649c7f7f0b00c8003ea81022Approved, thanks.
This comment has been hidden.
0
andlast_index - 1
are not valid solutions in your example.This comment has been hidden.
Read again the description, it's the last example. Not a kata issue.
Thanks!
Hey just a bit stuck. I seem to get everything to pass on the test, but not the attempt. I understand that the submit runs a ton of random tests on top of the basic tests. I pass all the random tests that are added, but fail one of the basic tests that I believe tests for a situation in which the total is 0 and the index is needed for returning is 0. Let me know if you want more information, but I am just not able to fathom the problem at this point. Appreciate any help.
This comment has been hidden.
Which language?
they only train in
PHP
... so, that. but, yeah, they should state the language when commentingThis comment has been hidden.
Read the description's last example, it's explained there.
ohh, i didnt see it, thank you
since arr<1000 , it's obvious that the program is O(n^2)
This comment has been hidden.
Hi @wubajono - regarding the comment:
note that you can see the specific input for any Codewars kata by printing the input to console as explained in the troubleshooting guide here
In Python,this just means: add a
print(arr)
statement inside your function and it will display in console.Doing this you will see that you fail on inputs:
[10, -10]
your code returns 0, correct answer is -1and input:
[-3, 2, 1, 0]
your code returns 0, correct answer is 3Cheers mate, will look into this, thanks
This comment has been hidden.
At the right of Index 2,you have {4,3,2,1}
no ,
This comment has been hidden.
index 0 is 10, the left side is 0 and the right side is -10, so it's not the same case as the all zero array at all.
This comment has been hidden.
It says left and right and it's shown the pivot is not included in neither side in the examples.
Thanks for clarifying.
This comment has been hidden.
Print the input, not a kata issue.
Hi, I'm getting this error on test [ The array was: [20, 10,-80, 10, 10, 15, 35] : expected -1 to equal 0 ] but the entry array is [20, 10, 30, 10, 10, 15, 35].
I'm working with JavaScript Node v14.x. Also I test the entry array with my code and the result is 3 not 0.
That exact case is explained in the description.
One of the tests I am failing is ([10,-80,10,10,15,35,20]),6), but I don't see how 6 is the right answer. My code says index zero which seems to check out since 10 does equal -80 + 10 + 10 + 15 + 35 + 20. At index 6 it is saying that 20 = 10 - 80 + 10 + 10 + 15 + 35 but it doesm't, it equals 0. Am I doing something wrong or is this test just incorrect?
Hi @Jchristian297 - for this kata, if you specify an index
i
, then the "left sum" is all elements to the left ofi
not including i itself and similarly for the "right sum" is all elements to the right ofi
not including i itselfSo let's look at
[5,6,444,2,7,2]
:In the above illustration, the answer is index
2
because to the left we have5+6 = 11
and to the right we have2+7+2 = 11
. NOTE WE DON'T INCLUDE444
in either sum.For your test case
[10,-80,10,10,15,35,20]
, if you say index0
, then the sum to the left of element10
is0
and the sum to the right is-80 + 10 + 10 + 15 + 35 + 20 = 10 != 0
.The correct answer here is index
6
because, to the left of index6
we have:10,-80,10,10,15,35
(note how we don't include20
) and to the right of index6
we have0
. And10 -80 + 10 + 10 + 15 + 35 = 0
as required.The Rust kata has a small bug, the initial function signature is
But the tests expect it to be
Fixable by the user but should be fixed in the kata
fixed
This comment has been hidden.
20 < 23. You're wrong.
COBOL translation, I don;t know COBOL and just copied it from another, duplicate kata, so please review carefully.
I could be wrong, but I don't think my code should have solved this problem. For example, if my method were given the following array [1,2,3,4,5], it would compare, at index 1, (1+2) against the sum of (2..5), because I coded this way: .each_with_index {|e,i| if arr[0..i].reduce(:+) == arri..-1...}, but my code soved the tests. The instructions asked for the sum on either side of the index to be equal, not including the index position number, but that's not what my code does, but my code passed.
You do realise that if
1+2 = 2+3+4+5
, then1 = 3+4+5
?Not a kata issue.
Thank you!
My code works for all the core test but fails a random test. I can't figure out the issue and I can't find an array that it wouldn't work on: (code in replies)
This comment has been hidden.
Use a console.log statement to print the input arrays
He (or she) works in C++, but still, should print the input to debug his/her code. Not a kata issue. Read this: https://docs.codewars.com/training/troubleshooting#print-input
It fails with inputs like this:
{ 1, 2, 2 }
the expected value is -1 and it returns 1.Sorry, I didn't know there was a document about troubleshooting on this site and after reading that I should've marked it as a "Question" not as an "issue". And I didn't even think about just printing the input arrays. Thanks for helping.
No problem.
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/166.
Please join the discussion to help us identify duplicate kata and retire them.
This kata was decided to stay.
please somone help me, my solution works perfectly on my computer but does not work in codewars:
This comment has been hidden.
This comment has been hidden.
i have the same problem
This comment has been hidden.
Your code is only trying to check whether the upper-section of the array's sum equals to the lower section. In other words, you are comparing the starting element with its corresponding last element and trying to check if their summation equals. However, it is only able to pass the first few test cases and fail other cases including this:
[20,10,-80,10,10,15,35]
. The expected result should be0
becauseLEFT SUMMATION of index
0
-->0
RIGHT SUMMATION of index0
-->0
(10 + -80 + 10 + 10 + 15 + 35)But, your code returns
-1
I think the test [1,2,3,4,5,6] will return the answer 3. it is because 1+2+3 is 6. If it is my misunderstand please tell me, thank you.
The answer corresponds to an index in the array, and you must then sum the values to the left of this index and compare this to the sum of the right values.
For
[1,2,3,4,5,6]
, the value at index 3 is4
. The values to the left of that index are at index 0,1,2 :1+2+3 = 6
the values to the right are at index 4,5:5+6 = 11
.So the index 3 is not correct answer - because
6 != 11
- in fact this input does not have a valid answer.OMG, very thank you for explaining to me, I think I have understood how to now. Very thank you.
There is a seemingly wrong test: "The array was: [8]: expected -1 to equal 0". The test seems wrong because the left side is an empty array that sums to 0, and the right side is [8] that sums to 8; thus, the index of 0 does not lead to equal sums.
The right side is also 0. Read the description again.
Scala translation
approved
Factor translation
Approved
the test case array was [-3, 2, 1, 0] expected: -1 my code returned 3 which was correct since the left side of my code equaled to 0
This is the test, and it expects 3, not -1:
The array was: [10,-80,10,10,15,35,20] : expected -1 to equal 6
Is wrong?
index 6 => 20 (the last index)
It can't be the first or last index because it compare sides.
Read the description again. Not a kata issue.
To my understanding, when index 6 == '20' start comparing, from right there is no corresponding so that means it is equal to 0 based on description, and the sum to the left side is also 0, so i think that's why it shows index 6. Actually, i'm having a hard time dealing with this specific test case lol
'0 should equal -1' 11th basic test what is this?
It means you returned 0 but the correct answer is -1.
i know but, can't understand what are the test numbers
https://docs.codewars.com/training/troubleshooting#print-input
This is only tested by chance, there should be (at least) some fixed test about this condition.
Fixed tests for this are included in python at least.
In JavaScript and Java (at least) such test doesn't exist.
Such test now exists in all languages
This comment has been hidden.
Please mark your post as having spoiler content next time and read this: https://docs.codewars.com/training/troubleshooting/#post-discourse
Why are you returning the first array value instead when there is more than one answer?
D translation
approved
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I just answered you below pointing out how you can find this out for yourself.
Thank you.
A really nice kata!
I can’t understand what the problem is: on the test check it says that everything is successful (You have passed all of the tests!:)), but when I want to complete the test it says "Test Failed Expected: 1 But was: -1"
I am getting the same thing. My code passes every test that I make. I can't figure out what test it is failing.
Did you ever figure it out?
If you want help, you need to specify the language you're trying this in.
As for figuring out "which test is failing", you can print the test inputs to the console.
It's happening the same thing to me, did you figure it out???
@Awesome A.D.: thanks for the tip about printing during the test phase...it's a good thing to know (which I did not). Big thumb's up!
approv2d
Hello. How to find which test was failed?
How to print -1 instead of None?
return -1
.
Hi guys, I really don't know what to do. The test passes without errors, random selection also passes without errors. But an error occurs in FindEvenIndexTest -> testIt
How to understand it
Language?
php
I can drop my Code
If you do it, use a spoiler flag.
This comment has been hidden.
I just spent 2 hours of my life trying to find a mistake instead of taking a proper look at problem description.
Read your manual before turning the appliance on! :D
How [1,2,3,4,3,2,1] should return 3, if 1+2 equals 2+1? the kata says "If you are given an array with multiple answers, return the lowest correct index."
It's explained in the description first example. Please read it.
yeah i read it, but in the 2nd position the sum of left side of the index ({1,2}) and the sum of the right side of the index ({2,1}) both equal 3, being the lowest index.. anyway, maybe i should sleep and try again tomorrow lol
The array was: [20,10,-80,10,10,15,35] : expected -1 to equal 0
How is it supposed to be a 0? The sum of all integers does not equal to 0.
Read the description, last example.
Don't try to solve things when sleepy, got it! Thank you!
In case anybody misread like I did: "sum of the integers to the left of N is equal to the sum of the integers to the right of N"
I interpreted this as sum of N from the left == sum of N from the right, and I couldn't understand why the first test failed on [1,...,1] which returns 0 (in my incorrect code).
Read carefully! Thanks for the kata.
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting#post-discourse
index
returns the first occurrence of the element in the list.Challenge users to do it in O(n)
I like how the slowest solutions are the "best practices"
My code passed all but one basic test, but.. I don't know what was the array of the basic test
https://docs.codewars.com/training/troubleshooting/#print-input
Ah I see, thank you!
I got 50 correct test and 1 incorrect saying: The array was: [10,-80,10,10,15,35,20] : expected 0 to equal 6
Hos is this correct? 10=(10-80+10+10+15+35+20) right??
Read carefully the description and see the examples: the value at the returned index must not be included. So with index 0, the left side is 0, the right side is 10, it doesn't work.
My soltion passed all of the test, expect last test, where write: "expected:<-1> but was:<0>". Language is Java, Thanks.
The tests are designed to stop as soon as user's code returns a bad answer, so most probably your code would fail on several random tests. You can print the input: https://docs.codewars.com/training/troubleshooting/#print-input
This comment has been hidden.
MKarzoun:
Your code is returning -1 instead of 0. Not the other way around.
Trying to utilize the reduce function correctly for this problem was throwing me for a real loop! If you're trying to reduce an empty array like I was it will always throw an error. You'll need an initial value first. Feels nice having my first rank up.
As soon as I saw the condition of the problem, I wanted to skip it, but I forced myself to take up the problem and it actually turned out to be very interesting and not difficult at all!
Excellent kata
Hi, everybody. I don't understand why my solution fails one test. I have problem with the array [10,-80,10,10,15,35,20] (kata gives an error: expected -1 to equal 6), my solution gives as result -1. Why this test fails? All other tests are passed. Thanks a lot.
Because your answer is wrong, that's why. Check the last example in the kata's description, it's very similar to this one.
Oh, now I understand. I missed one case when the answer is the last element. Thank you so much.
There is definitely a mistake in the checking class on line 14. The integers sum do not match so in should return -1
assertEquals(1,Kata.findEvenIndex(new int[] {2824, 1774, -1490, -9084, -9696, 23094})); to assertEquals(-1,Kata.findEvenIndex(new int[] {2824, 1774, -1490, -9084, -9696, 23094}));
The test is fine, read the kata's description again.
This comment has been hidden.
The answer of the test case that has input [-3 2 1 0] should be 3. However, it has -1 as an answer which should be fixed I think.
The log appears above the test result, you're confusing the result of the previous one with the input of the current one. Read this: https://docs.codewars.com/training/troubleshooting#print-input
You split the array on 3 parts not 2: 1st: from 0 till X : -3, 2 2nd: X : 1 3rd: after X till end. : 0 Thats why its -1. [-3, 2] =/= [0]
This comment has been hidden.
The log appears above the test result, you're confusing the input of the next one with the result of the current one.
Im stupid. Thanks you.
I've got some error "Testing for [14, -3, 5, -6, -14, -5, 5, -6, 0, -3] It should work for random inputs too: 6 should equal 5" Here the correct answers are 5 and 6. My solution gave the result 5, and I got this error. Of course I corrected my code so it gives the result 6 without any mistakes. But now I don't know how to understand this task's instruction with the following "Note: If you are given an array with multiple answers, return the lowest correct index."
If you see:
6 should equal 5
your function returned 6, instead of 5. 5 is the lowest index and that's the expected answer.This comment has been hidden.
the expected answer is
0
for this array. are you sure your browser knows this?Oh. thanks Man, I found my mistake.
This comment has been hidden.
Print the input to analyze why it fails, and please mark your post as having spoiler content next time.
This comment has been hidden.
Your return line is wrong, it won't ever return 0. 0 is a falsy value, so, it will return -1 instead.
This comment has been hidden.
If you talk about Python tests, the test with that input, expects -1. The log appear above the test result, maybe you're confusing the log of one test and the error message of another.
thanks, i see it now. my error. i see it is the [8]. i'll fix my code
This comment has been hidden.
10 -80 + 10 + 10 + 15 + 35 = 0
and right of the 20 (index 6) there is no other number, that counts as 0 too. It's similar to the last example in the description.索引是从0开始的,索引6左边的所有数的和为0
This comment has been hidden.
Please read this: https://docs.codewars.com/training/troubleshooting/#post-discourse and use markdown formatting.
Thanks. I'll make sure I format better next time.
This comment has been hidden.
Seems difficult at first but after looking at it for a bit it's quite simple... Nice kata!
interesting kata, though not difficult
This comment has been hidden.
Please see if this paragraph: https://docs.codewars.com/training/troubleshooting#kata-bug , or the article in general, contain anything helpful.
Stating your language would help a lot. Seeing your code too.
This comment has been hidden.
No, that test is explained in the kata's description, read it again.
Omg, I'm sorry! Thank you
I have issues with this problem. Test cases fail on this array: {1,2,3,4,3,2,1,1,100,50,-51,1,1,1,2,3,4,5,6,20,10,30,10,10,15,35,8,8,8,0,0,8} My solution returned -1, but the test expected 1, which is impossible since there is no way that the numbers on the right side after first position will evaluate to left.
That's not the input of a single test, that's the input of several tests one after the other, add a new line after printing the input.
This comment has been hidden.
Hi everybody,
my issue is in JS.
For some reason, i dont understand it, my Kata-Solution workes in VS Code but not on the same test cases in CodeWars.
I dont know whats wrong.
thank you for suggestions. :)
"Works in my IDE" doesn't mean that it's correct, and without seeing your code nobody can help you. Anyway, not an issue.
sharp-ID please read this: https://docs.codewars.com/training/troubleshooting/
@ Chrono79 thank you that helped me :)
@ FArekkusu I thank you too for your response. Sorry if my question insolts you, but you should understand that some people just started with codewars. I was not sure if I would spoil something or how the hole process with question worked. Anyway Thank you and i wish you a good day
I have the below one that failed saying :
Testing for [-17, -20, 19, 7, 17, -12, 17, 19, -30] It should work for random inputs too: 0 should equal 5
--> I fully disagree it should be indeed 0 - as the sum of all numbers = 0
Am I missing something?
You didn't understand the kata. Though the description is very clear:
find an index N where the sum of the integers to the left of N is equal to the sum of the integers to the right of N.
. You don't need to calculate the sum of the array.Thanks ....my mistake - I have it right now!!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
So, what's wrong with O(n^2)?? Isn't that polynomial time? Yes, and I was surprised to find out (while taking the courses I'll list below) that polynomial isn't good enough any more. Way back, when I was a computer science undergrad, polynomial time was fine. But Things Have Changed. That's because of increasing amounts of data, which means much MUCH larger problem sizes. And, critically, it means that the data very likely will not fit in memory.
So look at what happens if our array is so large that it has to be stored out on disk, or in the cloud, and only some fixed-length piece of the array can be brought into memory at once. Consider the solution that repeatedly calls sum on each end of the array. On each pass, it touches all but one element of the array. That means it has to re-read the array in from wherever it's stored on each pass.
Now, about those algorithms resources... I can recommend several things:
First is Udi Manber's book Introduction to Algorithms: A Creative Approach. It attempts to teach how to design or invent algorithms, not just teach algorithms that someone else came up with. (In grad school, I overheard one algorithms prof say they did not like Manber's book, for the same reason that I did like it. The prof thought students should be weeded out based on whether they could come up with algorithms with no training. I didn't and don't agree.)
Second are the two Princeton algorithms courses available though Coursera. These were a lot of fun (IMO), not just because of the interesting problems, but also because the course's forums were very active. Students were friendly and helpful -- careful not to give away answers, but cheering each other on. One interesting and relevant feature was that programming problems were tested to determine their runtime and space complexity. One didn't get full credit until one had an optimal solution. The courses are free. One caveat -- I took the courses some while back, and they were run in sessions so that a large batch of students went through the course together. That's one reason there were lots of students chatting in the forums. Coursera has made some changes, and I don't know if these courses are still run in sessions. That may not matter if there are enough students in the course at any one time.
https://online.princeton.edu/node/201 https://www.coursera.org/learn/algorithms-part1
https://online.princeton.edu/node/166 https://www.coursera.org/learn/algorithms-part2
Hi,
I didn't read the wall of text yet, but...
First, thanx for sharing.
Second... don't share solutions in the discourse unless you use the spoiler flag! ;) All messages can be read by anyone here, meaning you just dropped the solution for everyone to see... (I flagged your first message already)
edit: ok, now that I read the wall of text, I see there is nothing problematic in your first message and you already used the flag on the others. Well done. :+1:
(Note for later use: comments under solutions are visible from the dashboard too, so there too, you'd have to use the spoiler flag)
cheers
Hi, Blind4Basics!
Thanks very much for pointing to the dashboard -- I was wondering how to see my profile, and get back to previous katas, to try them in other languages.
Heh. I missed your line that started "edit:" until after I posted. Hmm...I wonder if I read your post first before the edit, and the page got refreshed at some point...
Just to be clear: There is no spoiler code in the first post. The code there is timing test code. It's included partly so people can verify how the output I'm showing was produced, and in case folks want to run timing tests on their own code, and aren't familiar with Python timing tools. Even if people do not read my replies where the code is, it is important that people know that the popular solution is effectively incorrect, because it is far from the optimal complexity, which is linear. (As mentioned, in the Princeton algorithms courses, one doesn't get credit on homework unless it hits the optimal time and space complexity. :D )
What I would like to know is what are the best practices for discussing issues that require referring to solutions. Ideally, the Codewars platform would separate the forum into posts visible before solving, and posts visible after. Are you saying that it is always appropriate to mark the first post in a discussion as having spoiler content, if any post in the discussion does? I was assuming that only the specific posts with spoiler code should be so marked.
Edit: Ooookay, I was going to add a bit more clarification that the code in the first post was just test code, but I guess I can't edit the first post any more -- the Edit button has vanished. I don't know if that is because there's a time limit for edits, or if no edits are allowed once there is a reply, or...?
yes, I removed the flag I put there, after I read the text.
"Caution, mother of virtues". Better to flag then unflag if it was unnecesseray than leaving a potential solution in the wild, imo.
definitely not.
I'm not entirely sure what you're exactly talking about there.
cheers
Yep, we were reading each other's replies in between edits...
Ok, so there are later problems that do require optimal time complexity. In that case, I won't comment on that. And I was wondering if anyone would see this. Also wondering if someone else had brought this up, but not sure how to search just the posts on this one problem.
This comment has been hidden.
You should say which language you're using. You probably misunderstand something and need to analyze it better. See if this can help: https://docs.codewars.com/training/troubleshooting. If you do think there's a problem or something impossible to understand, you may post your code with a spoiler flag, and clear examples of logs/test cases.
If I do console.log(index) it returns the right value. But if I do return index, it says it's undefined.
Solved by changing Node version to 8.1.3/Babel, because it shows all tests results. Node 10 was showing only the test that was returning undefined
.
This comment has been hidden.
Please don't spoil solution on the discourse page, the solution page is here for that.
well this is not the solution. I'm getting error so, asked here for help... once, I will get answer. will delete this too...
Ok sorry, better ask the question at the very beginning of your message! The trick is you can't delete a message once you got an answer ;)
Well you code returns -1 while it should return 0... We can't tell more without knowing the case. See if this can help: https://docs.codewars.com/training/troubleshooting.
Great Kata Thank You creator!
How do what the test is that I'm failing? This is all that shows up:
Test Results: ValidateWordTest GenericTests Test Failed Expected: 0 But was: -1 Completed in 78.4560ms RandomStringTests Completed in 169.1130ms
Thank you for any help!
Read this: https://docs.codewars.com/training/troubleshooting#print-input
Thank you!
My code passed 9/10 tasks and 1 problem is this: ''' -1 should equal 3 Log [20, 10, -80, 10, 10, 15, 35] 0 ''' i dont understand, why it should equal 3, my answer '0' is right ... and what '-1' is all about .. ?
Logs appear before the assertion, not after it. Not an issue.
oop my bad :/ how can i delete this question now? :d
You can't delete the comment, once an answer has been submitted. But take in consideration from now on always to check your code before raising an issue.
This comment has been hidden.
Not sure why, but when I run the test in Visual studio it works, yet here I am getting a type mismatch error: /home/codewarrior/program.fsx(18,17): error FS0001: Type mismatch. Expecting a 'int * int []'
but given a 'int * int [] * 'a'
The tuples have differing lengths of 2 and 3
But I am not using any tuples and the function is just returning an integer for the index. (so function signature is: int array -> int
I am getting same error if just return any random integer.
Can anyone help
If results from your outside environment do not match those of Codewars, question whether those tests are in fact truly set up in the exactly same way. If not, trust the expectations of this site over anything else. Check error messages and proceed from there. Hope that helps.
Thanks for your answer. I figured that much, but I still do not get why I am seeing the error. As the function should be returning an int (and it is) then it should not throw a type mismatch error..? Or am I wrong here? The test could obviously fail if my code would be incorrect, but certainly not a type mismatch error?
Update:
It seems that Codewars cannot handle array slicing. I am making sub arrays using: let left = items.[0..index-1] let right = items.[index+1..]
When I remove these slices from the code I am no longer seeing the error.. I figure however that this is correct F# code as it builds and runs in Visual studio without any problem.
Strange problem
Update 2: In the end it wasn't the slicing that was a problem. I was iterating using a for loop with a range from 1..items.Length causing an index out of bounds mistake. This was however not reflected in the error output.
Thanks anyways for your help.
You're partially right for the first part -- F# didn't allow slices to go out of bounds safely until F# 5.0. F# 5.0 returns an empty collection, while prior versions threw a runtime exception. I ran into this same issue just now.
Thanks for clarifying, I didn't know that. I also didn't know that the upper value in a range is included when iterating it using a for loop. I really like F#, but I feel that the error messages a lot of the times aren't descriptive enough, and because of that they will often have you look for the problem in the wrong location. Anyways thanks again!
nice kata
This comment has been hidden.
There is no such fixed test, and random tests are longer.
Not a kata issue. Seeing your code, you're inserting a
0
at the start, so it's your code what's wrong.I found my mistake. Sorry for the complaints. Thanks for the help.
Nice kata! I'm a beginner and this took me 2 hours + to finish it but definately difficult compared to most of other 6 Kyu questions! Thanks for this!
This comment has been hidden.
Why are you sorting anything? Read the description again.
For the below test input in Attempt,test is failing for me but when i run the same in my local machine it returns the true answer -1. [1,2,3,4,3,2,1,1,100,50,-51,1,1,1,2,3,4,5,6,20,10,30,10,10,15,35,-8505,-5130,1926,-9026,2824,1774,-1490,-9084,-9696,23094,4,5,6,7,8,9,10,9,8,7,6,5,4,8,8,8,0,0,8]
Anyone has any idea why this is happening?
That's not the input of a single test, that's the input of several tests one after the other, add a new line after printing the input.
Hi Chrono79,
Thanks for your reply.First i also thought the same but i have written System.out.println() at the start of the function in loop.So this will be the single input.
Just see the sample tests. devagx below asked the same.
Hi Chrono,
You were right.That was the input of several tests.So i tried to segregate it and it was as below: call 1,2,3,4,3,2,1, call 1,100,50,-51,1,1, call 1,2,3,4,5,6, call 20,10,30,10,10,15,35, call -8505,-5130,1926,-9026, call 2824,1774,-1490,-9084,-9696,23094, call 4,5,6,7,8,9,10,9,8,7,6,5,4, call 8,8,
Now,when i have taken each and every input and ran the code on my local machine,it gave me the right result for every individual test whereas when i run the code in codewars,it says expecting -1 but found 1 after the above mentioned lines.
nice kata!
Perhaps I'm being pedantic, but
0 < arr < 1000
doesn't look right to me for the length of the array in the description, since the namearr
is in use as the name of the array itself. Wouldn't it be easy to spell out the range of lengths of the array without notation? For instance, 'There will be fewer than one thousand elements in the array.'I think it is important to let people know that it also has to be above 0. Either way, I do not consider this an issue.
is there a way to view "Basic Test" input? receving the following output: "Passed: 57 Failed: 1 Exit Code: 1"
Hi. Please say the language you're using when you ask something related to you code. If Python you can print the arguments to the screen with print.
OP solved it, closing
This comment has been hidden.
I don't think you need .each at all here, and I think you'll find the array arr will accept .count but not .length (length works for strings but not arrays) – does this help?
You performed checking on whether the left-end equals to right-end when the iteration is still going (i.e, the loop has not reached its endpoint), resulting in inaccurate comparison. Also, your code will always output
nil
because you had an early exit before returning the result, plus nothing to be returned when there is no match between left-end and right-endhow it can be after pressing test button I got: You have passed all of the tests! :) and after pressing attempt button: CoreTests failed on the same code?
Because the core tests contain random tests, to make sure your code always works. If you do not pass, there must be some inputs which cause your solution to fail. :)
Maybe this will help:
https://docs.codewars.com/training/troubleshooting#test-attempt
This comment has been hidden.
Somehow this was challenging. At first I was like "maybe this test is broken" then I tried to reach the right answer even if the test was broken.
In the end it wasn't broken but when I tried to understand how it worked it made me see things I couldn't before which was essential to undertand where was the problem in my code.
Something wrong with this Kata. My code passes all 50 tests except one for input [20,10,-80,10,10,15,35] which expects 0. but according to question o/p must be equal to -1. the o/p will be 0 only if the number 20 on the 0th index is removed. please correct me if i'm wrong.
That's exactly the last example in the kata's description.
Answer 0 is correct. Left hand side of index 0 is nothing which sums to 0; Right hand side of index 0 is 10 + (-80) + 10 + 10 + 15+ 35 = 0 This scenario fulfils the question definition. So the answer is 0
I cannot submit my code. I get the error:
Parse error: syntax error, unexpected '=', expecting ';' in /workspace/default/_solution.php on line 8
This comment has been hidden.
Hello @ShubhamKaudewar
suggestion
, meanwhile your comment should be posted as aquestion
thank you
Thank you I follow instruction from next time onwards. Actually I solved in python but I want to know error in my JS code. Other 50 cases passed expect given array I am unable to find the error.
Okay, so when I run your code I get
TypeError: Reduce of empty array with no initial value
, so you should investigate that. Also, you can check out this documentation on Troubleshooting-your-solution.In description is: "Input: (...) The numbers in the array can be any integer positive or negative." And then in tests there is for example [0,8]. Zero (0) is neither positive nor negative so the description is wrong.
oh ye
Not an issue
Testing for [42, 9, -4, 3, 1, 9, 8, 0, 5, 17, 0, -16, -1, 20]
Failed attempt: 0 should equal 3
42 + 9 - 4 = 47... (index 3) ... 1 + 9 + 8 + 0 + 5 + 17 + 0 - 16 - 1 + 20 = 43
This comment has been hidden.
has anybody solved this on ruby?! i've read that its flawed... maybe thats why im struggling? any hits of what direction to take would be great!
thanks!
The tests are ok.
No errors in output failed=0 in Attempt but says MAX buffer size reached!!!!
If it's in javascript, it's caused by your code:
Read this: https://docs.codewars.com/training/troubleshooting/
Yes I written my code in javascript
I don't get any error in ATTEMPT but I gave an error says MAX buffer size reached.
there is something wrong with the c# kata my tests pass but one of the attempts fail
Check the input your code is failing. Read this please: https://docs.codewars.com/training/troubleshooting/
im having issues with hidden test case in java. Anyone getting same? test case is below. It says my code returns 0 running the kata here, but i run my code in my IDE and its returning -1 which the Kata expects. 1, 2, 3, 4, 3, 2, 1, 1, 100, 50, -51, 1, 1, 1, 2, 3, 4, 5, 6, 20, 10, 30, 10, 10, 15, 35, -8505, -5130, 1926, -9026, 2824, 1774, -1490, -9084, -9696, 23094, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 8, 8
Not a kata issue, and those are several input values together, please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
.
Hi devagx,
How did you resolve the issue?
This comment has been hidden.
This comment has been hidden.
did you use slices?
This comment has been hidden.
This comment has been hidden.
Both of those tests expect
-1
in Java.Then why, when submitting an attempt, am I receiving for one or both of those cases of the first test:
expected:<0> but was:<-1>
No idea, sometimes it's because of logs misreading, but not in this case, and sometimes it can be because you use a global var and it keeps its value between tests, read this and see if it helps: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution I can only see the tests and not your code so I can't see the whole picture here.
This kata is broken for C#. Same input is used in two different test cases at least and gives me two different results ensuring it's impossible for me to actually complete this kata. I've even tried randomizing the output between 0 and -1 to try to get around it but that hasn't worked yet, but the array [1,2,3,4,5,6] wants both a 0 and -1 answer, which is ridiculous. I pass all the randomized test cases with flying colors.
C# version of the kata works good for me. You are either misinterpreting the logs, have some bug in your solution, or kata has some bug which does not manifest for every solution.
If you are 100% sure your solution is correct, post it here (see here how), and someone will check what's wrong.
This comment has been hidden.
You probably debugged your solution somehow incorrectly. Tests do not fail for
[1,2,3,4,5,6]
, but for[8, 0]
. Expected answer is0
(sum of elements to the right of8
and to the left of8
) is equal, but your solution returns -1.Not a kata issue, your solution is incorrect.
I think that this test is wrong (c++):
{ vector numbers { 1,2,3,4,5,6 }; int expected = -1; Assert::That(find_even_index (numbers), Equals(expected)); } I think the answer should be 3 because left side {1, 2, 3} is equal to {6}
No, where did the 5 go? You can only skip 1 value (the one at index N).
I'm new to using codewars, so this may be a silly question. When clicking 'test' everything passes and works fine. When clicking 'attempt' it fails. Does this mean that there is something wrong with my code? Does 'attempt' use a different set of tests that are hidden?
Yes,
attempt
contains different tests, but you can see them by printing them.Hi,
nothing anormal, actually, because the "test" button is for the sample tests only (those you see in the trainer), while "attempt" is running the complete test suite. So if you fail this, that just means your code isn't handling properly some kind of inputs. You'll find "some" tips here
Btw: this is a question, not an issue (because "issues" are for a dysfunctionning kata. Here it's just a problem with your code)
Cheers
Ah thank you for this. Sorry - I shall make sure to be careful with tags in the future
This comment has been hidden.
Apologies for necro. Just cleaning up my kata's issue list. You probably already know this at this point, but the issue button is for issues with the kata itself. It is not used for issues with your code.
This comment has been hidden.
I bet you can do it with pen and paper (writing down the input array and figuring out the answer manually)
You'd win that bet but I have no idea what methods I'd use in python
Maybe not. But that would be a leap from where you are now. What's between where you are and that? Consider what actions it is that you would carry out. Maybe write them down. Maybe write them down as comments, one comment per action. After that you would have many small problems instead of one big one. You probably already know how to solve some of the problems. You could fill in those parts. For those that you don't, you still have a description of what you want to make happen. That's probably something that can be turned into a google query.
The overall thing might be too much to solve in one step. But you can split it up and those things are probably all very approachable.
This comment has been hidden.
Didn't mean that the whole thing should be googled, but rather individual actions that you require to build the overall thing. >_< If you're going to skip to the end then you should be forfeiting the kata which will let you see the solutions. It's really not a problem this time but this must not be how you do it. On the other hand, for known problems it can be quite reasonable to google for a solution, or rather, an algorithm, which one then implements on ones own. The idea is always okay to google.
You would come up with the same thing when working on it manually - humans are optimizing problem solvers and this problem has a very clear pattern that we are very likely to pick up when we do it enough times. Also, pen and paper is in itself not at all required, it's just the best way I can describe processing the problem. Putting your mind to work on it instead of staring at it. For many problems you get a lot for free just for being human, allowing you to study yourself and translate the steps.
You'll have a description/idea of what needs to happen from your manual exploration of the problem. It might not be immediately obvious how that is implemented in a particular language, but describing the action you want in the google search field tends to turn up options - just make sure that the language operation you choose matches the one that you have in mind. For example if you mean to remove the middle element of an array, it would be a really bad idea to re-build the whole array just to get it gone - in your mind you probably used a different data structure that supports that operation with much less work. If you can represent some data structure in your mind or on paper then you can probably represent that same data structure in code.
How do you forfeit a kata? I was hoping to do this but was feared that if I simply skipped, I wouldn't see a solution to understand how to approach. I do usually google parts as I go with my own projects the try translate the docs afterwards to try better understand. Im all up for skipping Katas that are beyond my skill level but really wanted to know the solution to this one.
Right next to
SKIP
button there should be anUNLOCK SOLUTIONS
button. I think you should advance 1 kyu more your current level to be able to do that tho.This comment has been hidden.
Are you running the same version of F# (CW is using v4.1, a bit outdated now) on your PC?
This comment has been hidden.
Groovy Translation submitted.
Would be nice if someone could review it
This comment has been hidden.
Hi! You need use array from block "Simple test"
My code passed all of the tests when I test against sample tests, but a test case failed when I attempt to pass the full test suite. How can I know which one?
Printing the input: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-input
Also read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#how-do-i-post-to-a-kata-discourse
This was a
question
, not a kataissue
:how is Test.assert_equals(find_even_index([20,10,30,10,10,15,35]),3) a valid test? "20" and "35" are not the same thing.
20 + 10 + 30 = 10 + 15 + 35
I am new here, so I hope I am going about this the right way.
My code passes all but one of the tests. It fails the test on the list: [-3, 2, 1, 0]. Since the list is small, we can look at it and know that our functions should return 3.
On my machine, my code returns 3. But it fails the test: the test says my function returns 2 and that it should actually return -1.
Is anyone else having a similar issue? What can be done about this? Perhaps I am missing something?
Magic!
Show us your code with markdown formatting.
This comment has been hidden.
I'm sorry. I have misread the feedback. The error is with another list.
Sorry to waste your time!
You are not correctly reading the logs, and it might happen that
i
of the for loop would go out of index range. The range should be[0, lengthArray)
not[0, lengthArray]
XRFXLP: thank you for taking the time to resolve my problem :)
How can I see all basic tests?
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#print-input
this test is buggy. It worked on my local machine:
find_even_index(list(range(1,100))) # error = 1 should be equal to -1
And why is it buggy? The test has an expected result and it is ok. Your code could be wrong. Hard to know without seeing it. Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
embedded tests looks buggy. it passes all tests on local machine
This comment has been hidden.
We also cannot find the reason why your code does not work for test #4.
However, you have the option to post your code with proper code markdown, (don't forget to use the spoiler tag), and then we will find the reason.
This comment has been hidden.
{} is NULL and NULL is of type 'None', whereas 0 is of the type 'int'. I am getting an error in Testcase 5
This comment has been hidden.
Start by reading this.
This comment has been hidden.
All of it is important, but specially the part about printing the input, so you can see which input your code fails and hopefully figure out why. The problem is in the 6th line of your code.
This comment has been hidden.
Is this the only test you failed? Because if so the above is a random test so just try again.
No, that's not a random test, you're printing the input of several tests together, add a new line when printing the input
See?
rge123, yap the only one. It's in GenericTests.
MdmaSteel Have you read what I wrote?
Chrono79, yes i did, thank you for that it become more clear, but i still confused, because i can't see where i should have 0. [8 8] => should be -1 [8 0] => should be -1
[8 0] => should be 0
Read the "Last one:" part of the description.
Chrono79, thank you!
Is there a perfomance version of this somewhere?
Hi! I'm having an issue where I can't pass one of the tests. I receive this input: [-3, 2, 1, 0]. My script tells me the answer should be the 3 index, an I agree, but it still gets marked as an error. I think it's because the solution doesn't count empty arrays to actually have a value, but in one of the examples they say so but with index 0. Am I wrong here? Is there somewhere in the instructions where they explicitly state this case, or am I missing somo concept here?
What's the error message? The test in Python (you didn't mention the language either) expects 3. Read this
This comment has been hidden.
That test is ok, you're failing the previous test, the log appears above the test result. Check how to use markdown formatting when posting code, otherwise the indentation is removed. Also, mark your post as having spoiler content when posting code like that. All of these is in the Troubleshooting.
Oh, ok. Thanks! Just another thing. Do you know where can I read about markdown formatting?
https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#how-do-i-post-to-a-kata-discourse
Thanks!
This comment has been hidden.
Please don't post solutions in Discourse.
This comment has been hidden.
Read this
please can i have solution to this question i've benn trying it all day long
If you return in both if and else inside a loop, your loop will run only once.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
not an issue. Just read the description, it's the example detailed there... x/
this data (i.e the list) is passed to function to check the validation of our model. The test case intends to answer 0 index in case of [20,10,-80,10,10,15,35]. how this could output index zero. Its not a valid test case because sum the right side of index zero (sum from index 1 to last is zero which is not equal to 20). That is why my code didn't pass the validation test
Ok, so... I tell you you're basically not reading the description / not paying attention to what's explained in there, and your answer is to downvote my comment... Fair enough, isn't it?
I should have answered with just "RTFM" then, because everything is explained in there. You're in the usual beginner situation: you read the description, your brain is too much crowded with different notions because coding is not natural to you yet, then you keep only a truncated version of the requirements in mind, and you wonder why what you think doesn't match the tests? Just because what you have in mind is not what you're asked for.
Lemme spell it for you:
Let's reorganize it a bit, now...
Oh, wait, there is another example...:
Mmmmh... Look how it suddenly makes perfect sense when you're actually reading the manual... xp
Next time an user tells you somehting like this, take at least the time to ask yourself if you're not doing garbage reasonning before jumping on the downvote button...
Thankyou bro. Sorry for this as you specified I was just rashing to solve the problem and was not looking at the problem carefully. After reading your second comment just got the idea what I was doing wrong.Once again sorry for that.
np. ;)
This comment has been hidden.
This comment has been hidden.
Okay I got it now so basically this problem tells (this was not explained in the description but according to the sample test cases) that there is a DMZ like north and south korea between left side and right side. I should've seen the pattern. Thank me later my fellow coders.
This comment has been hidden.
'looks like u solved it'
This comment has been hidden.
You can always print the input. One of the tests is
{8}
.The tests have been updated since then and those solutions should have been invalidated, but invalidation doesn't always work correctly.
15 + 10 + 10 + -80 + 10 + 20 == -15
This comment has been hidden.
writing in python, this test is going on:
Testing for [-15, 5, 11, 17, 19, -17, 20, -6, 17, -17, 19, 16, -15, -6, 20, 17]
and it returns to me:
It should work for random inputs too: -1 should equal 8
i did, also calculated myself, got the same result as my program, but test somehow thinks index 8 (17) is the right answer.. o.O
p.s. this aint only one failing like this
EDIT: i edited my program a bit, and it eventually passes, but on some occasions it would fail. passes like every 4th or 5th time. dunno what is going on, but nice work, that was interesting!
Your code is wrong, there should be more fixed tests (like the one your code didn't pass) to catch that.
Test added.
I failed a basic test (I passed 54/55 tests) but can't see what values were used as it seems to an 'extra basic test'. The random tests and the initial 10 basic tests give their test values. Any idea how to get this info? Thanks
i have the same probleam with you!
Print the input
This comment has been hidden.
There is no problem with the tests, the error message is only showing the first failed test, look at the sample tests, the first one expects 3, the second 1, that's why you get that.
This comment has been hidden.
Not an issue. If your solution doesn't pass the tests, it's wrong.
index error when using python 3.6 in testcase with Range()
switching to python 2 seems to remove it
Wrapping the range with a list seems to work. Maybe that should be done in the tests directly.
Fixed.
This comment has been hidden.
try printing the inputs to see which tests cause your code to fail
That was a lifechanging tip. Turns out i hadn't coppied correctly the code from my testing enviroment.
excellent! yeah, be cautious with foreign environments and happy codng
cool kata :)
When I'm trying to use and learn from nice JS best practice solution from "okeydoke80, wrousse, cucubau3000, eriksudd, qu3rn, yy123 (plus 32 more warriors)" it can't pass the test from description for the array {20,10,-80,10,10,15,35}. Q: Why? Am I doing smth wrong? My solution passes all but it's quite straightforward...
That solution is incorrect, but solution revalidation after test changes isn't exactly reliable on CW, so the solution isn't marked as invalid.
Julia translation
This comment has been hidden.
This comment has been hidden.
This is array that is run in tests: 1,2,3,4,3,2,1,1,100,50,-51,1,1,1,2,3,4,5,6,20,10,30,10,10,15,35,-8505,-5130,1926,-9026,2824,1774,-1490,-9084,-9696,23094,4,5,6,7,8,9,10,9,8,7,6,5,4,8,8,8,0,0,8 Somehow expected value is 1 when sum of it is -12924 so even if I'm at index 1 the amount on the right side of it is -12923 and it is not equal 1. Please fix it.
That's not the input of a single test, you're seeing the input of several tests there. The tests are ok in Java:
See?
Of course... how didn't I see this. Had a brain fart I guess ;).
Thanks!
Part of what makes this Kata's instructions confusing is the vague use of the word "position" combined with its reference to the "index" as if it were an ordinal, e.g., "the first position."
How does this make sense?
Expected: 1, instead got: 0 Log: [7, 3, -3]
This contradicts the last example given in the details.
The log appears above the test result, you're confusing the error message of one test with the input of the next one.
Ahhhh thank you! One of those days I guess.
This comment has been hidden.
Your code is not returning that. You can see that with this input value:
[1, 0, 0, 0, 1]
'Looks like you've solved it'
I completely missed the fact that N is to be excluded when I read the description. Could you highlight it a bit more ? For example like this:
"[...] integers to the left of N is equal to the sum of the integers to the right of N, excluding N. [...]"
When I tried to run the haskell unit tests I got:
The name of the test module must end with 'Spec'
Fixed. (You may need to reset the sample tests.)
This comment has been hidden.
The issue was in return. If .find() return 0 OR statement switch to -1.
This comment has been hidden.
Try reading the posts below, razorpl's one for example.
thanks! I got it!
[] 20 [10, -80, 10, 10, 15, 35] -> left_sum = 0, right_sum = 0, index_value = 20, index=1 [10, -80, 10, 10, 15, 35] 20 [] -> left_sum = 0, right_sum = 0, index_value = 20, index=6
how does {20,10,-80,10,10,15,35} have expected result is 0?
Left sum of index 0 is equal to 0 (nothing there). Right sum of index 0 is 10 + -80 + 10 + 10 + 15 + 35 = 0. Therefore the expected result is at index 0 the left sum is equal to the right sum.
This comment has been hidden.
Thank you. I see
There's no consideration/tests for overflow arithmetic. Particularly useful in C/C++ where AFAIK signed integer overflows lead to undefined behaviour.
This comment has been hidden.
0
is the identity element for addition, so it's logical.This comment has been hidden.
Tests with a single element (and maybe with 2) should be present in all languages.
Done. There actually were quite a few invalid solutions (for example, about 30% for Kotlin).
What is true for [1]? 0 or -1?
This comment has been hidden.
This comment has been hidden.
Python 3 should be enabled.
Done.
@Unnamed Could you check learner1205's post and see if you should wrap the range with list in the tests? Or is it a problem of his/her code?
The tests should be updated, using lists and ranges leads to an inconsistency in the passed data types.
Fixed. @Chrono79 You know there are no @-mentions on CW, right?..
Force of habit, it would be nice if it worked.