6 kyu
Fold an array
801 of 11,303user5036852
Loading description...
Fundamentals
Logic
Mathematics
Algorithms
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.
👍
It was a Very Fun exercise!
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
nice kate
Nice kata
Great Kata; it was fun practising some recursion!
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
How do I erase katas I submitted too many times so I only show last one
You can't.
Great Kata, took me a while :D
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.
Would you be so kind to at least post your code, so we could check what is the problem with it? please remember about code blocks and spoiler flag!
This comment has been hidden.
Please see: https://docs.codewars.com/references/markdown#code-block and post the code again, formatted in a readable way.
Your allocated buffer is too small.
with this line:
you do not allocate enough memory for an array of
int
of lengthlength_in
. the size of an array is not equal to its length, unless the array elements happen to have a size of one byte, which is not the case forint
.Please use the
question
tag to ask for help next timeThank you everyone for your help. I learned a lot about arrays in C and the ways of allocating memory. I also learned about the markup within the comments.
In the future I will only use the
issue
tag if I'm really sure that there is no bug in my code. But that probably won't be the case with C. ;-)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.
Your code is mutating the input array. If it didn't, then the test would be on
[ 1, 2, 3, 4, 5 ]
and folded twice, the expected answer is[ 9, 6 ]
. Not a kata issue.[6, 6, 3, 4, 5] 2 expected:<[[9, 6]]> but was:<[[14, 10]]>
I think I understand ty :3
One more question. How to change avatar in codewars??
Linking your CW account to a Github one.
This comment has been hidden.
That's because you're mutating the input, and the control function takes the same mutated input to calculate the expected value.
That's in the description.
Amazing Kata, It folds the brain jajaj
This comment has been hidden.
Probably nothing's wrong with test, but rather with your code. This is just telling you
you returned [4] and you should have returned [2]
. You may find some helpful tips in the documentation: https://docs.codewars.com/training/troubleshooting/Go translation
Can't approve because version is invalid. I changed version and create fork.
Thanks!
COBOL translation (author gone).
Approved
I keep getting a list incex out of range error and I have no idea as to why. I'm using Python for this
Because you are applying indices out of range. Please have a look at the documentation to see how to debug your code, you must print stuff to understand where it is failing: https://docs.codewars.com/training/troubleshooting/
Kotlin translation ready for review and approval.
This comment has been hidden.
OP solved it, closing
This kata has problem. Expected [9, 6] but showing [9] in JS.
What's the problem? If your function returns
[9]
for the input[6, 6, 3]
, then your function is not correct.This comment has been hidden.
OP solved it, closing
Amazing kata.. thanks for this ! :)
i am getting this error assertSimilar is deprecated, use assertDeepEquals
C translation (author left codewars)
Approved
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.
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
You'll have to post your code (marked as a spoiler) if you want better help, but if it works in your local system, there's a good chance that you're mutating the input. Tests 1-3 all use the same array in their function call (as in, the same variable), so if you're mutating it on test 1, you'll have bad answers for tests 2 and 3.
This comment has been hidden.
I have done it please take a look
This comment has been hidden.
Thanks
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.
IMHO strange things, as passing by value in prototype while instructions say "The input array should not be modified!" ?
nice kata!
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.
Are you modifying the input by any chance?
Hi. Yes i passed the folded array back to the function until a static property i call "count" equalls the run paramiter passed are equal. I gave up at the end and watched the answers. Bummer! work hard on a kata and end up with a working solution only to be rejected by a language error.
But thanks for the reply.
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.
This comment has been hidden.
expected [ NaN ] to deeply equal [ -514 ] error when attempting......what could be wrong please
There are many possibilities. NaN (Not A Number, see here) raises when you use an operator that is supposed to give a number (addition, multiplication or whatever) but Javascript (I guess it's the language you're using, please take care of mentionning it when you ask for help) could not perform the operation (operation with undefined values or letters, logarithm of a non positive number...).
You should try to debug your code to find out when it gets bad results, maybe you can find this helpful: Troubleshooting Your Solution.
This comment has been hidden.
My code works on codepen, but not here, did this happen to anyone ?
Probably you're tripping with this:
Rust translation
Approved by someone
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]'
The tests are ok, you're not following this rule:
That's not the input, use
console.log
and don't mutate it.This comment has been hidden.
splice
mutates the array. The same array is used in more than one test in final tests to make you obey the rule (the basic and sample tests are the same, you can see the input value is [1,2,3,4,5] not [1,2,4,5]). Your code is trying to fix that error appending thatmidNum
var you've created when the length is even and it's not 2, but it is wrong. If you fold an array with an even length all the folds except the last one will have an even length.This comment has been hidden.
Already told you the input array is not [1,2,4,5], it is [1,2,3,4,5]. See the description and the sample tests. First fold: [6,6,3] Second fold: [9,6]
when i logged it, it gave me [1,2,3,4]...
This is very frustrating.
And for the third time, that's because you mutated the input in your code. In the code you posted (not yours) see it uses a shallow copy with
slice
.Really nice Kata!
This one was fun :)
My solution works properly on my computer but does'nt work here :/
array = 1 2 3 4 5 runs = 1 6 6 3 array = 6 2 3 4 5 runs = 2 14 6
This comment has been hidden.
Why didn't you copy and paste only the data of the test you're code is failing? Not all that wall of text.
You're probably mutating the input array.
^ That's in the instructions.
This comment has been hidden.
SPOILER! Who forgot the spoiler flag? :/ Yes, and it looks like you're mutating the input. Clone the array first.
Can You explain me what does it mean?
Your code overwrites the input array, don't do that, use another array.
This comment has been hidden.
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]'
There is a difference there,
121
vs-12
but without knowing the input, nobody can tell you which one is right.This comment has been hidden.
You're not creating a copy of the array, it's another reference to the same one. Try this to check that:
When your function is tested the input was already folded once in the previous test, so it's
[6, 3, 3]
instead, and folding that twice it returns[15]
.Thank you so much, Chrono! I really appreciate the help. :) Have a great night!
This comment has been hidden.
This comment has been hidden.
You're mutating the input array there.
Steffen thanks for creating the kata.
Good one. Funny. I didn't actually laughed though.
this was good!
When doing the attempt, my code passes all the tests but spits out an error code. Please help!
No issue, check your code!
This comment has been hidden.
Not an issue, if your code does not work!
But it does work! In every TestCase. Just not with the RandomTests, where i cant check the input.
ok nvm changed it from recursive to iterative and it magically worked.
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.
It's stated in the description:
Yes,but it would be less confusing if when someone does try to modify it, as it seems from the discourse that lots of people do, the test script should either suggest to make sure the array is not modified or fail the test in which the array was modified. The function's execution in one test should not affect the input that is given to later tests, regardless of whether or not the description was read carefully.
I'd normally agree with you but, I did what you suggest in this kata: https://www.codewars.com/kata/remove-the-minimum/ People complain their code doesn't pass the mutation test anyway.
Perhaps failing the test and printing something out will help that.
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...:(
This comment has been hidden.
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
No issue. Check your code and do not create issues, when you are not sure about it!
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
Btw I think i found an error....
int maxLength = array.length; int countLength = (array.length+1)/2;
in my "for statement" it changes the value of maxLength and countLength... but here it's like initialize the variable set again going out from for statement...
What are you talking about?
This comment has been hidden.
nice
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!
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.
This comment has been hidden.
Your function name is incorrect
This comment has been hidden.
This comment has been hidden.
Please look at the kata before you write a comment for a bug. The kata has been solved 1436 times! So I think there is no problem.
Check your code again.
And do not forget this sentence from the description: "The input array should not be modified!"
if input array is not changed I get the same error From my point of view, if my script works in a console ,it should work on the site. 1436 other people used some other approach which should not mean that my approach ( again, working properly in console) is a bad one or something I will try something different but Not cool attitude, sir :/
Have you tried a "array = array.slice()" as first line (directly before your for-loop)?
thank you for your advise, it helped a lot! Now I understand what was wrong with the code, thanks a lot again
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
Fun Kata thanks
I'm glad you like it. :-)
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
This comment has been hidden.
There is no issue. This kata was solved 285 times in JS.
ok,i will try again
Great, that you solved it!
I met with the same problem ,I coded in Python,Have you solved the problem?
In my repl.it the code runs correctly (using ruby) but here it does not please advise
I have the same problem. "it shows my solution was passed both times (excepted[15])".
This comment has been hidden.
Thanks for this kata. Good and funny!
Thanks! I'm glad you enjoyed it.
A very good kata indeed. No obvious solution, a bit of a special case, without being overly "special-casey".
Thanks!
Thanks! I'm glad you like it.
This comment has been hidden.
Nice Kata SteffenVogel_79. Haskell solution kumitted for release.
This comment has been hidden.
Thx for this random test code. I just finished a, much less clean ;-(, approach in which ten random tests are carried out, the list to be folded and the number of folds are printed.
@SteffenVogel_79
,Although I can't speak for the other languages, this would not be an issue in Python if the tests were written properly.
Random Tests
, you could simply copy the input list and pass each function (user solution/kata solution) their own copy.Basic Tests
, the issue is caused byarr = [1, 2, 3, 4, 5]
. You could simply remove it and replace eacharr
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!
Thanks for the compliment. :-)
Now for the random tests the reference-solution is called first. So it would be no problem, if the user changes something. For the basic tests now a copy of the arr is used.
Generally I also like it more, if the tests are proof against manipulation. :-) Also if it is forbidden by the description.
I'm sad to say, that my changes are rejected by codewars... When trying to publish the changes, it says the solution is not valid, but "validating solution" gives only green tests... I'll try again tomorrow.
(Now it was possible to change the example tests... changes for the real tests are further on rejected... buggy editor...)
@SteffenVogel_79
,No problem, thanks for the response!
The same suggestion was raised in my
Rotate Corners
kata (which you approved, thanks again!). I simply hadn't thought about having to account for users modifying the input since my own solution didn't do that.Generally, I think it's good practice to not modify input variables. I just think it could be confusing for beginners to have their function output different results on their computer VS Codewars (as shown by the comment from
@optimusam
below).Anyways, it was just a suggestion not an issue! I enjoy your katas and I look forward to solving more of them!
This comment has been hidden.
Definitely no issue of this kata! Do not create an issue, if you are not sure, that the problem is in the kata!
As you can see, 53 people did this kata in python without any problems.
I think a reason for your problem could be, that you are changing the parameter-array. You should create a copy of the array and then change this copy.
I hope that helps. :-)
This comment has been hidden.
Where is the concrete problem?
41 people did this kata in python without any problems. And 348 people in generell.
I don't think there is a problem.
It doesn't compile on F# if you use Array.take
What do you mean? You get an error? Nothing happens? Have you tried to copy your code and reload the site?
Actually, the compiling error was on a different point, but it was saying it was on the Array.take. And a vey obscure kind of message. Now it's fine. It's just that the mono version shows unrelated or strange errors...
I think there is an error in one of the submit test cases. I added the following line to print the output:
And here is what I get:
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?
Oooooohhhhhhhh, that was my fault. I was modyfing the input array (as clearly stated, I shouldn't be doing that); the next test case was using this modified array, hence the wrong result
Great, that you found the problem. :-)
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.
Good advice! Thanks!
Now, it is mentioned!
Ich kann dieses Kata nicht genehmigen :( (500 Error)
In version II you can pass a parameter to say whether to fold left or right...
[3, 6, 6]
instead of[6, 6, 3]
In version III you can alternate the fold direction each run...
:-)
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,
or this:
Your 5th step is wrong and not necessary. There is also no step between 1 and 2!
BUT: The underline is very cool! :-) I will add this. Very good idea.
I thought the additional arroys would be helpful.... your opinion is, that only one arrow is easier to understand? Hmmm, I will think about that.
Please take a look now, my friend! :-)
Really good visualisation;-)... It was more confusing before than helping to understand... @Steffen: Only now i understand your "whatsapp- animation- message";-)...
??
Those pictures just like frames in a roll of film showing a book slowly closing from right to left. There could be more or less pictures (frames) and it would not be wrong - it would just be a faster/slower speed film :-)
Yep. Looks good now. Cheers!
Yet another fun little Kata. Keep the Java ones coming ;-) Thanks!
C++-Translation kumited.
Please check it and approve. Thanks! ;)
Speedy Translatos!
Approved! Thanks!
There is no need to use a class member function. Now to call it you first have to instantiate a class. Instead I would recommend:
or just
It was made to make the C++-translation similar to the origin version in C#.
I appreciate, when translations are near to the origin version! :thumbsup: