7 kyu
Functions of Integers on Cartesian Plane
92 of 3,129g964
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.
My first attempt was perfectly accurate... and way too slow. Thanks for compelling me to write faster code. I need that!
python new test framework
Approved
C#: method name should be
PascalCase
(Please refer to implementation of backward compatibility here )This comment has been hidden.
This comment has been hidden.
Basically, for number 5, you have:
sumin(5), which needs to add all these numbers:
1,2,3,4,5
1,2,3,4,4
1,2,3,3,3
1,2,2,2,2
1,1,1,1,1
sumax(5), which needs to add all these numbers:
5,5,5,5,5
4,4,4,4,5
3,3,3,4,5
2,2,3,4,5
1,2,3,4,5
sumsum(5), which needs to add sumin(5) with sumax(5)
That's all the description you need, folks.
How is this a spoiler? I just translated the description into plain English. But noooo, people should not understand the description.
The math was rather tricky for a 7 kyu
I think that there could be a little more explanation for how to the example table was created. At least a link to a resource for anyone that doesn’t know the math fundamentals. Also, I think there is a typo in the first line, “plan” should be “plane”.
Typo corrected.
Description and "sample table" is unclear/terrible/misleading
Violent opinion without any justification or explanation; that relieves a fit of bad temper but it is not constructive. From you I was used to better behavior.
I just tried to be concise.
Explanation:
x
andy
>= 1, so the zeros are unnecessary/confusingx
andy
, to be more clearcode
) to make them easier to distinguishx
andy
once, as it applies to all three functionsJustification:
(taken from the comments below)
Description modified according to your desiderata.
I leave the pleasure of these comments to you. At first I was hurt by them. Little by little I learned to get used to them; social networks are often much worse. Out of thousands of people there will always be the frustrated ones who haven't been able to solve the problem or who have solved it too slowly for their liking and who take revenge with sour (sometimes aggressive) comments.
Edit: I was delayed posting the description, sorry.
Seems much better! :-)
Cheers
Coding the solution was easy, the difficult part was figuring out what was meant by sumax.
Hint the table is a countdown version of the example table.
COBOL translation.
Approved.
.
Node 18 (
mocha
+chai
) should be enabled (Refer this and this for more info)Ruby 3.0 should be enabled (Refer this & this for more detail)
Ruby 3.0 enabled in this fork
The tests pass, but there is a 12000 millisecond timeout problem. I write in js.
I solved the problem. We need to get rid of nested loops. To do this, find a pattern in the table and write an algorithm with one cycle.
Julia: error when running sample tests:
Nim:
Both should be fixed. You are the best reviewer!!!
Thanks :)
I'm a big python fan, but am really starting to like racket. I just wish more people would give it a go! I'm watching the s.i.c.p videos, and just had to check it out!
One thing that helps me alot, is make a list of the first 5 or more solutions. Then make a list of deltas for that list, another list of deltas for the delta list, and so on till I get a list that doesn't change. This really helps me see the patterns.
very cool. I wish katas like this would have a rank of say 7 to get functionality, then maybe 6 for the aha moment whey you recognise the patterns, and do it without a nested loop. because the first job would be easy, you can start generating numbers . Then you see the first pattern... Then after a bit more work, the second one. Overall, I like these kind of pattern recognition ones.
Quick maphs
I didn't understand the description. Can someone explain this to me? Thank you!
Sumax - The tests are given 6 and 8 look at the difference
7th kyu? really? requeriments are so confusing.. im programmer not mathematician
Yea how can this be considered a 7th kyu? I agree.
I pass all test, but one problem is server timeout. maybe there are shortway of formula to pass this kata, somebody give me a hint please!!!
i am using javascript anyway
same but i am using python, have you overcome the timeout issue yet? I am looking for other ways to to solve this kata, instead of using double for loop but am stuck. Any hints are much appreciated.
the same problem on js
You need to find a mathematical way to solve this, not summing in loops.
I solved the problem. We need to get rid of nested loops. To do this, find a pattern in the table and write an algorithm with one cycle.
Hi, there! I can't figure out how to calculate the sumax(n)? If everything is clear with sumin (n), then at the expense of sumax (n) I did not understand the explanation
Please hint to me
In sumin(n), you choose the number that is smallest of x and y, in sumax(n) you choose the biggest.
Like this:
This comment has been hidden.
Thanks, the description is a nightmare.
This shouldn't be 7 kyu. Look at this problem: https://www.codewars.com/kata/5841f680c5c9b092950001ae/train/python
You're telling me these are the same difficulty rating? Come on now...
Ranks can't be changed though, and it's not the author's decision.
Nice Kata, great description with prefect hints. But it is more complicate than 7kyu. Ones started, need to finish a kata. And before start usually set time limit for exercise depending on kata's level. When under simple kyu stored more complicate kata this is good for training, but this consumes more time that you suppose to spend.
Thank you. Please keep making katas.
Ranks can't be changed tho.
lbvf50mobile: thanks!
Hello, I tried it and actually it works perfect when I try it on NetBeans, but here when I try to submit my code it passes 10 tests and fails 5 can somebody help me out please ?
Can I see your code?
This comment has been hidden.
This is python correct?
Got it, good job.
Thats exactly what I did too for my solution lol. You can even use arrays as well.
No, its java. Thx man :))
👍
This is more of a math problem, less of a programming problem.
Love the name lmao
How do we know if our code is correct?
By checking the output yourself for small numbers.
I ran my code on another compiler and it works but idk why the output is blank.
Language?
C++ and thank you for replying to my messages btw. Should i send you my code? you can run it on here: https://www.onlinegdb.com/online_c++_compiler
sure, but with spoiler tag.
This comment has been hidden.
Im new to this site but I really appreciate you being generous and helping me out!
Did I format it incorrectly according to how the Kata wants the solution to look like? @iaeliyen
'''
Code
'''
^^
Just like this, but you have to replace the those ' with a backtick
~(without shift)
, then your code would be like this:You can come over gitter.im/iaeliyen for having a discussion on this problem.
What is problem in crystal? I am getting negative answer, can someone please tell me how to do proper typecast? I am doing something like n.to_i65, but that's not working.
Try printing intermediate results. The type of a result of an arithmetic expression is the type of the first operand, so
i64 * i32
isi64
, whilei32 * i64
isi32
(at least in Crytal 0.24). Also, quite a few thing have changed since 0.24 including some basic arithmetic stuff, so I guess it's time to request a new version...It does not seems to be working, thanks for reply though.
Begin all operations with
n
which is ani64
and remove all your "to_i64" and it should work.I tried that, I was getting this(despite of the fact that there are negatives in my code)
I said to put in each function
n
at the head like you are doing withsumin
:-)This really enjoyable kata
Dart, Elm, and Factor translations.
All approved.
This comment has been hidden.
I got it: it is like: 654321 654322 654333 654444 655555 666666
!
This comment has been hidden.
I modified the Python test. Can you have a look?
You should change the number of tests and the input range (
5
tests with range800-5000
-->100
tests with range1000-1100
) asO(n^2)
solutions can pass now.Also such changes should be applied to all languages where you're testing user's solution with a quadratic time reference.I've looked through a bunch of translations and it looks like only Python had such badly structured test cases.Modified once more (the quadratic solution was already no more used): "100 tests with range 999-1101".If there still is something in Python which doesn't please you, feel free to modify yourself. Cheers! PS: it's only a 7kyu, don't be too harsh:-)
100 tests on 3 possible inputs?
Ok, I'll change it myself then.
Edit: I think I misread
range(999, 1101)
asrange(999, 1001)
... Doesn't matter really. Adjusted both Python and Ruby to have more tests withO(1)
reference solutions.Everything's according to your "Try to avoid nested loops" hint (without forcing the user to wait for 6-7 seconds all the time) ¯\_(ツ)_/¯
I thank you very much!
Hi, I'm have a problem with Java. If I run sample test they run without problem. But if I attempt the code run, even if it pass all tests the execution fail because of "Execution Timed Out". How can I fix it?
Make your code faster. Cheers.
I tried three different solutions in Python Loops Generator Expressions Itertools
However using all of them ended up with a timeout when attempting the final solution. I have to wonder wether the notion of this kata is really about your coding skills, because all of those three are programmatically valid and clean solutions in python. My impression is that this kata is based on finding a smart mathematical solution?
yes. Or at least, a "smart enough" one.
Not sure if your issue was timeout or maximum of recursion depth but you can increase the recursion depth with
sys.setrecursionlimit(10000)
I believe the kata is specifically trying to prevent solutions that count, requiring you to find an O(1) solution by seeing a pattern.
I have yet to give this kata a second try since I am here to improve on my language skills in python and not on my math skills, I usually avoid katas which require a mathematical solution.
I loved this kata! At first I thought it was trivial, but when you stated it could be solved without loops I got interested. It took me the better part of 30m to work it out (my math is rusty), but it was a lot of fun. Thanks for sharing it!
Thanks!
Upvoted (+1). I think I didn't see it before.
Thanks again!
This comment has been hidden.
Hi g964,
Small typos in the description:
the python code block at the beginning shows:
where it should be something like (I don't know the exact math notation so I'll let you handle the exact formatting ;) ):
or maybe
And a bit lower in the description:
The task is to calculate the sum of f(x), g(x) or h(x) for all integers x and y such that (1 <= x <= n, 1 <= y <= n).
should beThe task is to calculate the sum of f(x,y), g(x,y) or h(x,y) for all integers x and y such that (1 <= x <= n, 1 <= y <= n).
Cheers,
B4B
f(x, y) is min(x, y) and sumin(n) is sum of all f(x, y) = min(x, y) in the domain 1 <= x <= n, 1 <= y <= n so I think I must not change the first part: sumin(n) is
Sigma(min(x, y)) for all (x, y) in [1, n] x [1, n]
. Is that clear enough?:-) I tried to avoid math notation. But I will change "the task is to calculate...". Tell me if you agree. Anyway thanks for your post and cheers!sounds better yes. Though:
Sigma(min(x, y)) for all (x, y) in [1, n] >>x<< [1, n]
thisx
bothers me a little. ;)x
here means cartesian product: all pairs (x, y) when x is in [1, n] and y in [1, n]?(A × B = { (a, b) ∣ a ∈ A and b ∈ B }
.yeah, I know! I'm talking for other users, who might think about it being the variable. I don't know. Maybe to change it to
X
instead?This
x
does not appear in the description.Errr... ;)
Yes it is in my post but not in the description. Did you read the description?-) I wrote: "The task is to calculate the sum of f(x, y), g(x, y) and h(x, y) for all integers x and y such that (1 <= x <= n, 1 <= y <= n). The function sumin (sum of f) will take n as a parameter and return the sum of min(x, y) in the domain 1 <= x <= n, 1 <= y <= n. The function sumax (sum of g) will take n as a parameter and return the sum of max(x, y) in the same domain. The function sumsum (sum of h) will take n as a parameter and return the sum of x + y in the same domain.".
uh! sorry, I didn't check there, as you saw it...
good then!
Thanks for all your time and good work!
This comment has been hidden.
This comment has been hidden.
I didn't know the elegant maths for these series, so ended up doing a polynomial regression to solve summax/min. Sumsum was more straightforward.
Hi, I have a little problem, when i tried to run test my programm this one timed out but it could test at least one test. I use a double loops for sumin and sumax, the first for x and the other for y. But I don't think so it is the best optimize solution.
Hi
g964
,in the C#-version the [Test]-attribute for the method Test04 in the example tests is missed.
Hi Dentzil,
Yes, I know. It is in the "Edit" of the C# translation (you can edit it and see:-) but every single time I re-publish it CW removes it... Re-publishing is often awfully painful! Anyway thanks for your post!
Little problem in the description example : sumsum(999) --> 332833500 should be sumsum(999) --> 998001000
Fixed, thanks!
My solution (c++) was accepted with only one completed function (sumin); sumax was empty and sumsum returned 1.
I reported that as a bug: https://github.com/Codewars/codewars.com/issues/557 You can see my explanations there and you can complete them if you want. I thing the problem comes from your nothing returning in your 2nd function... Lots of thanks for your post!!!
Similar to most other solutions, I discovered that you could use a simple mathematical formula to derive the result. However, none of the solutions I found dealt with the fact that the functions would return an incorrect value if 'n' was too large. For example, pass in UInt64.MaxValue and you will get a response which is incorrect because C# simply wraps large values around its max.
Does anoyne have a thought on how to address this? I had attempted to set the equations equal to UInt64.MaxValue and then solve for 'n' to see if that would provide a constant limit... but my math-fu is too weak for this. Besides, I am not sure if this is the best apporach since C# may exceed the MaxValue while in the middle of computing the result... say by simply evaluating "n * n" before it ever gets to the division.
How about to let the functions return (sums mod 1000000000 [one billion]) or something alike?
That is how the 'big numbers' are checked as answers at projecteuler.net.
This comment has been hidden.
Sorry, but I cannot explain the math myself. Here is a reference though if you are interested: https://en.wikipedia.org/wiki/Square_pyramidal_number
This comment has been hidden.
In Clojure, my solution works but I get "It took longer than 10000ms to complete" failures when it runs the random tests.
Speed up your code:-)
I decided to do this the math way. I had a bit of a hiccup with sumax because the sumin graph included 0 (and matched nested-for-loop results with 0). Yes, I know that the range was stated as 1-n and not 0-n, but it could be clearer in the description.
Otherwise, awesome kata! It's simple enough for a beginner, but can also lend itself as a challenge for more experienced programmers.
Thanks for the feedback!
Don't calculate the result multiple times in your tests. E.g. in Haskell:
However, note that this test is actually misleading. What should work for positive integers?
sumsum
!You can apply the same logic in JS/CS and other rspec-like tests.
Two ways are better than mine but I prefer the 2nd one that is maybe clearer. Modified the tests according to the 2nd one. Thanks for writing the tests!
Just a small additional remark: you list properties of the thing you describe, so it's not necesary to add the thing again:
Output (except for the "Test passed" messages):
No random tests for
sumsum
in JavaScript (and probably in all other languages, but I'm not going to file an issue for every variant).Fixed where it has to be: JS, CS, HS. Could you mark the issue as resolved?
Seems fixed.
Your tests don't follow the
rspec
semantics ofdescribe
(which are the same in Python/JS/Haskell/CoffeeScript). You test one thing perdescribe
, e.g.:After all,
describe "foo" $ it "does bar"
should be more or less readable as "foo does bar". Alternatively, usecontext
. See rspec's documentation for more information about grouping tests.This would have given you a good hint whether you actually tested
sumsum
in Haskell, by the way ;) (see issue below).I'll now try not to forget rspec semantics.
Well, now your JS tests have
describe("Basic")
anddescribe("Random")
, which describe the tests, but not the object/method your testing.sumsum
doesn't get tested in Haskell.Fixed where it has to be: JS, CS, HS. Could you mark the issue as resolved?
Yup, seems fixed. But I have a new suggestion.
I like this kata but I think the description needs a little work.
I think the table is slightly confusing as it is. For starters, I think the 0 row/column should be removed - it doesn't add anything relevant. I can see what you were trying to do with the table, but I think I'd prefer either the x-axis be moved to the bottom (so they align) or to just bite the bullet and have the y-axis start at the top and increase downwards (to match people's typical assumptions about tables).
I think 'such as' in the line should be 'such that'. Similarly, 'take n as parameter and return the sum of the _' -> 'take n as a parameter and return the sum of _'.
Personally I would prefer the functions be called something like 'sum_f', 'sum_g' and 'sum_h', or alternatively remove the mentions of f, g and h (just call them min, max and sum) - otherwise it can be confusing what f, g and h actually have to do with the code.
Otherwise, good work! I like the hints a lot.
Thanks for your suggestions! I modified
I don't modify the table (too uneasy, it was difficult enough to do it:-) I can't modify the names of the functions otherwise all past solutions would be invalidated.
Is it hard to just remove the 0s? The function doesn't take 0 as an input anyway.
It's not that hard but it takes time to draw a table. Did they hinder you?
This comment has been hidden.
Correction: I actually seem to finish in just under 5000ms but get a memory error. This shows in the first submit, but in the final submit it comes cack as "unknown error"
This comment has been hidden.
I really enjoyed the challenge of this Kata. For the c# version I would specify that efficiency is an important focus area.
I originally solved the kata, but my tests were timing out because it was inefficient implementation.
I thought the hint saying to avoid nested loops was sufficient (whatever language, C# or others...). Thanks for the feedback!
I had the same issue with my java program. But I successfully improved the performance. You need to make sure that you don't brute force throught the nested loops inside the sumin and sumax. If you look at the table given in question you will see a pattern in the occurence of number for both min and max.
I havent yet figured out what the hell i am supposed to do.just made the machine spill ut the right numbers, but the explanation text was no help
I think there is a small error in c# example tests because interperter signal an error but submit button go.
Corrected, thanks!