8 kyu
Sum of positive
131,058 of 322,347JbPasquier
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.
This comment has been hidden.
This comment has been hidden.
Read this: https://docs.codewars.com/training/training-example
And that code doesn't do what the kata asks.
Thank you, I guess, I got it.
The description says: "[...] return the sum of all of the positive [numbers]". Do you know what sum is? Do you know what return means?
Remember that you can also visit Codewars Discord for help!
This comment has been hidden.
This comment has been hidden.
Your code fails this test:
Mark your post as having spoiler content next time, please.
This comment has been hidden.
Please do not post your solutions in kata discourses. It's not necessary, and they are spoilers.
This comment has been hidden.
Do not post solution in discourse. Also, not a kata suggestion
Remember begining
This comment has been hidden.
This comment has been hidden.
Don't post solution in Discourse, please.
This comment has been hidden.
Random tests for Scala
This will invalidate all solutions since I moved the function outside of the unneeded object. If anyone knows how to implement backwards compat with what we have on CW I'm more than happy to do so but in the absence of that I'll go for correctness over maintaining 8kyu solutions.
Approved
This comment has been hidden.
Learned about list comprehension with this. Thanks!
I am wondering if there is anybody who can look over the code that I have? I've been testing it on an online replit js compiler and it seems to work as the function asks. However, everytime I make an attempt it fails.
Maybe it's because I'm using the "..." rest operator?
Sorry, I am very new to this and don't have anybody I can ask inrl.
I didn't want to post my code publically to avoid spoilers.
read the test output, read the example test code, use console.log
Unfortunately, none of that seems to be of any help. :(
All I get from the test output is "expected +0 to equal 15." Which I am assuming is saying that the first test does not equal 15. It seems to work in my replit compiler though.
This comment has been hidden.
@Doperoni
when I try clicking on the
View Solution
button, all I saw was this:Ah, I wonder if it is any different now as I made an attempt with the code I was using prior.
Sorry, I'm still figuring out how this works!
put
before the
if
statement in your for loopWhy is it that it keeps telling me arr is undefined? Shouldn't arr be defined in the codewars import when the test is run?
Your code is wrong, not a kata issue, what is
ea
?This is the relevant part of the error message:
I was really confused by the return 0. I was trying to work my code, that it would end with return zero. I realized by trying that it needs to be ignored. But why is it there in the first place?
Suppose the given list of numbers doesn't contain any positive numbers.
There is nothing to sum up, but a return value still needs to be specified.
The mathematical sum operator ∑ is defined to result in 0 if there is nothing to sum up, but not everyone knows that. Some definitions of a sum even have the sum of nothing be undefined.
hey
This comment has been hidden.
why do you have spread arr in parameeters?, it should
function positiveSum(arr) {
right?C#:
using System.Linq;
should be removed from initial code.Fixed
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
If your code is rejected, it's because your code doesn't do what it is asked. It's as simple as that.
Why does codewars gives us a preset of code that we should use instead of us choosing our method?
See https://docs.codewars.com/training/training-example.
This comment has been hidden.
The
ISSUE
label is for provable kata issues. Please use theQUESTION
label when asking a question and appropriate formatting when posting code. See https://docs.codewars.com/training/troubleshooting#post-discourse.You don't need to do IO on codewars. See https://docs.codewars.com/training/troubleshooting/#.print-vs-return and https://docs.codewars.com/training/training-example/#writing-a-solution.
This comment has been hidden.
This comment has been hidden.
is 0 included?
Does this matter in any way?
This comment has been hidden.
This comment has been hidden.
Thank you so much!! I understand it now ;)
The 0 part is where I got stuck initially, the positive vs negative isn't the bug. It's figuring what to do when the loop incounters the 0.
This comment has been hidden.
it DOES tell you something, you just have to look for it
error: cannot find symbol IntStream stream = Arrays.
This comment has been hidden.
It wasn't easy, I had to torture myself. Good kata
how do i make it return only the sum and not each step of the sum if an array lets say is [1,2,3,4,5] my code returns 1 3 6 10 15 please help me solve this issue
This comment has been hidden.
I think you can represent python code in js :)
OP solved it, closing
This comment has been hidden.
Not a kata suggestion. Don't post solutions on kata discourse and read this.
In this exercise I solve it, I gave it a test, I did the test and it was approved, I later gave it an attempt to finish the challenge but right there I get an error (I'm new to this kata thing).
This comment has been hidden.
kata solution != kata suggestion!
Also, do not post solution in discourse and please flag it with spoiler if you want to disclose anything related to revealing solutions directly or indirectly
please help to understand, i can't find information why we must initialize var sum = 0; why code can't work just like var sum
This comment has been hidden.
This comment has been hidden.
Hi and welcome to Codewars - please don't post spoilers in Discourse (your code is visible to everyone, including people who have not solved it). If you want to discuss code, you can do it the the Solutions page - or you must use a Spoiler tag.
This comment has been hidden.
Golang 2022/10/11 19:23:07 [1 -2 3 4 5] Test Failed Log
Expected : 28 to equal : 13
Ok..? What's the question here?
See https://docs.codewars.com/training/troubleshooting
This comment has been hidden.
all my TDDs pass using console.log, but using return i get this error? Testing for fixed tests expected 28 to equal 13
Read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
This comment has been hidden.
RISC-V translation Kumited - please accept :-D
Approved
Missing edge case test, the value at the pointer happens to be zero: https://www.codewars.com/kumite/62fd17a797ef0a5ad4dd1c68?sel=62fd18a126e44ca851971105
Added the edge case, please review and approve :-)
D translation
Approved!
Good Kata!
No way this was 8
This comment has been hidden.
nah just 8
This comment has been hidden.
It does. Your solution passes all the tests.
This comment has been hidden.
This is definitely not 8 kyu
This comment has been hidden.
Your loop ending condition is wrong.
thanks, figured it out
This is 7 kyu, 8 kyu is even more basic.
This comment has been hidden.
multiverse
COBOL translation (author is inactive).
approved
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
i understand but what it will show?
This comment has been hidden.
This comment has been hidden.
OP solved it, closing
This comment has been hidden.
Read this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce#parameters the error message tells you what's wrong too.
OP solved it closing!
Missing test cases for empty array in C, NASM. Missing such fixed tests in several language
One of the first Kata I've done in Haskell. I really enjoyed it.
Good, but I have done 98 kata and I didn't know how to do it! (I do this in Python)
This comment has been hidden.
I think that's a problem on how you calculate
len
.As we say, keep it simple... Thank you
Hi @ThomasBucaioni also note that almost 5,000 coders have solved the C++ version, so the chances of such an extant bug is highly unlikely.
Good kata!
This comment has been hidden.
This comment has been hidden.
R translation (author inactive)
Thanks :)
This comment has been hidden.
Your function always returns 0.
What a numpty! Thank you
I might bee too new to programming but this KATA was a bit too hard for a 8Kyu KATA, I already had a grasp of how arrays work and how to create and call elements from them to list them, but never had even been introduced to operations with elemts withing an array let alone filtering elements to then operate with them within a function.
If you've been mentored, are an experienced programmer in other languages, or have a teacher I can see it being easy for you but as a self learning begginer programmer, this is a big jump from say, creating multiplicative or exponential methods to this, this calls for a bit more advanced syntax knowledge than the basics.
Still great KATA, made me give up and search for the solution to later find an explanation online, I am very grateful for the new knowledge.
Kind Regards.
I am self learner also but I am Three years old in programming now Manipulation with lists or any kind of data in all ways is pretty important also Problem solving is a great skill to work on and improve don't give up, it won't be easy but I see you made great job until now
Each kyu level must have coverage on all topics:
strings
,algorithm
,mathematics
and alsoarrays / list
. Conduct more self-research and learning on the topic, then you will find it a breeze. Also, reranking old katas is not possible due to DB setup, not to mention this kata has gained 271K solves already, which means most (except the 30 upvotes on your comment) solvers agree with the ranking !Nice Kata, thanks!
also I catching index out of range error while compiling Go code in browser. But when I try to build application in my native console all is OK. What's going on ? If that my code is ok and returns correct values.
There must be something bad in your code. This kata has been solved more than 2000 times in this language, I've just run my (very basic) code and had no problem. Please be more specific, give example of failed tests, with logs and printed stuff, and post a question rather than an issue.
This comment has been hidden.
"Working" != "correct". Not an issue.
that's 8kata bro, it can't be difficult, i have good solution, and it 100% correct on another(mingv) compiler
second case should be 11 insted of 13
No, read the description again, the tests are fine for both C and Java.
Clojure Translation ready for review
.
Thanks for this Kata
I am pretty sure my code is valid, but it says no module named codewars_test. Please fix this issue.
Please try again.
thank you. That solved it.
For OCaml the tests are uncorrects, indeed, for input [1;-2;3;4;5] the result expected is 13 which that is wrong it's should be 11.
13 is correct.
Factor translation
Approved
I've forked the Rust translation in order to update it to Rust 1.49 and updated
rand::Rng::gen_range
to its new signature that takes astd::ops::Range
.Good Kata
This comment has been hidden.
This comment has been hidden.
good kata for beginners
This was fun!
Very nice kata for beginners
This comment has been hidden.
'Looks like you've solved it'
This comment has been hidden.
When I copy my code to VS code and test it in a browser for every test array it works great, but when I sumit the same code here it turns only one good the rest is error. Why?
What's the error message you're getting?
Read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
There are more than 31k solutions in javascript, it's a problem with your code. Are you using some global var maybe?
Thank you for the link, I will go through their solutions of the issue. Output of the code is the same for each test array, if I put first test array than the result for all test array is the first one, if I put third array the reult for all is the third one, and that's why only one test comes back as correct.
This comment has been hidden.
See if you haven't edited the sample tests (in the original your function is never called with 2 arguments). Also, don't use global vars like that, they keep their value between tests.
Move your post_list[0] just below the function def and you should have it. make sure to indent correctly.
Thank you both, it works with your suggestions :)
C# tests with wrong values
Nope.
This comment has been hidden.
Use the initial code. With your code you're not even creating a function. Click reset, start over.
I practise in NASM.How to know which register has input and what it's representation?
For NASM, Codewars compiles your solution to 64bit machine code, what means it uses System V AMD64 ABI calling convention.
; rdi = array (pointer) ; esi = size (unsigned int)
I'm having real issues having Codewars accept my code that I know is right. Keeps giving me redundant errors.
since you passed this kata, can you give an example of a redundant error?
Hi, I picked C# but when I opened the challenge it changed the language. I forget what language it changed to. So I knew the error was incorrect and figured out it keeps changing my language to something else when I click on and go into a challenge. Thank you for responding so quickly! I spent way to much time trying to figure out why my code, that I knew was right, wasn't working.
alright, best of luck!
Reached the end of the file while parsing
This comment has been hidden.
General guidelines
Regarding your question:
return
the result.Thank you iaeliyen im new to code wars
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Not an issue. There's a bug in your solution.
Your function has to return the value, not print it.
This comment has been hidden.
You're missing an important part ;)
just solved it what a relief! Thank you!
so far so good :/
SQL Translation added
Submitting a translation to this Kata.
Lua translation
Please review and approve
soooo good task for understanding enumeration of arrays
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Scala has no random tests. JS probably doesn't have enough.
added for JS (updated to Node 12 as well)
Done after 4 years :-)
Dart translation Reason translation
Racket translation
Approved
This comment has been hidden.
Read this and why don't you solve it in a simpler way and then try using higher-order methods?
Because I solved 2 problems in the morning went to a level 7 KYU and though i was all that. Oh well.
I will try to solve this for then next 15 mintutes then take your suggestion and do it with a simpler way (for loop)
This comment has been hidden.
Print the input and you'll see what's wrong.
When you go throught "sample tests" and get status final submit, button become green and wait your last click. But there is some little bug of layout, green button of submit go down of screen. it's global bug i think. two times get from two kata's.
chrome google Version 75.0.3770.80 (Official Build) (64-bit)
https://github.com/Codewars/codewars.com/issues/1214 Not related to a specific kata.
thanks!
Issue with Ruby. It says in the description to return 0 if there is nothing to sum. One test case fails for me because it says it expects nil to return when there is nothing to sum so I am unable to complete this.
Sorry I must mention, submitting it as final works, but the test cases fail.
The reference solution was wrong. Fixed it, try again.
Please review the Rust translation
Swift translation added :)
Try to delete the comment with the solution in the translation discussion, you're giving it away ;)
Ok, so I sorted that out now. Sorry about that, I wasn't aware that it was visible to all and sundry...
Comments inside kumites are not visible to people who have not solved the kata.Turns out this is true in some cases, but comments under translations are visible to everybody...This comment has been hidden.
Not a kata issue, check why you're using rest operator there and please don't mutate the input, it'll pass here anyway, but it's a very bad practice.
Thanks for sharing!
One more Kotlin Translation
This comment has been hidden.
This comment has been hidden.
Given you're just starting, it's not that bad. See other solutions and try looking for higher order methods on MDN Docs. Even without using higher order methods, try refactoring your code to make it smaller (note that you go through the array twice (and once more for the other array) and it can be done in a single pass). You could also write this as a comment of your solution (remember always to mark it as having spoiler content). Good luck.
Thanks for the feedback. I can see that people have been able to write this in just one or two lines. And, I will work on eliminating the redundency. Cheers.
No problem, and I almost forgot, check this to properly format your code in the posts.
Crystal translation kumited :)
PowerShell translation Kumited! :)
This comment has been hidden.
That for loop looks incomplete. Also look where you're returning sum.
This comment has been hidden.
The assertion error is because the returned value is not the expected one.
That's because of where you're returning sum (as I've already said ;) ).
This comment has been hidden.
vectors are zero-indexed, look at your loop closely ;)
How does an vector work? Please explain it to me or give me an link... Thanks
Googling for this kind of thing is a skill you should try to get at some point. Try here
Nice Kata.
in the go language version, submitting works fine, but testing against the examples won't run because the test case code has unused imports.
Fixed the sample tests and added random tests.
I have a working solution that passess all the tests, but after attempting to finish it, it looks like only one of them is checked (and it passes), because it shows that I have passed 1 test and failed 0. Is this a server issue or something's wrong on my side?
Which language?
CPP
Actual tests seem to work properly, I think your code has bugs ;-) Please post your code here marked as spoilers and formatted like this:
``` code code code ```
This comment has been hidden.
Your code returns early with error because program aborted with exit code 139 i.e segmentation fault for the empty vector test.
Hint: That's not how you get the length of a vector ;-)
Thanks a lot :)
This comment has been hidden.
goog
Good little kata had me think, didn't get the one line solution though.
This comment has been hidden.
Elixir translation added:)
Approved
This comment has been hidden.
You should return the answer
my bad, thanks ;)
Numbers don't mean integers, actually ;)
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Fun kata
#beef_the_world_behind
Thanks for the nice kata!
This comment has been hidden.
In php, array length is found with
count(array)
. Also, you are missing the $ in front ofi
in a number of places.This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
PHP Translation Kumited - please accept :D
Approved, thanks. :-)
The service is always time out!!! Making me crazy!
Mine too!!! For pretty much everything!
This comment has been hidden.
This comment has been hidden.
Thanks for your help ! Much appreciated !
TypeScript-Translation kumited!
https://www.codewars.com/kumite/5814e7cba2f1563c67000068
Please check and approve it! ;-)
Approved, thanks. :-)
C translation kumited! :) Please approve if all's good.
Awesome, thank you.
When I submit my Haskell solution, I get the error: /tmp/haskell116514-14-1al9efz/Codewars/Arrays/Test.hs:3:8: Could not find module `Codewars.Arrays' Use -v to see a list of the files searched for. anyone know whats up?
Your test cases should have null values in some of the elements. Also something to make this one harder would be a case that overflows the max int in javascript.
No need to be harder.
Perhaps a tag referencing arrays. Description is clear, clear examples provided. Good work!
JS version need some random tests
There are.
CoffeeScript, Haskell, and C# translations kumited.
Awesome, thanks!