8 kyu
Grasshopper - Summation
102,390 of 282,420danleavitt0
Loading description...
Fundamentals
Mathematics
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.
why its not specified in code to keep num inclusive??
Can't you see it in those two examples?
Added that remark
Thanks, Gauss!
Можно мне на собес такую задачку плиз
This comment has been hidden.
This comment has been hidden.
please, asking for help is not a kata issue, ask a question instead and format your code correctly: https://docs.codewars.com/training/troubleshooting/#post-discourse
your line
num - 1
has no effect. it computesnum - 1
and then discards the result, this is why your code never ends, sincenum
never changes.num = num - 1
akanum -= 1
would ensure thatnum
is decrementedHello how to do this is question
This comment has been hidden.
This comment has been hidden.
This message indicates that that your function returned 37677 when it should have returned 37675.
i don't think so buddy, cause if you run my function for value of 274 you'll see the output as 37675
Yes, when you run only one test, but because you use a global var and it keeps its value between tests, all tests after the first one come up with a wrong answer, read this: https://docs.codewars.com/training/troubleshooting#works-but-no
thnx bud
This comment has been hidden.
See the initial code:
And read the error message:
Don't change the function's name. The tests look for
summation
, not forsumUp
.I get an error by creating an empty array (like const arr = [])
咋才能把时间减短
用的for循环
The text of output is wrong.
No idea what you meant there.
Closing.
36 (1+2+3+4+5+6+7+8)
The result is only the first number. The content between parentheses is confusing and is wrong.
Read what I quoted there again.
If this is an 8 kyu I am sol. This is definitely a 5 or 6 kyu. As a side note, how can I get better at solving problems like this? My math skill is awful.
You don't really need math skills, this is a simple formula. You can always google this type of formulas, no need to memorize all of them.
so, im 9kyu hahahaha!
GUYSSS! If you learn about Euler summation in school then this is a walk in the park!!!
Maths makes computer science so much easier :)
Hey guys so I ran into a small issue I wrote my sollution but the answer to one of the tests is wrong it says ''expected 3 to equal 36'' which is not correct.
Your code is wrong, it always returns 3 for any number greater than 1. Not a kata issue. Please read this: https://docs.codewars.com/training/troubleshooting
Hello. You misunderstood what that message says. It says that your returned value 3 is not equal to expected value 36 for some unknown input (actually, that input is
n = 8
).Having a quick look at your code, your function always returns in the first iteration of your loop (when
i = 1
), which is not correct. 1+2+3...+8 is not equal to 3.Have a look at this article for some general tips: https://docs.codewars.com/training/troubleshooting
Can someone help me with this problem. I can't figure out the answer, I did some tests, but it didn't pass. By now I don't know if my answers are write or not, cause I tried two diferent methods and got the same result. "returned 2, expected 3"
The
issue
tag is for when the kata itself needs to be modified. This isn't that.Make sure you read the instructions carefully. You're doing something completely different from what they're asking.
The description is very ambiguous, especially the Input -> Output code block.
I finally solved a 5 or 6 kyu that was worth 8 kyu hahahaha
My answer was:
But don't pass the test, just need the total of the sum LOL.
The fixed description is:
Summation (Adition)
Write a program that finds the summation (adition) of every number from 1 to number.
For example (Input -> Output):
Description changed, not to what you wrote, but I think it addresses the problem.
can someone help me with the summation kata but not give me the full answer plz
This comment has been hidden.
I agree with all the people saying the description needs updating to be more clear.
This comment has been hidden.
Not sure why this does not work, thankful for any replies.
Not a kata issue. See https://docs.codewars.com/training/troubleshooting/#post-discourse
Why does the Rust version use
i32
s when the instructions specify that the input will always be positive?I agree. And an upper limit should be specified otherwise it's possible to have an overflow with valid input.
This comment has been hidden.
Fantastic Kata
This comment has been hidden.
This comment has been hidden.
finally solved
Hi @sjagga13, do not post code outright (or discuss solution methods) without using the spoiler tag. (I have added the tag to your comment).
Also, do not post as an
issue
unless you can demonstrate that there is some flaw with the kata itself, and be able to give examples as evidence. Otherwise post as aquestion
.Enjoy.
Actual and expected are swapped in PHP
fixed
Keep grinding guys...
I have no idea HOW to write a program, that's why I'm on this site. If this is 8Kyu is there a 9 for people with absolutely no coding experience?
no 9 kyu... you just need to study basic methods / techniques / syntax for your language with tutorials or other resources outside of a site such as this before expecting to be able to solve challenges. meanwhile, you could attempt some of the very easiest 8kyus and do some additional research while trying to solve them. anyway, always be sure to pay close attention to the error messages. you may get additional clues or concepts from reading the comments. don't give up. best of luck.
Thank you. I didn't think of it like that, so this is not necesarily a jump-start to learn coding but a bridge between just barely knowing the concepts and appling them practically. I would like to ask though if you don't mind. Do you know of any places, resources, websites, ect, where I could learn those methods and techniques to work on my syntax? If not, I understand thank you for your help. I kinda thought this would be where I would start.
Edit: I look forward to eventually moving on up to using this site.
w3school for most languages eg https://www.w3schools.com/php/default.asp Walks you through the basic syntax
dont give up, i thought the same thing when i first started.
I'd reccomend codecademy to get started learning the basics! A lot of free courses, pretty interactive and great for actual beginners
What language are you trying to learn?
This kata is a subject to deduplication process here: https://github.com/codewars/content-issues/issues/188.
Please join the discussion to help us identify duplicate kata and retire them.
This kata was decided to stay.
Please change the description it is not very clear. NEW DECRIPTION Please find the sum of the all the numbers from 0 to the entered number. For example: User enters: 8 Summation Process: 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 Displayed Result: 36
This comment has been hidden.
You put the "list = []" outside the function, try put it inside it will work
It is wrong, not a kata issue, read this: https://docs.codewars.com/training/troubleshooting
Also mark your post as having spoiler content next time.
@daichi31 hmm, interesting...
Brings back memories.
Traceback (most recent call last): File "/workspace/default/tests.py", line 2, in from solution import summation File "/workspace/default/solution.py", line 10, in num = input("please enter a number: ") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ EOFError: EOF when reading a line
Is anyone can tell me why??
Don't use
input()
.See https://docs.codewars.com/training/training-example for better explanation.
You must code only "def", + call def .
What's wrong with my decision? I wrote the formula correctly
Your code is wrong, not a kata issue. Please read this: https://docs.codewars.com/training/troubleshooting
Description should be language-agnostic
fixed
netu number
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please use spoiler flag next time.
lol I need to try to remember that there's always an easier way of doing these math focused katas.
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )Hmm, i have the correct output but still give me the error message : none should give i have the good result and the string are correct i don't understand how to validate that Kata
It seems you passed the kata, probably you were printing instead of returning the result.
This comment has been hidden.
This comment has been hidden.
Definitly this kata should've been rated as 7kyu
Very easy and beginner friendly kata!:)
D Translation
Approved. Thanks ~
COBOL translation
Approved by
LearningFTW
This comment has been hidden.
This comment has been hidden.
public class GrassHopper {
}
why is there an error cannot find symbol?
extra
}
-- Actually, most solutions here seem to only consider positive integers, -- whereas summation should probably consider summation of negative numbers as well IMHO
The number will always be a positive integer greater than 0
What more do you need? Please...
My bad... Is there a way to delete my wrong post?
No, since you've been answered.
This is my solution
Now, if I put 'Dim number As Integer' before the function why the result is 37 in the second test case?
Please say which language you're using, there are several tens of them.
OP solved it, closing
This comment has been hidden.
What do you return at the end? You need to make a sum, that's all (summation of every number from 1 to num isn't it clear enough?). Also, please use markdown tags to format your code or it's very discomfortable to read, see: how-to-format-code-in-markdown. Indentation is fundamental in Python, we can't even know if your code is syntactically correct.
This comment has been hidden.
Good for you, but don't post solution in the discourse page, the solution page is already there.
سلام من در کدوارز تازه وارد هستم. آیا کسی فارسی زبان هست تا کدها را به اشتراگ بگذاریم ؟
This comment has been hidden.
Not a suggestion.
Just for giggles I did it twice, first for my simple solution, then did it using recursion just to see how it would take it. Worked perfectly fine although the second method is slower because well, recursion.
This comment has been hidden.
You need to return the vlaue
totalsum
, right now you are just printing it to the output screenThis comment has been hidden.
This comment has been hidden.
Well you can always start from some math-related problems or use the kata search bar to start from the easiest lvl (8kyu)
CommonLisp: the initial solution exports
multiply
instead ofsummation
.Edit: approval pending
Pascal Translation ready for review.
Approved.
Raku Translation ready for review.
Perl Translation ready for review.
CommonLisp Translation ready for review.
Approved
OCaml Translation ready for review.
Approved
Haxe Translation ready for review.
Approved.
This comment has been hidden.
If you're talking about python, python2's xrange was renamed to range in python3. (and you shouldn't be using python2 pretty much anywhere)
thank you! thats what I was taught.... but feared xrange was NEW lol
Use your middle school math hint: sum of natural numbers upto n?
This comment has been hidden.
Please read this paragraph: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#how-do-i-post-to-a-kata-discourse
You might also want to study other solutions to see how to solve this kata in a proper, efficient way.
please i need help i solve a question but i don't know how to run check the answer in codewars
What have you tried? Hit train, finish given function, hit attempt.
This comment has been hidden.
Read the error message, you forgot the
:
and please mark your post as having spoiler content next time.Thank you! And yes, will do.
This comment has been hidden.
This is why we have code review, sometimes I write unnecessary 'complex' code..
File "/runner/frameworks/python/cw-2.py", line 111, in wrapper func() File "main.py", line 5, in basic_tssts test.assert_equals(summation(1), 1) File "/home/codewarrior/solution.py", line 2, in summation for n in (num): TypeError: 'int' object is not iterable
what does this actually mean and how do I fix it
It's right there:
Iterables are strings, lists, some other kind of data, but not ints.
This comment has been hidden.
First off, you can change it into a
str
. Secondly, you don't even need the if statement because 0 + n = n. Thirdly, Your declaring num_list every time you loop, and each copy is changed back to[]
.Declare numlist outside of the for loop. Also, it is append(), not append[].You don't need a list at all, there are more ways to solve the kata (even using a list, but there are easier ways), you're overcomplicating yourself.
This comment has been hidden.
the keyword
pass
is a placeholder for your code. basically the code would do nothing, so you write in the code, includingreturn
. also: posting an ISSUE is only for when there is a problem that you can identify in the kata.This comment has been hidden.
Kata hint != kata suggestion ~~
This comment has been hidden.
return the value, not print
whats the difference between the two
This comment has been hidden.
Print does exactly what it says it prints the value. But you use return when using a function because the kata wants to receive the output not for you to print the output. When you print the output the kata does not receive an output so it marks it wrong. WHen you use print you get 'None'.
This comment has been hidden.
return in the cycle
This comment has been hidden.
CFML, SQL, Shell, Erlang, Fortran, F#, Objective C, PowerShell, Prolog, Rust, Solidity, Swift, and VB translations.
All approved, with SQL closing!
Isn't this kata a duplicate of the Triangular Treasure kata?
Not quite. They're similar, but Triangular Treasure has the added challenge of input validation.
Huh, classic
introducing test cases that makes it impossible to submit solutions where the numbers between 1 and n are just added together, since the naive solution lies is O(n) compared to the optimal solution which lies in O(1)
Looks like a few translations allow for zero in the random tests, which doesn't match the kata description. Unfortunately they are languages that I don't know, so I don't feel comfortable fixing them:
I've fixed these:
Elixir is fine -
:rand.uniform(n)
generates a random number between1
andn
.I'm fixing them all, and will resolve this issue when done.
Nice one. Every day's a school day!
Fixed everything except Ruby (remember, I'm not 10k!...). To fix it: just add
+ 1
after therand(80)
call.Also, enabled TS 3.3, Python 3.6, and Haskell GHC 8.2.
Thanks Steffan. I've added that to the Ruby translation.
Here's a couple of upvotes to get you closer to 10k!
Oops! And I lost my version updates. (and the upvotes, don't worry, my honor goes up pretty fast. :D)
Go translation awaiting approval.
Factor translation
R translation Dart translation Reason translation
There were zeros in the random tests, which I noticed after approval; I fixed them in the kata.
[Racket Translation'(https://www.codewars.com/kumite/5d6bf18a64dabe001bc572f1?sel=5d6bf18a64dabe001bc572f1)
This comment has been hidden.
This comment has been hidden.
I have solved the kata on codeblocks but same code on here and it is not working, i think i am inputting the code wrong. Can someone help me ?
Julia translation
This comment has been hidden.
Fixed. Thank you for reporting this issue.
Yo smth wrong with tests it says 32 should equal 36. If you get the sum of all the numbers its 5xx
This comment has been hidden.
num
doesn't havelength
.num isn't an array i believe so you wouldn't use .length. Make sure to use <= so it cycles through all the way.
Python 3 should be enabled.
Python fork
This comment has been hidden.
This comment has been hidden.
Groovy Translation kumited! Plase accept :smile:
Kotlin Translation kumited! Plase accept :smile:
Scala translation available for review.
This comment has been hidden.
This comment has been hidden.
Haskell : https://www.codewars.com/kumite/5b623534a33c36abb1000071?sel=5b623534a33c36abb1000071
Added BF translation
Please review and aprove :D
https://www.codewars.com/kumite/5b2a848f9e40b919220001c3?sel=5b2a848f9e40b919220001c3
TypeScript translation submitted.
Can anyone approve this?
Random tests can generate tests where $n === 0.
What's wrong with this? The function should deal with input 0.
The number will always be a positive integer greater than 0.
Fixed.
This comment has been hidden.
This seems like a clever little mathy solution. How was this formula derived? Runs in O(1) time, rather than O(n) time of the iterative solution.
This comment has been hidden.
Good little kata that made me think for a minute.
I like this one, but I got an error because I didn't have an output for 0.
I suggest changing the last sentence 'The number will always be a positive integer greater than 0.' because you are testing for 0!!
This comment has been hidden.
It should describe every number FROM 1 to num, not "between". specifics are important. Fix this.
Done.
This comment has been hidden.
Actually return value should rather be long instead of int :)
Well, in PHP the tests seem to be buggy ?
Not anymore.
PHP version testThatSummationWorksForRandomTests sometimes getting error, accepted solution fails to pass tests
Failed asserting that 0 matches expected 1.
This comment has been hidden.
Not an issue.
There are no performance requirements in every language version ;-)
C translation kumited! :)
Anyone else having issues submitting this Kata?
Successfully completes all tests but never prompts to refactor and submit code, just runs the tests again and gives me results even when I click submit
That's not an issue.
Strange, can't get this working in C#. Put the same code into LINQPad and it compiled wonderfully. I'm just gonna skip this.
C# needs some initial test cases. Warriors at Kyu 8 can't be expected to know how to write their own.
Added
C++ Translation Kumited !
PHP Translation Kumited - please accept :D
The random tests generate 0, issue above.
Not anymore.
translated to coffeescript :)
This comment has been hidden.
Translated to Python - my first time translating / creating a problem so please let me know if there are any issues.