6 kyu

Fold an array

801 of 11,303user5036852
Description
Loading description...
Fundamentals
Logic
Mathematics
Algorithms
  • Please sign in or sign up to leave a comment.
  • Hunzombi Avatar

    It was a Very Fun exercise!

  • Nikita100days Avatar

    Hello!

    I recently decided to return to practicing. I am having an issue with this kata, as I keep receiving text "The input array should not be modified", meanwhile when i test in compiler it works corectly. Idk if i can post my code in comments, but I will be glad, if somebody could explain me what I am doing wrong and how to improve the code that i have. Language Python

  • FutureGoose Avatar

    Great Kata; it was fun practising some recursion!

  • mprime Avatar

    For TS the second test ([1,2,3,4,5],2) uses the output of the first test ([1,2,3,4,5],1) as the input. It's only happening for that one test, and if I reset the input variable just above the test, it passes everything. No idea how or why it's doing that

  • Argile5 Avatar

    How do I erase katas I submitted too many times so I only show last one

  • eli4me Avatar

    Great Kata, took me a while :D

  • DennisCook Avatar

    I think there is something wrong with the C random tests.

    I always passing all tests, except one. And every time I am getting this error


    Test Crashed

    Caught unexpected signal: 6


    Sometimes I also get this error but with an extra log. For example this one:


    input was: length : 61 {65, -60, -22, -15, 45, 34, -31, -43, -63, 96, -15, 66, -37, -62, 19, -29, -54, 24, -43, 96, -68, 87, 3, 61, -8, -44, 53, -6, -87, 14, 54, 78, 54, -68, -37, -2, 67, 32, 56, -97, 28, -60, 70, 92, -22, -11, -38, 25, -88, 19, 20, -55, 5, -78, -94, 98, -21, 59, -9, -7, -28}

    expected: length : 16 {28, -60, 70, 92, -22, -11, -38, 25, -88, 19, 20, -55, 5, -78, -94, 98}

    but got: length : 16 {91, 25, -64, -30, 40, 86, -66, -28, 1, 31, -35, 121, -98, 79, -95, -40}


    After checking manually I think the expected array is wrong.

  • gianluur Avatar

    Hello, I would like to report an error with the the basic tests, so my code works fine in all the test expect one of the basics. I need to fold [6,6,3] 2 times and the it "should" return [9,6] but this is incorrect and it should return [15]. I will follow the logic of the example as you can see... [1,2,3,4,5], 1 fold -> [6,6,3] and if u fold it again [6,6,3], 1 fold -> [9,6] and if u fold it once more [9,6], 1 fold -> [15]. This is even wrote in the test cases expected = [ 15 ]; assert.deepEqual(solution.foldArray(input, 3), expected). So how is it possible that [6,6,3] fold twice is equal to [9,6]? If I missed something or I am wrong anywhere please let me know. Thank you.

  • LetMeeDie Avatar

    [6, 6, 3, 4, 5] 2 expected:<[[9, 6]]> but was:<[[14, 10]]>

    1. [11,10,3]
    2. [14,10] ?
  • Awalder Avatar

    This comment has been hidden.

  • R.Daneel.Olivaw Avatar

    Amazing Kata, It folds the brain jajaj

  • stumanov Avatar

    This comment has been hidden.

  • akar-0 Avatar
  • akar-0 Avatar

    COBOL translation (author gone).

  • BradyNeu Avatar

    I keep getting a list incex out of range error and I have no idea as to why. I'm using Python for this

  • docgunthrop Avatar

    Kotlin translation ready for review and approval.

  • StuckinCoding Avatar

    This comment has been hidden.

  • zahidulislam2020 Avatar

    This kata has problem. Expected [9, 6] but showing [9] in JS.

  • Kennedy1127 Avatar

    This comment has been hidden.

  • user1430804 Avatar

    Amazing kata.. thanks for this ! :)

  • amanlad97 Avatar

    i am getting this error assertSimilar is deprecated, use assertDeepEquals

  • trashy_incel Avatar

    C translation (author left codewars)

  • alexc19 Avatar

    Nice little kata, thanks. I provided a recursive and an iterative solution, and I regard both as quite neat. With a simple trick I was able to avoid a lot of overhead to handle odd vs even input length.

  • DarkLord007 Avatar

    My code provides the right output on my system yet it shows test case 2 and 3 failed with expected output [9,6] and showing mine output as [14,10] idk what i am doing wrong

  • icarus612 Avatar

    Personally i find the way checks are made on original array to be extreamly frustrating. It makes error testing nearly impossible if that is your issue. That being said: love the kata. 10/10 other than that.

  • eurydice5717 Avatar

    IMHO strange things, as passing by value in prototype while instructions say "The input array should not be modified!" ?

  • RNOH Avatar

    nice kata!

  • Amir Hamedi Avatar

    Hi. i get this exception : Expected string length 3 but was 5. Strings differ at index 0. Expected: "9,6" But was: "6,6,3" When i run the program however i get 9,6. Is this kata wrong or i'm missing something.

  • cccxxi Avatar

    A heads-up for people struggling with a certain issue mentioned here (JS/TS version):

    The input array for the tests is declared with var... So take that into account when working with the array inside the function.

  • edobasky Avatar

    expected [ NaN ] to deeply equal [ -514 ] error when attempting......what could be wrong please

  • aidanking Avatar

    This comment has been hidden.

  • arthurabia Avatar

    My code works on codepen, but not here, did this happen to anyone ?

  • imjasonmiller Avatar
  • ykIsCoding Avatar

    hey, if i understood the 'folding' correctly, i think there's an issue with the kata

    Log (array) [ 1, 2, 4, 5 ] 2 (number of folds) Expected: '[9, 6]', instead got: '[12]'

    so if you take a look at the above, the first fold should be (6,6) the second fold should be (12) so the answer should be correct. Where did [9,6] come from????

    Same problem as below....

    Log (array) [ 1, 2, 4, 5 ] 3 (number of folds) Expected: '[15]', instead got: '[12]'

  • phibble Avatar

    Really nice Kata!

  • Chrille Avatar

    This one was fun :)

  • Pawelle Avatar

    My solution works properly on my computer but does'nt work here :/

  • nvha63 Avatar

    Expected: '[43, 231, -444, -187, -28, 509, 122, 58, -365, -293, -631, 153, -27, 576, 1, 121, -690, -79, -16, -37, -553, -239]', instead got: '[43, 231, -444, -187, -28, 509, 122, 58, -365, -293, -631, 153, -27, 576, 1, -12, -690, -79, -16, -37, -553, -239]'

  • jordanmoore753 Avatar

    This comment has been hidden.

  • patricksudol Avatar

    This comment has been hidden.

  • Kordrad Avatar

    This comment has been hidden.

  • dagolinuxoid Avatar

    Steffen thanks for creating the kata.

  • gonpeche Avatar

    Good one. Funny. I didn't actually laughed though.

  • Tototo Avatar

    this was good!

  • Cluckerino Avatar

    When doing the attempt, my code passes all the tests but spits out an error code. Please help!

  • Binäric Avatar

    This comment has been hidden.

  • TheDoctor Avatar

    Modifying the array input causes the input for later test cases to not be what is expected for several of the tests. While it may be bad practice to destructively modify array inputs within a function, the test cases should be written so that it does not cause the test script to fail. This is still a really fun kata though. Thanks for making it.

  • miRemid Avatar

    expected [9,6] but it gets [14,10]... expected [15] but it gets [33]... and 1 error... i run my codes on sublime and all results right...why...:(

  • cbccbd Avatar

    Same problem in Python 2.7 as AlexTheHero-s: [14, 10] should equal [9, 6] in my Jupiter notebook and repl.it and cmd all simple tests works right

  • AlexTheHero Avatar

    expected:<[[9, 6]]> but was:<[[14, 10]]> ... it's really strange because when I run all the test on intelij idea... it shaws me right numbers...here it doesnt even pass the simple test...

    6 6 3

    Process finished with exit code 0

    9 6

    Process finished with exit code 0

  • ninooz Avatar

    My code works for all sample tests, but here it just produces an error. I can't figure out why, And my native code is correct!

  • ephraimg Avatar

    I'd appreciate some help with my solution. In a repl.it, the code works for all four sample tests, but here it just produces an error. I can't figure out why! Code in comment below...

    I'm working in Ruby.

  • bori0066 Avatar

    This comment has been hidden.

  • Lilian5 Avatar

    This comment has been hidden.

  • GuyInCube Avatar

    need guidance if any can be given. When i return a vector with the last value of 0 it does not seem to return that value, if i increase the size of the vector, the 0 is there. for example returning ret: [1,2,0] only shows [1,2]. but if i increase the vector size by one it'll return [1,2,0,4]. i don't understand why it's not recognizing the 0 when it's the last value

  • markhgbrewster Avatar

    Fun Kata thanks

  • MighteeCactus Avatar

    Cool kata, I enjoy it very much! Thank you!

    Btw, I think if you want to enforce people not to modify array parameter, you might consider to declare it as const. Tho nobody prevents the coder to modify arguments while solving the kata :D

  • Renkosky Avatar

    This comment has been hidden.

  • albmaso Avatar

    Thanks for this kata. Good and funny!

  • seasidetesting Avatar

    A very good kata indeed. No obvious solution, a bit of a special case, without being overly "special-casey".

    Thanks!

  • louiskueh Avatar

    This comment has been hidden.

  • JorgeVS Avatar

    Nice Kata SteffenVogel_79. Haskell solution kumitted for release.

  • zebulan Avatar

    @SteffenVogel_79,

    The input array should not be modified!

    Although I can't speak for the other languages, this would not be an issue in Python if the tests were written properly.

    • In the Random Tests, you could simply copy the input list and pass each function (user solution/kata solution) their own copy.
    • In the Basic Tests, the issue is caused by arr = [1, 2, 3, 4, 5]. You could simply remove it and replace each arr with [1, 2, 3, 4, 5].

    I don't disagree with having the message in the description (since it may be more relevant for the other languages) but it just doesn't seem right for the Python tests to be so brittle.


    Thanks for this kata, I enjoyed it!

  • optimusam Avatar

    This comment has been hidden.

  • akshaykalyan Avatar

    This comment has been hidden.

  • AlexHart Avatar

    It doesn't compile on F# if you use Array.take

  • lw-bmc Avatar

    I think there is an error in one of the submit test cases. I added the following line to print the output:

        System.out.println("Test case = " + Arrays.toString(array) + " runs = " + runs);
    

    And here is what I get:

        Test case = [6, 6, 3, 10, 11] runs = 2
        ✘ expected:&lt;[[9, ]6]&gt; but was:&lt;[[20, 1]6]&gt;
        
    

    As far as I can see folding [ 6, 6, 3, 10 11 ] twice should result in [ 20, 16 ], but expected value seems to be [ 9, 6 ]. Is this test case correct?

  • JohanWiltink Avatar

    Please mention in the description that the input array should not be modified. The tests break interestingly if you do.

    This is not a problem with the tests (modifying inputs is bad programming); it simply deserves mention.

  • suic Avatar

    Ich kann dieses Kata nicht genehmigen :( (500 Error)

  • dinglemouse Avatar

    In version II you can pass a parameter to say whether to fold left or right...

    • result [3, 6, 6] instead of [6, 6, 3]

    In version III you can alternate the fold direction each run...

    :-)

  • dinglemouse Avatar

    To be honest I found the Kata "visualization" confusing at first with all those arrows. And showing 1 more step before the result might help too. For example,

                           5            5        5          
                         4              4          4            5 4      
    1 2 3 4 5  ->  1 2 3      ->    1 2 3    ->  1 2 3    ->    1 2 3    ->    6 6 3
    
  • dinglemouse Avatar

    Yet another fun little Kata. Keep the Java ones coming ;-) Thanks!

  • Dentzil Avatar

    C++-Translation kumited.

    Please check it and approve. Thanks! ;)