7 kyu
Sort array by string length
24,901 of 50,506stevehopkinson
Loading description...
Sorting
Arrays
Fundamentals
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!
Haskell translation
forked and approved
This exercise taught me a lot of other related functions, super useful.
Did anybody else notice how really weird and bone chillingly creepy the test case's string combos are? ["a","Dog","Of","Food"] ["Beg","Life","I","Too"] ["","Moderately","Brains","Pizza]
Like what in the world, straight out of Stephen King novel
CoffeeScript translation
Approved
This was cool, I loved it!
I solved this with javascript, pretty fun!
random tests on php do not pass, because test generates random array where 2 elements have the same length, which contradicts the descripption
fixed. feel free to raise an
issue
when you encounter such problemsAll of the strings in the array passed to your function will be different lengths, so you will not have to decide how to order multiple strings of the same length.
It is not true, in random tests the array contains string of the same length.
(PHP) fixed. feel free to raise an
issue
when you encounter such problemsdef fn(arr): print("In fn:", arr) return
for n in range(len(tests)): # an array of strings NOT two arrays data = tests[n] # TWO arrays/lists print() print("Data[0] (The Problem) =", data[0]) print("Data[1] (The Solution??) =", data[1])
pass
''' 1.The convention in Codewars is that the sample data is presented as ,.
You have observed this in your INSTRUCTION SECTION.
your check/evaluation/validation of the submitted code has a simple bug.
You REVERSE the itemS, and my code fails.
OF COURSE IT FAILS!! FIX YOUR DAMNED CODE AND STOP BEING SO PIG-HEADED ABOUT IT.
THIS IS MY LAST MISSIVE ON THIS MATTER. THE MORE YOU PROTEST, THE WORSE YOU LOOK.
It's said, "When you're dead, you don't know it, but other people do." It's the same when you're stupid!
Data[0] (The Problem) = ['i', 'to', 'beg', 'life'] Data[1] (The Solution??) = ['beg', 'life', 'i', 'to']
Passing in the twin array: In fn: [['i', 'to', 'beg', 'life'], ['beg', 'life', 'i', 'to']] [ Answer ], [ Problem ]
'''
You're the one keep protesting against test data that is intended to be
[solution, input]
but you misinterpret it as[input, solution]
.You need to observe the basic test code carefully:
You can see that the first item of the list is intended to be the solution, not the input. The input is the second item. The test has handle this correctly, it puts the second item (the input) to function, and check whether it matches the solution (first item).
Again, if you don't like the test order of
[solution, input]
, fork the kata -> edit the fork -> submit.7 kyu - Sort by String length What you are saying is that this ONE kata can present the data in reverse, contrary to every other kata in the system, WITHOUT EXPLANATION that this is the case.
Clearly your evaluation code is WRONG. It indexes the two arrays in reverse. Easy to fix!
How can you pretend to defend such an obvious error. Your explanation is unacceptable and should be reviewed by your peers.
If you have a look at my profile, you will see that at 4kyu I am no longer a novice Please do not insult me with yout puerile responses.
If you print out the array in your function, you may see for yourself what is being passed in, and if it is already sorted or not. You may also try returning a hardcoded answer for the first testcase and see what passes and what fails.
This comment has been hidden.
It is not an error. People have different perspective of how the tests should be managed. It doesn't have to be in the order of
[input, solution]
. Although, yes that can make some people be confused of the test, the one that wrote the python translation wanted it[solution, input]
, so be it. The test still working fine right? It's a weird thing making it a reason to label it as an error.If you don't like it, you can just fork the kata, edit it, submit it, and wait for approval.
The argument passed to the fn is already sorted BY LENGTH. ["i", "to", "beg", "life"] (tests[0][0])
The second element, the "solution" is unsorted: ["beg", "life", "i", "to"] (tests[0][1])
Just look at the problem as listed below!!! PLEASE!!
Is tests = [ [["i", "to", "beg", "life"], ["beg", "life", "i", "to"]], [["", "pizza", "brains", "moderately"], ["", "moderately", "brains", "pizza"]], [["short", "longer", "longest"], ["longer", "longest", "short"]], [["a", "of", "dog", "food"], ["dog", "food", "a", "of"]], [["", "bees", "eloquent", "dictionary"], ["", "dictionary", "eloquent", "bees"]], [["a short sentence", "a longer sentence", "the longest sentence"], ["a longer sentence", "the longest sentence", "a short sentence"]], ]
i see ^^ : All of the strings in the array passed to your function will be different lengths, so you will not have to decide how to order multiple strings of the same length.
Failed asserting that two arrays are equal. Expected: Array ( 0 => '' 1 => '7k' 2 => 'm3' 3 => 'JGQ' 4 => 'F92'
def sort_by_length(arr): 7kyu
The solution in each case appears before its problem Fix??
Could you rephrase this? I don't understand what's the problem here.
problem/soution #1 [["i", "to", "beg", "life"], ["beg", "life", "i", "to"]],
["i", "to", "beg", "life"] is already sorted by length
["beg", "life", "i", "to"] this is the task
task and solution are reversed?
Uh... not really. The code goes:
for exp, inp in tests: ...
, meaning the first list is the expected result, and the second one is input. It's a bit strange, sure, but it still works.This comment has been hidden.
перевод на lua
Not a kata suggestion. Read this: https://docs.codewars.com/training/troubleshooting/#post-discourse
Language: C++
Test suit missing the required headers
std::string/std::vector
fixed by ejini战神
This comment has been hidden.
Thanks!
nice Kata
easy
what
C translation (author gone)
Approved
Good stuff
Update to Scala 3
.
Go translation
Approved
D translation
Approved
Rust translation
Approved
This comment has been hidden.
With more than 22,000 passed solutions, what is more likely to be bad, your code or the kata? Please use markdown tags to format your code or it's not usable. Refer to the documentation about this and more: https://docs.codewars.com/training/troubleshooting/#post-discourse
You not understand me. This kata is work in vs code, but not work at this compiler. Maybe my code is bad, but him is successful compile in vs code. Who bad my code or this compiler? Sory for spoiler up.
All day long there are users complaining of the same: their code supposedly works on their IDE, but not here. I have never one whose error was originated in Codewars compiler. So who bad? I'd bet your code.
I don't you think seriously read and followed the indications from the link I gave you. Please do it first, and your problem persists, come back with some useful information, as it is explained.
You're missing some
using
there for your code to work.It is a bad practice to use the name "array" as parameter. something like "arr" makes sense.
in which language, and why is that ?
Please approve the C++ translation which should fix all the issues regarding errors at
random tests
sectionapproved
the tests are no running in C++, spits out a stderror.
fixed by ejini战神
This comment has been hidden.
This comment has been hidden.
Have you tried printing
list
?This comment has been hidden.
Hi guys, I'm doing this kata in Python. I've managed to work out how to sort the list by string length. I pass all inital 'tests', and then I pass all 'attempts', including all 3 random tests.
Here are the results copied: Time: 906ms Passed: 8 Failed: 0 Exit Code: 137
However, I get an error at then end that stats this: " STDERR Max Buffer Size Reached (1.5 MiB)"
Any ideas what I'm doing wrong?
Are you printing too much to the console?
Ohh thats exactly it!
I forgot to remove my print() tests.
Thanks heaps!
'looks like you've solved it'
This is OCaml only
Decription suggests working with arrays, but in reality it expects working with lists. Please update the tests to expect and test against arrays, not lists
exactly. wtf.
I faced a very strange problem. I submitted the solution but my points got deducted. I didn't understand the reason. Earlier I had 60 point now 59. Faced something strange yesterday as well. Submitted a solution but points didn't increase. Anyone can tell me what exactly it is happening? Thanks for the help.
Cache issue. (Typically refreshing the page should fix it) Or at that point of time one of your comments or solutions got downvoted. (Not a question related to the kata nonetheless)
This comment has been hidden.
fixed by ejini战神
Passing all tests in C++ except for the RandomTests, which throw an "unknown exception". Has anyone else had this problem, or is it just my code?
fixed by ejini战神
I can pass all initial tests but every "attempt" test states that my answer should equal an empty list. I'm assuming this is a bug of some sort? Tried running it in python 3.4.3 and python 3.6.0, same issue.
Besides that, great problem!
Don't modify the input.
It's fine to relax.
Scala translation available for review.
Random tests in JS are absolutely bullshit. Assertions should be rewritten completely.
Fixed.
there is an issue on javascript when i try for the array: ['A', 'Of', 'Dog', 'Food'] it turn error like this SyntaxError: Invalid or unexpected token
Not a kata issue, your code is wrong.
This comment has been hidden.
The random tests were wrong, your function isn't returning anything (you're also overwriting the param value).
you could not redefine the array and you should return the operated array.
This comment has been hidden.
User is not active anymore.
The random test cases are broken in javascript. My solution passed without even returning a value for the random test cases.
The line that needs to be fixed is " Test.assertSimilar(testArray.sort((a, b) => a.length > b.length),testArray); ".
Details:
sort
mutates the array and returns the array itself, so it's literally just comparing the same thing ;-)Fixed.
the same problem now :(
Ruby Translation added :)
I have trouble with the random python test. When i normally run my code it sorts the random array as it's supposed to do but the test says it sould equal []. If i set a limit for maximum length of the array and return [] like it told me before, the test says it should return the sorted list ...
thx for any help, maTuUx
I had the same problem.
Likewise
Aren't you changing the input array
arr
in some way? It's a bad practice, and also the single most frequent cause of error in CW.C translation kumited. :)
No random test cases with OCaml?
Python translation kumited.
Pleas check and approve.
Thanks,
suic
C++-Translation kumited.
Please check it and approve. Thanks! ;)
Currently the vector argument is called 'array' in C++ translation.
That confused me for a while :)
Great Kata! :)