5 kyu
flatten()
2,899 of 4,608jhoffner
Loading description...
Arrays
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.
Lua translation!
This comment has been hidden.
Because it's not supported in the current Node version this kata has. I don't really think using a built-in deserves a 5kyu rating so I'm not inclined to allow a newer version of Node and enable using that.
brilliant! It seems logical to me! Great kata too!
This comment has been hidden.
If you know someone who would want to update 2000 JavaScript kata, let me know. If you want to update at least some of them, reach out to me at
#fixing
channel of Codewars Discord, and I will explain you how to do this.Thanks!
Wouldn't that render this kata trivial and not deserving a 5kyu rating?
Thanks jhoffner!!
Was challenging for me but did alright.
Thanks Alot, This Is My First Time i Practice Recursion
Why does it say 'Value is not what was expected' with no explanation on the last test? What is expected there?
Tell us your language. Help us help you!
Insufficient data. Closing.
This comment has been hidden.
You should learn how default arguments work in Python. Not an issue.
You obviously haven't tried running multiple tests in a row.
Sorry and tnx, will have a read!
very nice kata. It was my first time practicing recursion
No sample tests in CS and JS
Submit tests copied
I really want to give up to this kata. but think again properly, you can solve this.
JS version has no test cases and will not do console.log on failed attempt tests... this makes it really hard to figure out what my code is even doing!! Any suggestions on how to force output even on the failed tests?
just skip every kata of jhoffner
console.log
?Nice little kata. that could also be used in a real world application. also really nice recursion exercise
This comment has been hidden.
That is only supported in node v11+, but codewars only has support for v10 so far. Also, that would make the task way too easy :P
:D sad but what u can say about concat() its failed again...
That does work :D You're probably using it wrong somehow :/
thank you buddy
You should add more tests to validate or not the work we do, i once passed your 4 tests but my function wasn't totally good, and it's just because i wanted to refactor and added a sample test i did myself that i saw that my code wasnt totally finished thanks for reading
that's about JS. Issue already raised below => closing.
I created a translation of this kata into Python: https://www.codewars.com/kumite/5d95a3e7ab4aad0022dbf303?sel=5d95a3e7ab4aad0022dbf303
Revised fork: https://www.codewars.com/kumite/5d95a3e7ab4aad0022dbf303?sel=5da2c7e12aec73af332917e9
Changes:
100
random tests.
You should probably disable
require
: https://www.codewars.com/kata/reviews/516f30217c907a79f20001a1/groups/5d4a3b202447aa00016867f9 https://www.codewars.com/kata/reviews/516f30217c907a79f20001a1/groups/5d4a38d70b4401000183cc88It is not stated anywhere that using libraries is forbidden.
My code passes all the real tests but fails both samples.
The tests is crap at least in JS, it relies on the
toString
method on user result to compare with expected, so anything that is not primitive (e.g functions and objects) will be destroyed. AndTest.expect
should be replaced.Also, needs random tests.
What is that last test all about. Passed all the three first Tests except that last test without any further information.
Hmm, nice one! Solved
Aside from no example tests this kata was nice
This comment has been hidden.
You only manage to do one-level of nesting (the outer-most layer). You should consider using a stack-based or recursive approach
This comment has been hidden.
This kata is driving me crazy. I don't know what the last test really does, it just says that some value is not as expected. No further information. I even test it in webstorm IDE against several inputs, with null's and empty arrays, nested arrays, everything works fine. I also converted all arguments of the process to an array before flattening. The validation module says:
Test Passed Test Passed Test Passed Value is not what was expected 3 Passed 1 Failed 0 Errors
any hints what could be the issue here?
May console.log be with you.
Hi, of course I console.log the arguments what is given to me and the original array that is created out of those before calling flatten() and also what is returned in the end. As long as the flatten() function is beeing called, the test passes and I get "Test Passed" after my console.log's are beeing displayed. But there are also tests that seems to check my function without invoking it. One of those tests passed. Only the last one didn't. There is little I can do with console.log, when the function is not even beeing invoked... or is there?
same problem here, I dont know what to do. anyone can help us? haha
Same problem here after 1 year! All three Test passed excpet the last test without further information.
In javascript, 1st(1,3) check for nested array. 2nd(2,4) check for result.
Your code fails for arrays containing subarrays consisting of
null
element.This comment has been hidden.
Because
Flat
is a global object, thus storing unnecessary results from previous function calls.I have not used it, but if someone wants to save that to be happy: /[^,][":]|[,]{2}|[":{[]}]+/gi
This comment has been hidden.
This might help?
This comment has been hidden.
This is not much helpful test output:
Test Passed Expected 1,2,3,4,5,6,7,8 but was given
If you could include the actual result in addition to expected result, would help to debug and fix what's missing.
I'm getting the same error and I'm console logging the input and I'm not getting anything helpful.
This comment has been hidden.
Without knowing Coffeescript, my guess is that you're iterating over the
arr
parameter instead of thearguments
special array that gathers up all passed in params.Your solution is not correct; it's an infinite recursion. Think about what happens if there's one argument that is an array.
Marking resolved as this is not an issue with the kata.
This comment has been hidden.
I can understand writing a function that takes as its input a potentially-nested array and returns a flattened array, but why would we want one that works this way?
The flatten() method probably isn't a useful utility method that you would ever actually want to use but the general idea of having to extract data and transform it into a different format is obviously something that is done often. This is a basic exercise to practice that concept. More advanced and real-world use kata that expand on this concept will no doubt be created in the future.
Sorry, that's not my point. A "flatten" function in general is quite useful. For example, let's say you have a method which returns a list of users via last name, then you can extend this to return a list of users by a list of last names thus:
In fact, this composition of map + flatten is so common it has its own method: http://apidock.com/ruby/Enumerable/flat_map
Scala also has first-order support for flatMap: http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/
map + flatten is the key to how the "List" monad works in Haskell, too, which is what allows it to be composed with other monads. In JavaScript, map + flatten would be the basis for aggregating the results from a sequence of Promises, for example.
But take a look at my solution. It makes a lot of sense to have a flatten function which takes as its input an array and returns a flattened array. It makes little sense pedagogically to have a "flatten" method where there's an "invisible" outer list that relies on the fact that every function in JavaScript is variadic. Not only is it a bad example since, as you say, you'd have no Earthly reason to write it, but it confuses the issue and requires the person solving it to also know the ins and outs of how JavaScript handles arguments.
On the flip side, if you have a "flatten" method that takes an array as input and returns an array, then the "argumentFlatten" function can just convert its argument list to an array and call the "arrayFlatten" function.
Thats fair enough. To be honest I don't remember my thought process at the time for making the functionality accept variable inputs. I may have been trying to actually require the user to know the ins and outs of how JavaScript handles arguments, as this is a very important thing to know - however it does fragment the spirit of the kata a bit (is it about handling arguments or about recursion).
At a personal level I tend to prefer methods to allow variable inputs. Especially if I see myself possibly having to wrap them in an array just to fit the method signature properly. A flatten method like this could be used to flatten multiple sources into one. Some would probably argue that wrapping multiple inputs into a single input would make the code more clear and I can see that. In this case, the method usage is actually flexible to do either or. If you want to flatten a single array just pass in a single array.
Sure, my point is more pedagogical, which is that you can build a function that does fancy JavaScript variadic stuff from the simpler function which takes an array as input and returns an array. Then you have
:)