6 kyu

Pascal's Triangle

5,574 of 11,682boatmeme
Description
Loading description...
Arrays
Mathematics
Algorithms
  • Please sign in or sign up to leave a comment.
  • Oreo_lover:> Avatar

    sorry for my importunity, but there's a typo here: "but depending on seleced method" -> selected method

  • Aza_rt Avatar

    What to do if I have reached max buffer size? I have no idea how to solve this in a different way.

  • Yushi.py Avatar

    Odd that you have to flatten the triangle, but I guess it's a diferential.

  • saudiGuy Avatar

    python new test framework is required. updated in this fork

  • Ramy-hakeem Avatar

    it was a nice one but i'm confused as to why when n=2 it has to return [1,1,1] not [1,1]

  • jpssj Avatar

    Common Lisp translation, please approve / reject (author inactive).

  • JJRG Avatar

    There is an error in the test cases when n > 2. In my program when n = 3 my result is length = 6 and array(1,1,1,1,2,1) In my program when n = 6 my result is length = 21 and array(1,1,1,1,2,1,1,3,3,1,1,4,6,4,1,1,5,10,10,5,1) etc... All my tests have a correct answer, but something is wrong with the test cases because it tells me that it is not correct. Please, solve the bug.

    I have seen another error in the Sample Tests: move 3 to n move 6 to el move 1 to e(1) e(2) e(3) e(4) move 2 to e(5) perform doTest end tests.

    It is wrong, the correct one is: move 3 to n move 6 to el move 1 to e(1) e(2) e(3) e(4) e(6) move 2 to e(5) perform doTest end tests.

  • weirdfishh Avatar

    That was really satisfying to solve. The rules found on the wikipedia article helped a lot.

  • JAC Sadi Avatar

    This comment has been hidden.

  • nomennescio Avatar

    Just want to give a thumbs up for the formatting of the C tests (although there's some misalignment with the spaces)

  • rickansen Avatar

    I just refactored my solution from the version 2 of this since i did that first, still nice kata!

  • Beyouuu Avatar

    Was doing it in C, passed all the tests, tried to attempt but got this error:

    free(): invalid next size (normal)
    double free or corruption (out)
    

    Since the string is already allocated and we don't need to free, i guess there is a problem with the free in the test.

  • ejini战神 Avatar
    • Ruby 3.0 should be enabled (Refer this & this for more detail)

    • function name should use snake_case

  • akar-0 Avatar
  • trashy_incel Avatar

    C translation (author gone)

  • KiberCyber Avatar

    using Rust i can pass "Test" without overflowing by "u128" type - ok, nice at attempt i've got error "mismatching types" - my "u128" can't be compared with your "usize" at your function - ok, maybe i should change "u128" to "usize" ? - no, it's overflowing

    my opinion is that it's not ok am i made a mistake somewhere?

    (sorry my english, not native)

  • farhanaditya Avatar

    Beware of overflow. Requested terms of a triangle are guaranteed to fit into the returned type, but depending on seleced method of calculations, intermediate values can be larger.

    Is this info related to a specific language? Because I don't get it at all.

  • Bean42 Avatar

    Very good description. Good Job putting the gif in there.

  • vvedenskiy Avatar

    I really liked it, thanks!

  • NazarKokhan Avatar

    This comment has been hidden.

  • prosasah10 Avatar

    I have almost the same "Expected" and "but was" in the error for random and 60th, but it's quite hard to check all numbers. Anyway, any number i check is correct aaccording to "expected". What can be the problem?

  • Kykuy Avatar

    This comment has been hidden.

  • hobovsky Avatar

    CoffeeScript: no random tests

  • hobovsky Avatar

    Updated fork of Haskell translation, with added random tests, enabled GHC 8.2, and resolved merge conflicts.

    Please review, and since I am not really good with Haskell, update if necessary or let me know what has to be fixed.

  • hobovsky Avatar

    Fork of C++ translation with fix for non-compiling sample tests

  • razvanboboc Avatar

    I think the formula in the description is flawed: (n k) = n!/(n-k)! According to this wiki page, the formula is: (n k) = n!/k!(n-k)! https://en.wikipedia.org/wiki/Binomial_coefficient

    I tried the formula from the description and the generated elements are not correct. As for the second one, it is fine.

  • marcica696 Avatar

    This comment has been hidden.

  • m.kravchuk Avatar

    K i didnt figure it out. How come my factorials are wrong for levels 60+ ?

  • m.kravchuk Avatar

    How come my factorials are wrong for levels 60+ ?:)))))) does it has anything to do with my RAM or something?

  • Mastermind_sap Avatar

    This comment has been hidden.

  • imjasonmiller Avatar

    Rust translation kumited! :-)

  • hobovsky Avatar

    Java translation with added random tests and resolved merge conflicts.

  • Mastermind_sap Avatar

    This comment has been hidden.

  • Voile Avatar

    Some languages do not have random tests, e.g Haskell.

  • orestes7054 Avatar

    Module Sympy is not working. It was forbidden or something???

  • JohanWiltink Avatar

    ( Haskell, possibly others ) ( Not JavaScript, possibly not others )

    No random tests.

  • user4708048 Avatar

    in python : OverflowError: Python int too large to convert to C long

    all my tests pass but the "random test" due to the above line

    how to overcome it?

  • Chrono79 Avatar

    Forked Python translation, the author is long gone, so any power-user that can check it in a week or so...

  • Siddiantus Avatar

    This kata is REALLY good

  • pnaib Avatar

    I liked this kata, managed to find the numbers in only one loop :)

  • guzzler13 Avatar

    this kata is great, but I did not earn points ... do you know why?

  • uxvikas Avatar

    It is throwing me an error "Cannot set property '0' of undefined" when I try to submit my solution. Although, I have tried the solution on other web based tools like jsbin and codepen and it works just fine there. I am not sure what is causing this problem. Any guesses ?

  • NSEvol Avatar

    c# sample test wrong : n = 1 must reurn {1,1,1} not {1}.

  • Unihedron Avatar
    • Missing sample test cases in Ruby, CoffeeScript, Javascript.
    • Haskell and C# sample tests are almost non-existent and should at least contain the statement given in the description.
  • lbvf50mobile Avatar

    This is supper KATA to show power of TDD. Because of this amazing gif image. When I stareted I had no idea about this algorithm, but step by step using tdd I made pretty nice solution. Thank you

  • mblair415 Avatar

    JavaScript version. My code works fine in Atom. When I run the sample tests here I get this failure:

    Test Results: Solution should test for something This is just an example of how you can write your own TDD tests - Expected: 'expected', instead got: 'actual'

    Is there a mystery direction that requires I also write some tests for this!?

    When I submit my code it works fine. The error only exists for the sample tests.

  • tommyhyatt Avatar

    I'm passing every test in Javascript except for n=25, which maybe means 25 has some clerical error in it (as AndrewsAnthony has also suggested, I think, below). Not encountering any timeout or similar. Any chance it could be checked? Nice Kata otherwise!

  • AndrewsAnthony Avatar

    Does have last test (25 rows) on Javascript the error? (sorry for my english)

  • mahburg Avatar

    Javascript tests are not written.

  • notherexxx Avatar

    Sad that noone tried to golf it in JS :/

  • kotenarok Avatar

    Using recursive and got "Process was terminated. It took longer than 12000ms to complete". //PHP Version

  • smiks Avatar

    You should add random tests too.

  • spacether Avatar

    Ruby level 2 triangle is failing when producing the array [1,1] Problem specifies that the result should be a single dimensional array.

  • rivermanvr Avatar

    This is the first time I created my own test cases. Thought I followed the directions properly and created this line:

    Test.assertEquals(pascalsTriangle(5), [ 1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1 ]); The first argument being the function with its argument stated, and the 2nd argument being the answer expected. ...not sure what I did wrong.

    Secondly, I hit submit to have the creaters test cases run and passed all the tests. ...Also did not get a final submit button to show.

    Help would be greatly appreciated.

  • faabiopontes Avatar

    Very nice Kata! At first it seemed impossible, but then after studying a little of the Pascal's triangle it just kind of clicked you know? "It's array, everything is a array"

  • gMarx Avatar

    This comment has been hidden.

  • sankh-15A Avatar

    Even after passing all the tests ("You have passed all tests! :)", the button "Submit Final" does not appear !!

    What is wrong ?

  • manurampandit Avatar

    This comment has been hidden.

  • mariohernandezv Avatar

    Really fun Kata. I think you should explain in the description that the final array must have all the lower levels of the triangle and not just the level n.

  • donaldsebleung Avatar

    PHP Translation Kumited, please accept :D

  • alexandersmanning Avatar

    Here are test cases for the ruby people, so they can understand indexing and what the output should be

     it "should create proper triangles" do 
        Test.assert_equals(pascalsTriangle(1), [1])
        Test.assert_equals(pascalsTriangle(4), [1,1,1,1,2,1,1,3,3,1])
      end
    
  • itsafinn Avatar

    The description in Ruby version could be indeed made better with examples; I first looked over the "returns a single-dimensional array representing Pascal's Triangle to the n-th level." as a possible typo ("why not arrays in arrays? could it mean a single row instead?").

    Also, it would be helpful if the test showed exactly what went wrong - I apparently had some problems with floating point accuracy on my first attempt, failing on larger numbers of the triangle.

  • HotCheese Avatar

    Ruby version lacks an example output.

  • orangemukmuk8779 Avatar

    Returning a 2-dimensional array works as well as a 1-dimensional array for the "submit" (javascript).

  • deanbrophy Avatar

    I enjoyed doing this kata is C#, but I'm going to do it later in Haskell. Solving it in something purely functional would be fun.

  • zhaparoff Avatar

    IMHO tests for this Kata should contain cases with some big N to force padavans to read some Wiki and to think about some code optimizations. Most of solutions have very straightforward algorithm but pass tests.

    Or mark this Kata with higher kyu.

  • hilary Avatar

    This comment is here for others starting on the kata so they can get the most out of what is an intriguing little kata.

    From the wikipedia page the kata author cites: "The rows of Pascal's triangle (sequence A007318 in OEIS) are conventionally enumerated starting with row n = 0 at the top (the 0th row)."

    However, the tests for this kata enumerate the rows starting with row n = 1 at the top (the 1st row). (The instructions do not warn that the kata is not following the conventional enumeration and give no worked examples.)

    It's a fun kata which is not particularly difficult to solve. It's more interesting to solve with the kata author's variant, in fact. (I would have constructed my solution differently had I known about the unusual enumeration on the way in. As it was, I just tweaked a parameter to make it work and moved on.)

  • DeirhX Avatar

    Shouldn't this be on higher kyu? Definitely fun but not really on the same par as efficient finding Hammond's numbers for example.

  • markiz Avatar

    Description is idiotic, what author actually wants is a WHOLE triangle mapped into one-dimensional array, not the n-th row. E.g.

    2 -> [1,1,1] # first row then second row 3 -> [1,1,1,1,2,1,1,3,3,1] # first row, then second row, then third row

  • muesli4 Avatar

    Haskell test cases are probably bugged:

    pascalsTriangle
    should work for some examples
    expected: [1,1,1]
     but got: [1,1]
    
  • osofem Avatar

    Factorials above 20 was a pain in the butt!

  • Bodigrim Avatar

    I thing type declaration

    pascalsTriangle :: Int -> [Integer]
    

    will work better for Haskell. Numbers in Pascal triangle grow pretty fast.

  • Bovaz Avatar

    I spent 20 minutes debugging this, before realizing one of the tests I added had a typo... Not really a hard kata

  • Osmosis Avatar

    Was going to do a Haskell translation but I noticed there are Python and Java translations sitting there that have not been added. I think I will wait until those are added, first.

  • pablo.varela Avatar

    Thanks for this kata. I translated it to java, feel free to merge it if you are happy with the translation.

  • afonsomatos Avatar

    Solution have so much code.. check my best practice solution!

  • oldccoder Avatar

    So this is a level 4, but I get only 1 honor point for completion?

    How come?

  • user9582889 Avatar

    This should be the n-th level of the pascal triangle Level 0 : [1] Level 1 : [1,1] Level 2 : [1,2,1] Level 3 : [1,3,3,1] Level 4 : [1,4,6,4,1] if you write pascalsTriangle(n)[k] with these values you have the binomial coeff of (n choose k).

    Instead the test force us to level 4 == [1,1,1,1,2,1,1,3,3,1] which are binomial coefficient up from (0 choose 0) to (3 choose 3) no really the good value for 4. and with indexes all over the place, this kind of result would be very difficult to use in a real program

  • os6sense Avatar

    Please, Please, PLEASE, do not use animations which cannot be stopped, paused or hidden. See WCAG 2.1 and 2.3 http://www.w3.org/TR/WCAG20/.

  • Ellyll Avatar

    It should really be pascals_triangle rather than pascalsTriangle for Ruby style :)

    This one certainly made me think. My first attempt failed on level 20 due to rounding errors, so I re-wrote it in a different way.

  • ineiti Avatar

    I thought I solved that kata - but can't find my solution anymore - did I dream (well, I could always blame my connection, couldn't I)?

  • jhoffner Avatar

    Ranking feedback? 4 kyu or 3 kyu?

  • constablebrew Avatar

    The 25th level test case was a good catch for my factorial based solution, which ran into accuracy problems. I simplified the factorial expression to overcome the problem, but I am sure larger triangles would reveal this weakness in my solution whereas other algorithms that reference previous cells would not have any problems.

    I think a good variant of this kata would be to ask for a single cell's value that is deep within the triangle.