7 kyu

Functions of Integers on Cartesian Plane

92 of 3,129g964
Description
Loading description...
Fundamentals
Mathematics
  • Please sign in or sign up to leave a comment.
  • seddik@123 Avatar

    This comment has been hidden.

  • HerrWert Avatar

    My first attempt was perfectly accurate... and way too slow. Thanks for compelling me to write faster code. I need that!

  • saudiGuy Avatar
  • ejini战神 Avatar

    C#: method name should be PascalCase (Please refer to implementation of backward compatibility here )

  • eurydice5717 Avatar

    This comment has been hidden.

  • eurydice5717 Avatar

    This comment has been hidden.

  • ixian Avatar

    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.

  • LegendaryFartMaster Avatar

    The math was rather tricky for a 7 kyu

  • Polymathic Avatar

    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”.

  • anter69 Avatar

    Description and "sample table" is unclear/terrible/misleading

  • ffoleyTE Avatar

    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.

  • akar-0 Avatar
  • ejini战神 Avatar
    • 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)

  • yigres Avatar

    The tests pass, but there is a 12000 millisecond timeout problem. I write in js.

  • dfhwze Avatar

    Julia: error when running sample tests:

    ERROR: LoadError: LoadError: UndefVarError: sumin not defined
    

    Nim:

    Error: undeclared identifier: 'suMin'
    
  • krischinx Avatar

    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!

  • krischinx Avatar

    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.

  • krischinx Avatar

    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.

  • MikChan Avatar

    Quick maphs

  • JayYay Avatar

    I didn't understand the description. Can someone explain this to me? Thank you!

  • 1Stas Avatar

    Sumax - The tests are given 6 and 8 look at the difference

  • JTend Avatar

    7th kyu? really? requeriments are so confusing.. im programmer not mathematician

  • risato94 Avatar

    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

  • andorey Avatar

    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

  • nomennescio Avatar

    This comment has been hidden.

  • cgregurich Avatar

    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...

  • lbvf50mobile Avatar

    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.

  • ozgunbali Avatar

    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 ?

  • Cereal4Dinner Avatar

    This is more of a math problem, less of a programming problem.

  • kevalboo Avatar

    How do we know if our code is correct?

  • user9644768 Avatar

    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.

  • alexfaley Avatar

    This really enjoyable kata

  • user8436785 Avatar

    Dart, Elm, and Factor translations.

  • Konstantin Modin Avatar

    This comment has been hidden.

  • FArekkusu Avatar

    This comment has been hidden.

  • jeeza86 Avatar

    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?

  • MrTony Avatar

    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?

  • steven_dunn Avatar

    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!

  • raulbc777 Avatar

    Upvoted (+1). I think I didn't see it before.

  • Blind4Basics Avatar

    Hi g964,

    Small typos in the description:

    the python code block at the beginning shows:

    f: 1 <= x <= n, 1 <= y <= n --> f(x, y) = min(x, y)
    g: 1 <= x <= n, 1 <= y <= n --> g(x, y) = max(x, y)
    h: 1 <= x <= n, 1 <= y <= n --> h(x, y) = x + y
    

    where it should be something like (I don't know the exact math notation so I'll let you handle the exact formatting ;) ):

    sumin(n) =  1 <= x <= n, 1 <= y <= n --> f(x, y) = min(x, y)
    sumax(n) =  1 <= x <= n, 1 <= y <= n --> g(x, y) = max(x, y)
    sumsum(n) = 1 <= x <= n, 1 <= y <= n --> h(x, y) = x + y
    

    or maybe

    sumin(n) =  f: 1 <= x <= n, 1 <= y <= n --> f(x, y) = min(x, y)
    sumax(n) =  g: 1 <= x <= n, 1 <= y <= n --> g(x, y) = max(x, y)
    sumsum(n) = h: 1 <= x <= n, 1 <= y <= n --> h(x, y) = x + y
    

    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 be The 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

  • tetrisromanus Avatar

    This comment has been hidden.

  • Trofis Avatar

    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.

  • Dentzil Avatar

    Hi g964,

    in the C#-version the [Test]-attribute for the method Test04 in the example tests is missed.

  • nimou Avatar

    Little problem in the description example : sumsum(999) --> 332833500 should be sumsum(999) --> 998001000

  • FlingWhale Avatar

    My solution (c++) was accepted with only one completed function (sumin); sumax was empty and sumsum returned 1.

  • Kittens Avatar

    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.

  • bootkeen Avatar

    This comment has been hidden.

  • desbo Avatar

    In Clojure, my solution works but I get "It took longer than 10000ms to complete" failures when it runs the random tests.

  • jacobb Avatar

    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.

  • bkaes Avatar

    Don't calculate the result multiple times in your tests. E.g. in Haskell:

    it "should work for random positive integers" $ property $
        \(Positive x) (Positive y) -> do
          let value = 101 * x + y
              referenceSolution = solution (101 * x + y)
          
          sumin value + sumax value `shouldBe` referenceSolution
          sumsum value              `shouldBe` referenceSolution
    

    However, note that this test is actually misleading. What should work for positive integers? sumsum!

    describe "sumsum" $ do
      it "is the sum of sumin and sumax" $ property $ \(Positive x) ->
        sumin x + sumax x `shouldBe` sumsum x
      
      it "returns the correct value" $ property $ \(Positive x) ->
        sumsum x `shouldBe` solution x
    

    You can apply the same logic in JS/CS and other rspec-like tests.

  • bkaes Avatar

    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).

  • bkaes Avatar

    Your tests don't follow the rspec semantics of describe (which are the same in Python/JS/Haskell/CoffeeScript). You test one thing per describe, e.g.:

    hspec $ do
      describe "sumin" $ do
        -- test only "sumin" things here
        testMinij 5 55
        testMinij 6 91
        testMinij 8 204
        testMinij 15 1240
        testMinij 100 338350
        testMinij 365 16275715
        testMinij 730 129938905
        testMinij 999 332833500
        testMinij 2000 2668667000
        testMinij 4000 21341334000
        testMinij 5000 41679167500
      
      describe "sumax" $ do
        -- test only "sumax" things here
        testMaxij 5 95
        testMaxij 6 161
        testMaxij 8 372
        testMaxij 15 2360
        testMaxij 100 671650
        testMaxij 365 32484635
        testMaxij 730 259610995
        testMaxij 999 665167500
        testMaxij 2000 5335333000
        testMaxij 4000 42674666000
        testMaxij 5000 83345832500
    

    After all, describe "foo" $ it "does bar" should be more or less readable as "foo does bar". Alternatively, use context. 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).

  • bkaes Avatar

    sumsum doesn't get tested in Haskell.

  • eneb Avatar

    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.

  • Hvitnov Avatar

    This comment has been hidden.

  • MarkPearlCoZa Avatar

    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.

  • toolchild Avatar

    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

  • MicheDev Avatar

    I think there is a small error in c# example tests because interperter signal an error but submit button go.