4 kyu
Snail
31,988 of 80,109stevenbarragan
Loading description...
Arrays
Algorithms
View
Career Accelerator
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.
that was a tricky one i struggled alot, tip consider it like a snake
That was fun, i completed with only one loop. I seem to be enjoying these type of questions more, apparently. :)
It would be great to have more representative test samples. I got a timeout, so I assume the tests triggered an infinite loop, but there was no information about what caused it. I tested my code with lists from 3 to 10 items, and it worked fine, but now I have no idea how to fix it without knowing what went wrong. Are all the arrays in the test square?
You can always print the input at the beginning of your function to see what might be causing the issue.
This comment has been hidden.
Almost everything there is wrong. Starting with the function's name:
You should use the function's argument (
snail_map
), not some fixed value.Could you rename it into "Snail Traversal" in the description? Calling it "Snail Sort" is being sloppy with terms that have meaning and needlessly confusing. I just showed it to a friend, and they immediately fixated on this.
Test error when empty error. May be the test needs a correction.
Surprisingly fun kata, very enjoyed! The empty array in an array edge case input really confuses me. I was dissapointed when I got the whole thing working except for the empty array in an array test. I guess I don't really understand how to work with an array with only an empty array in it in Java.
I had already done more than half of the answer and it has not been saved ;(
finally solved it and each time i look at all the papers i used and how complex is my code i think i am a freaking genius , tht s all i ve to say
Given an n x n array,
Attempt gives
[[]]
which is notn x n
(it is0 x 1
array)No tests with
n x m
array. Please fix descriptionThis is specified in the description:
This comment has been hidden.
Had a lot of fun !
Really fun kata, little hint to someone, who dont have any idea of this task, just try to use array as an one-dimensional:) Maybe it will be helpfull, good luck)
It's been weeks of thinking about how to solve it, I finally finished it.
I am having problems with this one, how can I turn 1D list with n elements into a 2D array that my code is able to then traverse and snail_map?
for example: the test input [1, 2, 3, 8, 9, 4, 7, 6, 5] when I run through my python solutions in G Colaboratory yields - TypeError: 'numpy.int64' object is not iterable but when I pass: array = [[1,2,3], [8,9,4], [7,6,5]] yields - [1, 2, 3, 4, 5, 6, 7, 8, 9] - correct result!
How does one create a 2d array out of 1d array of unknown size? Sure, if all input would lend itself to 3x3 that would not be a problem ... how do I work this out, please help!
Isn't it the other way around? The input is a n x n array (or List for Python) and the output is a 1D list.
my problem is I am having partial success, so the code works for some nxn array, seemingly... here is whst i get:
Test Results:
Fixed tests Tests
Test Passed
Test Passed
[1, 2, 3, 4, 5, 6, 7, 8, 9] should equal [1, 2, 3, 6, 9, 8, 7, 4, 5] [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] should equal [1, 2, 3, 4, 5, 10, 15, 20, 25, 24, 23, 22, 21, 16, 11, 6, 7, 8, 9, 14, 19, 18, 17, 12, 13]
[1, 2, 3, 4, 5, 6, 20, 21, 22, 23, 24, 7, 19, 32, 33, 34, 25, 8, 18, 31, 36, 35, 26, 9, 17, 30, 29, 28, 27, 10, 16, 15, 14, 13, 12, 11] should equal [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
It looks like you are just taking a 2D array and turning it into a 1D list that is sorted (which is not what the Kata is asking).
Two things:
Fun kata, vague instructions.
nice kata!
Wen .Net 8.0 / C# 12 so we can use collection expressions and inline arrays ? …
Lack of support for C# 12 is a general problem for the whole Codewars, and not an issue specific to this kata. Closing as not a kata issue. The support for C# 12 has already been requested.
I've lost my sanity
Overall, a fun challenge, though I agree with those who are saying the test for the empty list goes against the instructions, as it is not n x n. Yes, there is a note about it and the fix is pretty trivial, but trying to explain it away as a design choice is just silly. It's a bad design choice.
Wow, such a fun kata.
Amazing kata i am proud finishing this!
Language used : C
Could someone tell me what is that uncheated snail ? Is it an empty one ?
It seems the very last attempt (the 15th ?) crashes (signal 11 : SIGSEGV)
[EDIT]]finally got that last little devil...
Tried
from complete_solution import snail as super def snail(array): return super(array)
to see if this is possible thank goodness it's notFun kata, not that my solution is efficient.
very proud for finishing it
Chai + assert for CoffeeScript
Approved
This comment has been hidden.
For those doing this kata in Prolog, do NOT use the transpose in
It gives problems with the tests for some reason, implement your own transpose
I've fixed it so that solvers can use
clpfd
.This comment has been hidden.
Please read the description:
Hello world
This comment has been hidden.
Lists are passed as reference in Python, so it's always the same list that you're passing and modifying in your functions.
array =return [[1,2,3], [8,9,4], [7,6,5]]
there is a 'return' in this test snippet
You must have edited it, backup your code, click reset and try again, there is no return in the sample tests. Not a kata issue.
Remember mentioning the language when reporting an issue.
Really enjoyed thinking through this one. Thanks!
dido
I misread that word o_O
This comment has been hidden.
This comment has been hidden.
The
ISSUE
label is for provable kata issues. Please use theQUESTION
label when asking for help. See https://docs.codewars.com/training/troubleshooting#post-discourse.That's not how an empty matrix is represented in this kata. From the description:
Not a kata issue.
this was a doable 4
This is probably the best kata I ever did. There's so much you can learn from this kata. Good luck to anyone trying this kata
My solution got StackOverflowError, but on local machine it works fine on test samples from attempt
Does your solution work locally when run in your IDE twice in a row for different inputs? Your solution seems to have a bug which causes that it works only in one test, but not in two or more tests.
This comment has been hidden.
I have same problem. Did You solved Your problem?
Coming up with a performant solution for this one was much trickier than I thought.
I have no clue what I am doing wrong on random_tests it keeps complaining about length being 0 but I dont know why its 0, does that mean its empty so I should return an empty vec but its not working still, Im so confused language RUST.
I figured it out, so the input is an array [] even though I was expecting an array inside an array and since I was indexing the second array which didnt exist it caused my issue.
Recomendations since this problem is kind of difficult if you don't know much about matrices. Write on a paper down the movement, and what does i and j do in each movement, then you'll eventually find a pattern that matches! Write the code first at brute force, then optimize the algorithm. Good luck whoever is reading this, liked this kata, took me a bit to optimize it and I believe it's not the most optimal solution!
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Please use appropriate formatting when posting code. See https://docs.codewars.com/training/troubleshooting/#post-discourse
You're not checking if the input matrix is empty correctly. From the description:
This comment has been hidden.
I tried this problem using python
from what I understood of the instructions, it said that arrays would be provided in the format of "list of lists", like: array = [[1,2,3], [4,5,6], [7,8,9]]
but random tests provided arrays like just one list: array = [1,2,3,4,5,6,7,8,9]
I still used math to calculate squareroot of these big lists, used numpy to make it into matrices, returned it to the format "list of lists", found the correct solution and yet the problem wouldn't accept the result, not sure why.
Random tests in Python always generate lists of lists with size
n x n
and you can see that with this line of code:print(len(snail_map), len(snail_map[0]))
my apologies, you are absolutely correct. The input for the problem is handled correctly. I just made a confusion with the attempt report that said: "answer" should equal "correct_result" and both are just lists, correctly so, because its a comparison between your code and the expected correct answer.
still don't know why my code isn't working, but the random tests are providing list of lists. Sorry for my confusion.
This comment has been hidden.
My solution works on my machine locally, but in tests it fails a lot of them and sometimes even the super simple ones like 2x2 array which is basically hardcoded in my code.
Hi,
not an issue. This might help.
Cheers
In Go solutions the function snail returns []int, but NOTE 2 says, that our func should return empty array inside of array
[[]]
, but how I can return [][]int, if return type is []int?I mean, I really get destroied by this NOTE, so I really need an hint
that's not what NOTE 2 says
but literally:
NOTE 2: The 0x0 (empty matrix) is represented as en empty array inside an array [[]].
Yeah I can see that. And it does not say what you said it says. You said that NOTE 2 says X and X is confusing, but NOTE 2 does not say X, so if you re-read NOTE 2, then you'll no longer have that confusion. You paste it back at me as if I haven't read it, but it's you who needs to read it again. >_<
Dude, thx u very much :D
Thx to natan
Great Kata, the return of the empty array was a little tricky, but overall a great little task. My c# is long, inefficient, but oh well. It works and I had fun. Thanks to the creator for your efforts
This comment has been hidden.
[Ruby] I can get all the test to pass except the first. It says I get [nil] but expect [] (I assume the argument is an an empty array), but when I test an empty array on in zsh on my computer I get [].
[Rust] Random tests sometimes generate an empty vector
vec![]
which doesn't match the Kata description saying that an empty matrix is represented byvec![vec![]]
Unfortunetly, I have an exception when array is empty (c#) it seems like a test case defect.
This comment has been hidden.
This comment has been hidden.
Your code fails with tests like this:
Not a kata issue. The random tests are always n * n:
thanks for the answer, I didn’t notice that the numbers must be unique, thanks, maybe it would not be superfluous to indicate this in the task description, for those inattentive like me :)
just in the examples from the problem statement - they are all unique
Now that the image is back, you can see that there.
The details are clear enough in my opinion, but the image after "This image will illustrate things more clearly:" is broken with a 404 error
Duplicate issue nad it's been fixed now.
Is SnailTest2Empty correct?
Conditions of the problem are "Given an n x n array", but for input array = [[]] array.Length == 1 but array[0].Length == 0 so it isn't 1 x 1 array, or 0 x 0 array, it's 1 x 0 array
I think this task is not determined precisely. The first sentence: "Given an n x n array..." is misleading. I was struggling with last test. The information from this test was also misleading. It turned out that my code wasn't prepared for a matrix like [[7,8,9]]. But it shouldn't be! Because it is the matatrix 1X3. When I understood which case I didn't take into consideration I corrected my code easily, but it wasn't my fault. The description of this exercise is to blame.
Can you please specify what language you attempted to solve this in?
Typically, with complaints like this, the issue is actually a misunderstanding on the user's part, or a bug in the user's code (for example, overwriting the input). However, on the off chance that the tests are actually malformed, this can be an issue and needs to be adressed.
I used Java. But I don't think it matters. The task refers to matrix n x n so there shouldn't be a test for a matrix [[7, 8, 9]] because this matrix is 1 x 3.
There is no such test in Java, the array is always n x n as the description says.
The random arrays are built like this:
You can see there they're always square.
Between SnailRandomTest I found this:
[[763]] should be sorted to [763].
So I added a suitable condition for an array and now it is ok.
Could you post your code that failed with the random tests? Please mark your post as having spoiler content. I can't reproduce what you say and in theory, mutating the input in your code shouldn't affect the tests.
P.S.: wait a minute, that test is fine, it's not the same as what you said in the first post.
I think what people are struggling with when doing this in C# is that the empty test case isn't really empty.
I was using array.Length to get the dimension of the array, but for the empty test case array.Length = 1 which caused some of my loops to go out of bounds because the dimension was not 1, it should have been 0. The array is passed as a jagged array and the empty case is a length of 1 with an empty array inside of that.
I had to fix my code by using array[0].Length to get the dimension of the array.
The image in the description test isn't loading
Image restored.
In Go is the input supposed to be named "snaipMap" instead of "snailMap"?
This comment has been hidden.
It's most probably an exception thrown by
at
, wheni
is out of range of whatever thesnail_map[leftRightCount]
is.snail_map
is not empty, butsnail_map[leftRightCount]
is smaller thani
.Looking at your code, that check seems to happen too late. Why not take care of this case at the start of function?
I finally figured out a solution. So I do have a check at the start
if (snail_map.empty())
which passes over the size 0 matrix. But when it came to a matrix of size 1 it kept throwing out that exception. So I added another check inside to see if the matrix of size 1 is empty as shown below.That was the only line of code I added to get the program to work through the Codewars test. EDIT: So I just reread the description and I guess my smooth brain didn't pick up on the hint at the bottom about how the 0x0 matrix is represented. Seriously though, I think it would make more sense to say a 1x0 matrix because the way most people understand
NxN
is[the number of vectors]x[the number of ints in a vector]
.More of a snake than a snail if you ask me
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Why are you not returning an empty list?
[0]
is not an empty list, neither is[None]
. There is no issue, you're not doing what you should do.This comment has been hidden.
Short-circuit your code, you won't apply it to that case. Use an if, it's as easy as that.
I wish I could understand. Anyway, thanks for helping.
Write something like this:
You need to figure out how to properly write that if condition, it's not the literal solution, just some pseudocode.
Sure, it's super easy:
I see that you're trying to turn a list into a string and then parse it back into a list. Are you trying to avoid input mutation? The standard library has a module called
copy
for that, in particular, you might find thecopy.deepcopy
function very useful. Docs: https://docs.python.org/3/library/copy.html#copy.deepcopyThis comment has been hidden.
That code works for any other case except
[[]]
, why is it so hard to use an if before that and simply return an empty array when the input is exactly that? I don't see the problem.Is "should tests with random snailmap" test correct?
Numpy shows their true power.
My code in java works locally for all the test cases that it fails here.... something is fucked up here
But it passed?
"Something is wrong" is quite far from a proper issue report. I am going to close this one, please reraise a new one with some details which would allow to debug the problem.
This task lifted my spirits! Thanks for this)
This comment has been hidden.
No, your code is wrong, not a kata issue:
Your function returned an empty array instead of an array of 9 elements.
So far the most satisfactory kata to get through. thoroughly enjoyed!
The fixed cases for Golang have issues. The empty matrix test doesn't work even though there's nothing wrong with the code.
This comment has been hidden.
OP solved it, closing
C translation has confusing initial code which is NOT illustrating what the user should return
Is it just me or the empty matrix solution in GO give some problem?
you should initialize the result like this result := make([]int, 0)
Enjoyed this, was so sure I had a clean solution, but then the solutions provided a solid facepalm. Couldn't ask for more
it does not work in python random tests but when i run it in ide it works fine. anyone has the same problem here?
A similar thing happened to me (I code in cpp not python), since it is a bigger code than normal, I first made the code in vs and then copied it here after running all the simulations I thought were needed. And moreover the error message was really ambigous. So I had to instead look at the testcases and identify in which testcase the problem was occuring.
P.S. it was {{1}}
My first 4kyu. WoooHoooo!
i pass these tests apart but i can not pass them together
Probably a problem with empty arrays
Might be a little too easy for a 4th kyu. Could it be possible to filter low performance solutions? My solution is "meh" but it still passed.
pain
Yooo man cool snail shit
I've written a function which I am almost certain should solve this kata, but I am having a couple of issues.
If I click "Test", I get a failure message as the sample test which includes a 36-element array seems to want the numbers in ascending numerical order [1, 2, 3, 4, 5, 6, 7, 8, ..., 36] rather than the actual spiral pattern of [1, 2, 3, 4, 5, 6, 12, 18, 24, 30, 36, 35, 34, ..., 21].
However, even if I hard code in an ascending numerical order for 36 elements, I am having an issue when I click "Attempt", where the numbers in the given array are not 1 to n, but instead seemingly random unordered numbers, such as [793, 58, 556, 635, 246, ..., 385].
That's a problem with your code, not a kata issue:
There are two fixed tests in javascript, one where the numbers are ordered in the array, similar to the first example, and another similar to the second example. Random tests contain random numbers. You must follow the snail pattern with them too.
@JPR95 - by the way, since you are new to Codewars, a useful tip for debugging is to print your inputs to console so you can see better what is causing you to fail:
how to print inputs Troubleshooting guide
As @Chrono79 said, you will find that some tests are passing you an input that is indeed "unordered", but you still should be able to create the required "snail" pattern from them (because the "labels" of the elements isn't relevant, only their location in the input array - you could use letters instead of numbers and the kata still makes sense.)
Oh man, looks like I skim-read the instructions and ended up bringing the pepper shaker instead of the salt. I should have paid more attention to that second drawn matrix, rather than looking at the first three elements and thinking "yeah I get it". I've finished the code for real now, and it's now a lot cleaner. Great kata and thank you both for the information.
You're welcome and congrats on solving!
This comment has been hidden.
Would you kindly format the code so it would be actually possible to read (see how).
This comment has been hidden.
This comment has been hidden.
I am able to pass all examples tests locally, even when switchin to an old version of node. But the random tests cannot set property ? of
undefined
which is not an operation I am actually performing.[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 ]
my output for the final example test.Can also provide my code if required, would love to know what causes this error.
A problem with your code is not a kata issue, please read this: https://docs.codewars.com/training/troubleshooting/#works-but-no
And yes, you should usually provide your code to check what's wrong. You're using global vars, they keep their value between tests.
This comment has been hidden.
This comment has been hidden.
What should be returned for empty array in Java ? I've tryed return new int[]{} and return new int[0] My brain is broken.
An integer array of length "0" should work. It is probably best practice to creat an appropriately sized integer array to begin with, rather than returning the integer array upon receiving an empty input array.
same problem, that's the only test that I couldn't pass
Eventually I found a solution. int n = array.length; if (n == 0 || array[0].length == 0) return new int[]{};
Сложная ката! 😭 Осилил, но не без гугла и пошаговой отладки
This may be usefull for you. You may want to try another computer. I have 2 computers, wrote the same code in vs code on both computers. My solution worked in one and didn't work in the other. I logon to code wars on both computers. Again my solution worked in one and didn't in another. I have no idea why this happened, if you do please explain. I should mention that it worked on one computer both in vs code and codewars but didn't work in the other, both in vs code and codewars.
Maybe only one of your computers is connected to the internet?
Actually the code runs on both but the answer is just incorrect on one of the computers. If the computer wasn't connected, I know codewars will not be able to run the code and will display an error.
The code that you submit to codewars doesn't run on your machine, though. It runs on a container server-side that codewars controls. The same code should produce the same results, no matter what computer it came from because the machine that actually runs the code is the same in both instances. Either you didn't submit the same code each time, or your code actually fails an edge case that can show up in random tests and you just happened to get a set of random tests that didn't test that edge case.
I think your second reason is correct. I tried the code again on the machine that it didn't work initially and it worked this time. It must have something to do with the test case.
i had a similar issue , but i realised that you're probably encountering an error in a loop, the code runs smoothly the first time but it could have an issue while running the loop again
So many different Javascript methods and properties are not working in this kata. flat() isn't supported, reverse() isn't supported, and now its even saying .length isn't working. I copied and pasted my function into the console and it has worked with every array I've thrown into it so I am 99% sure my code isn't buggy. Does it really not support something as basic as .length? This one has been SO much fun to figure out but it's frustrating how many fully supported methods and properties can not be used
reverse() and length work just fine, maybe you're not using them right? flat requires some Node version to work.
yeah nevermind i think I found some issues lol
what seem to be the issue? My code work well in vs code but not here! I spent 2 days working on this Kata
This problem is broken it works fine in csbin
No, the problem is in your code:
Print the input and debug your code.
"should return empty array given an empty array" - why does this mean that the array is not empty, but the first element of an input array is an empty array? lang Go []int{} -- empty array [][]int{{}} -- is NOT empty, but kata assumes it is, but the input itself is wrong: it is not 0x0 or 1x1, it is 1x0, but not NxN
The representation of an empty array is very counterintuitive. I know it has been brought up previously but just have to pile on and reiterate that.
Edit: for C++
Two of the random tests kept getting errors of 2 numbers out of sequence. I've been looking at my code for an hour, could the random test be possibly erroneous?
this one cant even into understanding the task. Have read the task 10 times, still confusion. What to do here is mistery.
look at the examples should return array with numbers in order of a counter clockwise spiral
The title in details "Snail Sort" is confusing. I spent 5 more mins to impl. a sort while it was not even needed.
This is clear.
It is said
Snail sort
because it is a way to rearrange a 2-d array.And, actually, not a sugestion.
Hi. Language is C#
The C translation seems to be bugged, I am unsure what output is expected.
Accessing the same element [0][0] of
mx
in a loop produces undefined behavior.There is no bug. You must be missing some edge case.
Ok so I figured it out. I thought that the function expected
outsz
as the output, but it simply expects you to set it, and to allocate your own array and return it.I think this could be made a lot clearer.
I added a note in solution set up.
This comment has been hidden.
.
It adds random tests.
Please review and approve, thanks !
.
D translation
Hi! First, thanks for this kata.
What about to modify this kata and instead of (n x n) size array, provide with (n x k) size array
(would be more challanging :) )
No. This would be a different kata, and would invalidate all the submitted solutions. It won't be done.
As akar-0 said, it needs to be a different kata. So you can create your own kata
Snail (hard version)
that provides a rectangular array.Очень классная ката!
When using JavaScript, it keeps throwing errors completely unrelated to my actual code ( cannot set property ? of
undefined
when I'm not even making such operation and the column index is pointing to whitespace. )That error comes from your code, it's not a kata issue. Debug your code, moving up your
console.log
.This comment has been hidden.
I put my fully passing and submitted solution into the leetcode version of this and discovered it wouldn't pass the testcase:
I was outputting
[7,9,6,undefined]
I'd suggest adding this to the test suite.
Edit: Javascript in case this isn't clear
This comment has been hidden.
Ahh okay, it seems as though the website was just being picky and I just assumed it was because my algo was inefficent. reloaded the web page after a bit and tried it again and it worked! This Kata was a pain but really fun :)
My java solution handles all of the tests, except for the empty array test. When array = { {} } it complains about indices. Any advice for how to determine if the array is empty like this?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
I googled basic java stuff bc I forgot a lot of stuff but nothing specifically related to the problem does that count 😂
Function pop() on JS isn`t work. Please, add it
It works fine (when used properly), not a kata issue.
Function 'flat' on JS is not work in this IDE!!!! Please, make something with it, if you can.
that's because of this. Closing, because it's sort of a duplicate issue.
This thing is not working! At least on c# i have checked all of the numbers in sequence, found no difference, but still got an error PrintScreen https://dropmefiles.com/XBg52
The error code is : System.IndexOutOfRangeException : Index was outside the bounds of the array. i can provide my code
its just unbelievable! here is another example https://dropmefiles.com/BoVlq
What is not working is your code and this is not a kata issue. Your screenshots are unusable. Please don't raise issues so lightly. You can refer to the documentation: https://docs.codewars.com/training/troubleshooting/
This comment has been hidden.
If your code throws an error you don't get any test log, the tests just crash, so you can't compare anything. I'm not going to debug your code for you. More than 2000 persons solved this kata in C#, it's obvious the out of range error is coming from your code, what kind of magic could throw an error from nowhere?
Why my vs studio dont throw me an error, but kata do?
Probably because you don't test it with the same input. Something else: use markdown format tags to format code when you post it, or it's unusable. And provide your complete code if you want some help. All this is explained in the documentation, I gave you the link, please read it.
I got that type of error when I treated my inputs as multidimensional array than jagged array
I have "Test Crached" but in Dev C++ 5.4.2 it's OK. is't write "signal 11", but IDK why
Please see if you find anything useful in the Troubleshooting FAQ, especially in this section: https://docs.codewars.com/training/troubleshooting/#c-c-signal-11---sigsegv
Amazing kata <3
Very pleasant kata.
Hi, my golang solution passes all tests except random ones. However, I checked with a size of 6x6, it works. Are there any problems with kata tests on golang?
It's a very good kata, but I find weird that a len 0 array gets represented as [ [ ] ] since that would make a 1 x 0 grid, yet the description clearly states that it's an n x n grid.
Yes, I know it says at the bottom how it's represented. It still makes no sense.
I had to make an additional if statement to my solution so it would pass the empty array case, if it was just [ ] (0 x 0) it would've passed without the need of an additional if, as my solution (and many others from what I've seen in other comments) works for all grids n x n, what the kata asks for.
I'm rating it as somewhat satisfied instead of very satisfied because of that. I'll rate it very satisfied if it get's changed.
This comment has been hidden.
Hi, You should create a new array, store the result in it and
return
it instead of directly printing the result.Ooo that makes sense ! Thanks for the fast reply :)
I don't think the "Empty Test" should be in test cases. In that test case, the input array is
[[]]
, which is an 1 x 0 matrix. It doesn't fit to the description of input array is n x n.It has already been pointed out and answered several times. It's explicitly in the description, it's a design choice, and however, after more than 40 k completions it won´t be changed.
Ah, that's my mistake. Anyway, thank you for the reply.
Agreed
This comment has been hidden.
Your code is failing some tests with a single number in it, and the other are just logs and your code returns the right result.
Not an issue.
This comment has been hidden.
I found a mistake in myself that the tests do not cover after I made a kata, the task is interesting, but the tests can be finalized
Node v18 is enabled now.
Kata does not support the method flat(), unfortunately. You give hard tasks, but do not support new tools yourself. This kata should be removed
That's no reason to remove a kata, and a lot of people solved it without it:
You could ask for a Node version update instead, that'd more understable.
I did it anyway, but I want to use normal methods, not old outdated
I already told you the normal procedure to ask for a Node update (even posted the issue for you). Now you have to wait for someone to update the kata and enable newer versions. No kata will be removed for that reason.
I'm curious about how flat() would make you life easier in this kata.
This comment has been hidden.
No, they're not.
When reporting an issue, be as detailed as possible. What is broken? What's your code?
There is an exception when I'm trying to return an empty array (Array.Empty() or new int[0])
Log [[]] should be sorted to [] System.IndexOutOfRangeException : Index was outside the bounds of the array.
C# if (array[0].Length == 0) return new int[] {};
Thanks. It works
COBOL translation, please review carefully.
approved
what(size_t *outsz)is used for ??
a pointer which you should set as the length of your return array
Hello, please update golang version or provide returning of nil slice, when i try to return an empty slice golang panics
This is not a kata issue, but due to a particular behaviour of Go. You need to initialize your slice with
[]int{}
, this way it'll benil
if empty. It's the same thing in most Go katas.Problem solved. Thx Also somehow at empty slice test case len(snailMap) > 0 but len(snailMap[0]) == 0
Working in Python, so I don't know if this issue affects other languages.
There is a test case for
[[]]
but this is actually a malformed input, representing a1 x 0
array ie. notn x n
.Consider
[[],[]]
and[[],[],[]]
which are2 x 0
and3 x 0
; an array which is zero in one dimension can be nonzero in another dimension.The correct representation for a
0 x 0
array, which I think the test case should be changed to, is simply[]
.Last line of the description.
Thanks for your reply, though I was already aware of that note. Obviously I'm new to this site and don't know how things work around here, but I'd hope that, before deciding to raise an issue with a kata, most people would feel obliged to read its full description and trust in others to have done the same.
To address the content of your reply, I'd suggest that simply asserting a statement does not make it true.
If you can find the time, please try this little challenge:
Under the scheme where
[[]]
represents the0 x 0
matrix, what representation does the1 x 0
matrix have?That's a design choice, the tests only use square matrix, n x n, the only exception is when n = 0, the author chose to use
[[]]
to represent that, maybe because he wanted to keep the idea of 2 dimensions. It's stated in the description and your code should resolve that.Of course it was a choice, but choices don't need to be permanent. In an open community like this one, we should be able to examine the reasoning behind them and perhaps reconsider them.
Also, since the result was an incomplete representation with an exception that had to be spelled out in a post-post-script (with a typo) and added nothing of interest to the problem, I suspect this particular case was more of an accident than a choice.
I think that's enough said about a detail of a random programming problem on a random website. Feel free to post another defensive reply but I encourage you to look at your motivations for doing so beforehand.
So what you're asking for is to invalidate existing 40k solutions to please you. Not saying anything, but I don't think it's gonna happen :P It may or may not be correct, but it's too late to change it now.
It's been discussed before, read it: https://www.codewars.com/kata/521c2db8ddc89b9b7a0000c1/discuss#5da990697b24c4001e188501 it wasn't an accident, it was the kata's author choice. Too late, unfair (it'll probably render all current valid solutions invalid) and useless to change it. Cheers.
Haskell random tests broken
Tests are fine. Must be a problem with your code. Please provide a clear example when raising an issue.
With Python I get many test error messages regarding 1D arrays, such as this one:
… but this case is not defined in the instructions (only 2D arrays are mentionned). I suppose the correct result for 1D arrays is a by-product of a specific solution?
looks like you didn't understand what the tests feedback is giving you.
go read those:
Thanks for your answer.
You're right: my post-lunch brain crash was quite strong and I misinterpreted the message, which is "the ouput of your function should equal this value", not "with this input your function should return this value". My (very) bad.
This comment has been hidden.
Read this: https://docs.codewars.com/training/troubleshooting/
You're using a couple global vars there. Try calling your code in those compilers more than once.
This comment has been hidden.
Use spoiler flag next time.
My bad, thanks.
Well, to answer your original question, that seems a good idea (though it may depend on the language you use, which makes it more or less difficult), and that's how I personnally solved this kata in several languages (but in others I did it only parsing the array, and not destructuring it, which is a better and more instructive practice, IMO). Now, it's easy to get lost and/or do something bad so that it eventually doesn't work. Hard to help you without knowing more. You should consider carefully the tests that fail, print stuff to check the progress of your output... In one word: debug it.
I get
TypeError: Cannot read properties of undefined (reading '0')
when trying to runy < array.length - 1
What's wrong and how could I fix it?
Thanks!
The message is explicit (please specify your language when posting a question). You try to get a property (length ?) on
undefined
, which is not correct. Probably your array isundefined
, you need to debug it. This may help: https://docs.codewars.com/training/troubleshooting/yeh... nope.
This comment has been hidden.
C#
SnailTest2Empty Log [[]] should be sorted to [] System.IndexOutOfRangeException : Index was outside the bounds of the array. Stack Trace at SnailSolution.Snail(Int32[][] array) at SnailTest.Test(Int32[][] array, Int32[] result) at SnailTest.SnailTest2Empty()
NOTE 2: The 0x0 (empty matrix) is represented as en empty array inside an array [[]].
I don't understand the return format at zero. If I return int [1][] or emty int[], I still don't pass the test.
int[0]
This comment has been hidden.
The tests in java are 100% broken. I pass the basic test with 9 elements and then the rest of the tests fail even thought my results are absolutely correct even for the corner cases {{}} and {{1}}. And the test spits some nonsense like "array lengths differed, expected.length=16 actual.length=9" eventhought that's not the case and "actual.length" is different every time I run the same test.
No, it works, just passed it:
Read this: https://docs.codewars.com/training/troubleshooting and check your code.
This comment has been hidden.
Cause I'm new to the site and am still quite disoriented. Anyways, I did some refactoring of my code and it passed, however I think the tests are not quite 100% there since both my versions returned exact same results. Logic dictates the answer should be the only fact that matters for passing the tests not how you get to it.
@asthesus kata's Discourse is shared for all languages, so, what do you mean with:
@Chrono79 Obviously I was assuming the category selection had some effect on this page.
It's unfortunate there is no way to delete a reply.
This comment has been hidden.
This comment has been hidden.
Very fun kata, tried it as a rookie in the past, but now it wasn't too bad.
could you please fix your broken test cases [[953, 579, 241, 809, 554, 782, 1, 756, 846] [91, 833, 225, 143, 469, 42, 140, 805, 848] [811, 256, 613, 444, 490, 974, 159, 196, 389] [362, 408, 480, 854, 52, 829, 777, 189, 791] [524, 623, 269, 527, 661, 912, 167, 76, 802] [2, 296, 308, 819, 597, 242, 26, 808, 476] [489, 537, 690, 630, 601, 275, 907, 953, 413] [222, 444, 162, 102, 762, 262, 808, 507, 728] [324, 946, 41, 540, 396, 205, 444, 562, 497]] should be sorted to [953, 579, 241, 809, 554, 782, 1, 756, 846, 848, 389, 791, 802, 476, 413, 728, 497, 562, 444, 205, 396, 540, 41, 946, 324, 222, 489, 2, 524, 362, 811, 91, 833, 225, 143, 469, 42, 140, 805, 196, 189, 76, 808, 953, 507, 808, 262, 762, 102, 162, 444, 537, 296, 623, 408, 256, 613, 444, 490, 974, 159, 777, 167, 26, 907, 275, 601, 630, 690, 308, 269, 480, 854, 52, 829, 912, 242, 597, 819, 527, 661]
At index 25 it is expected result to contain the number 2 , whereas from the given array it does not even contain 2
Look again, and the index value for 2 is 27.
Last Java test is broken [[]] should be sorted to []- what it says i'm returning an empty int array, which it expects, but it keeps failing Please fix it, i'd love to see other people solutions
maybe the reason is 'it give you a int[[]]'
At first it looks very scary. But think easy. HINT: think it like CSS box model.
hmmm.. thanks for the tip! i have no business trying a 4, but im gonna try it.
There are no tests before submitting which is a shame :)
For which language? I just checked, and all languages seem to have sample tests.
For JavaScript the test appears empty. It looks like this:
describe("Solution", function() { it("should test for something", function() { // Test.assertEquals(1 + 1, 2); // assert.strictEqual(1 + 1, 2); }); });
That's strange. Try hitting the reset button (it will remove your solution to initial state, so you should copy it)
that worked! Thanks
Can't even get started here because an if statement for array === [ [] ] never fires... anyone know why this is?
I'm passing in exactly what console.log spits out and it never runs the block.
Because you can't compare arrays like that in JS, the references are different in this case.
Not a kata issue.
What's the correct way to check for this case then?
You should look it up on some search engine :P One way is to use JSON.stringify() to convert an array/object to string representation, and strings you can compare with ===
The easiest is probably to use JSON.stringify as says B1ts. You can also try with Array.prototype.every()..., but with a 2d array (or more) it's not comfortable.
With javascript I get the error: "expected [ undefined ] to deeply equal []" how to avoid that? Shouldn't "return []" return the right value in that case?
Thanks!
The log tells you that you return [undefined] while it expects []. Logs are not always clear ;) Mark the question resolved if it's ok :)
Thanks, I think my problem is that I'm not able to return an empty array. Even with "return [];" I get [undefined].
Oops, Got it now. The problem was not going inside the if-statement where I returned the empty array.
can you give me direction where I must search error?
ya tuta
Kata took me 5 hours of drawing diagrams, finding patterns, and making algos to solve. Not one for the light-hearted!
After 6 hours making diagram on miror my brain just stopped. I well end later
Nice kata, simple yet challenging
aye seemingly simple but difficult to get just right
This comment has been hidden.
flat
needs Node 11+Not a kata issue anyway.
This comment has been hidden.
This comment has been hidden.
It's great that you ask for help and provide something to use: the error messages. They tell you that in line 11 you are trying to access a value at an index that is invalid for the container from which you are trying to access. I can't tell whether it is
jmin
that cannot be found insnail_map
or whetheri
cannot be found insnail_map[jmin]
, but that is how you can figure out what's wrong. I hope this helps you debug. Questions?You can see there also the test it fails:
test.assert_equals(snail([[]]), [])
Are all test cases n*n arrays? sure?
Yes.
Yes and no. Empty matrix is represented by
[[]]
arrayThis comment has been hidden.
Read Note 2 at the end of the description, also the error message you get.
What are you supossed to return in C?
I mean,Can I simply return an array ooor it HAS to be a matrix?
You return an array - a pointer to an array. When it is empty you can return NULL.
The messiest part about this problem stems from the fact that lists are zero indexed
This comment has been hidden.
One of random test faild. What is wrong? Traceback (most recent call last): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 66, in it_1 test.assert_equals(snail(a), solution(b)) File "/workspace/default/solution.py", line 14, in snail if snail_map[i][j] != None: IndexError: list index out of range
I understand that the index error, but why does everything else go? And what array does not pass it is not clear?
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
DAMN, had no idea this was even possible, i have an idea of what im supposed to do, but not quite sure how to code it yet. Will come back, even the concept of this Kata blows my mind, cheers.
I grinded out a long answer. It was satisfying when the tests passed.
This is the best Kata until now, need many hours to found the solution. But, when using the same algorithm for both PHP & JavaScript, only JavaScript can passed it while PHP get timeout.
Very good kata!
Doing the NASM version
There is no explanation what the parameters actually are
In the starting source there is
No explanation there, in the testcode or in the description.
I guess I will just have to experiment how it's supposed to be.
In the C-version, which I did solve, there's this comment.
Does it apply to the NASM version as well?
I am having trouble with the c version. I pass the 3 tests, and when I "attempt" it either fails the first test or passes 6 tests and then fails with a message that it crashed. I think I may not be returning the right thing for the empty [[]] matrix. I have tried returning 0, NULL, and int *out where out is set to 0. What are you returning for this case?
EDIT: I found the bug. Returning NULL for this case is fine. My bug was when I was allocating memory for the output vector I was using (cols)x(row) rather than (cols)x(rows), and row was a variable I had set to 0. It was working some of the time, probably overwriting unused memory or something, but then occasionally crashing it.
well, i feel like idiot to be honest i just can't get what exactly i have to do i mean i can't get what exactly the question is
There are some pictures there, what's exactly what you're not understanding from it?
i don't know pictures aren't even helping i am not getting the pattern
Check them again, you're given an array of arrays resembling an n x n matrix, you should return an array taking elements from the top-left, to the top-right, then from top-right, to bottom-right, etc. Like shown in those pictures.
It's a snail
First 4 kuy kata is done:) That's was a funny jorney for good ~5 hours,thanx for your work.
This was one hell of a good kata. Spend whole day and just got it done with python. Now watching other people's solution and I just realised I know nothing. People are just way smarter. Bookmarking this. Will come back later and practice other people's solution.
I know this feeling! I've been recently learning haskell and when i see other people's solution i'm like wow!, i'm still super bad at this. haha
Expected: [], instead got: [undefined]
I get this message. I run it anywhere else exept Codewars I get the expected value.
Pls ,specify language that you use.
I am using JavaScript
Oh,can't help then ,sorry
it means you returned an array with
undefined
in it, but the test was expecting an empty arrayThis comment has been hidden.
UndefinedBehaviorSanitizer:DEADLYSIGNAL ==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000042c8ba bp 0x0000021f7100 sp 0x7ffd9b0c80a0 T1) ==1==The signal is caused by a READ memory access. ==1==Hint: address points to the zero page. ==1==WARNING: invalid path to external symbolizer! ==1==WARNING: Failed to use and restart external symbolizer! #0 0x42c8b9 (/workspace/test+0x42c8b9) #1 0x425597 (/workspace/test+0x425597) #2 0x428213 (/workspace/test+0x428213) #3 0x4269ee (/workspace/test+0x4269ee) #4 0x426549 (/workspace/test+0x426549) #5 0x42613b (/workspace/test+0x42613b) #6 0x42c4f5 (/workspace/test+0x42c4f5) #7 0x42585d (/workspace/test+0x42585d) #8 0x7fea57989bf6 (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6) #9 0x404589 (/workspace/test+0x404589)
UndefinedBehaviorSanitizer can not provide additional info. ==1==ABORTING
I dont't understand why I have this error
Getting the same error although visual assist runs it just fine.
SEGV indicates that there is a segfault. If you are programming in C++, then I suggest to use std::runtime_error to gather information about your problem. Also note that the test case {{}} is initialized as {std::vector({})}.
getting the same error, anyone know how to fix it?
Same thing. Based on shown tests, everything is fine locally. No exceptions are thrown.
Does your solution work correctly locally for input of
{{}}
?You can post your code here (see how: https://docs.codewars.com/training/troubleshooting#post-discourse) so someone could take a look.
Javascript flat() function is prohibited?
No, but it won't work if the kata hasn't Node 12.x enabled. It's already asked and answered below.
This comment has been hidden.
No, and please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution
that seems more like a 1x0 matrix
it was really challenging! thanks for the kata
I don't understand return (int *)(*outsz = 0);
What is return? Pointer to allocated memory? Or does function need to change some given array?
Same problem, I don't get it (that's the kind of things I've found in other katas, I've never understood). My code returns the correct array but no idea of what I should do with this outz pointer:
Edit: Hm.... After some hazardous tweaks I eventually could solve this, assigning to the pointer the size of the returned array... Still no idea of what's the use of that X)
(I consider the answer is solved, ask for details if needed)
That's how output parameters work in C, and when you return an array, you usually have to return its length. Otherwise, how would caller know how long array was returned?
Not that it's necessary in this particular kata (the size of the output array can be easily determined from the size of the input), but it's general approach in C for functions returning arrays.
Omg, it's not a spoiler)))
Sounds good, but not clear for me.
Function "returns" size of array. Ok.
But how can cast operator take this and returns like pointer?
This comment has been hidden.
Please review Perl translation: https://www.codewars.com/kumite/602626732b35dd001ad4e5ec?sel=602626732b35dd001ad4e5ec
I really liked this kata, thank you!
This comment has been hidden.
Already answered below.
This comment has been hidden.
Because you used a global var, and it keeps its value when your function is called more than once. Please, I know you said you read it, but read this again: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution it says there also how to post code with markdown formatting, etc. We wrote it so we don't need to repeat the same over and over.
thanks
No problem, the answer to your problem was there too btw:
you are right. i fixed my code using a while loop instead of recursion. it worked fine. thanks again for your time.
You're welcome, recursion is a bit tricky until you get the hang of it. Now that you solved it without it, see other solutions that use recursion and analyze how they work.
sure thing. actually i found a couple of amazing solutions that i didn't think of. this website is awsome
This comment has been hidden.
Please don't create issues when after more than 11k solutions in Python, the problem is unlikely in the kata. Use
Question
as the github page says and post your code.Without a way to reproduce your claim, this is "trust me bro". And that trust is usually not deserved with a comment to this effect. There's no way that others can know that this is different. It probably doesn't lead anywhere either even if you're right, because that still doesn't say what the problem is.
i posted my code in the questions section
I mean, there are different labels for posts here, it's documented in the github page, I'll copy and paste what it says about it:
Nice Kata, but isn't this rated a little too high? I think this could be 5kyu or even 6 kyu.
Stop showing off pls (:
Thank you Steven, it was a big satisfaction, when I finally solved your kata.
This comment has been hidden.
Max Buffer Size Reached (1.5 MiB). Can someone please explain to me why this is happening?
you are probably printing sth. check your code
SO HARDDD :(
something has got to be wrong with the attempt-tests!
The first time I ran an attempt, 0 tests passed. Just for curiosity, I re-ran the tests (without changing any code) and 5 tests passed. I kept re-running and at some point 10 tests passed.
Please read this: https://github.com/codewars/codewars.com/wiki/Troubleshooting-your-solution#kata-bug
that's probably because of the random cases. your code might return a right answer in a specific situation, for example when the matrix is empty and it passes the random tests that generate such matrices
Interesting kata, biggest point to note when testing, it's (n x n) and not (m x n)
yeah I know. My code works fine on codeblocks but here I get an error. Thanks for replying though.
This comment has been hidden.
Seems like you forgot to handle edge cases for
[[]]
, OP solved it though, closingNice work on this one Steven, Thanks for the experience of solving this kata.
hello, i couldn't solve this kata. what algorithm or tecnhnique should i learn in order to solve this kata?
and actually i am having trouble to study data structures and algorithms. would anybody give me some advices about how can i learn and practice data structures and algorithms please?
This comment has been hidden.
i think that'll work great, i will definitely try it.thank you so much!
Great kata, thank you )))
This comment has been hidden.
Do you remember when you started working on the kata?
I just checked and it looks as if you have indeed forfeited the kata at some point, I cannot check when exactly though. I can ask, but I am not sure if it is possible to find out. Your solution to this kata has not been flagged as a cheat nor invalidated by a mod, actually no mod were active at the time you completed it. Your solution to another kata was, but it's not related.
I can ask others for confirmation if you really want to, but on my side it looks like a forfeited kata.
Dang, I don't remember forfeiting this Kata at all. Guess I must have a long time ago and just forgot :/ is there no way to get points? Completely understand if so. Also for the flagged Kata, that solution definitely wasn't mine (so I completely expected it to get flagged); had no idea how to solve the Kata and just decided to look at the solutions and submit someone else's after reviewing it.
Also, I definitely remember reading this Kata in the past, I just never got around to actually doing it. It's definitely plausible that I forfeited a long time ago, looked at the solutions, and just forgot about it.
I can see that you started working on the kata in september, what would be a couple of months ago. I can also see that you are not eligible for points for it, but not because of a cheat or invalidation. All of this means that at some point, possibly long time ago, you have apparently forfeited on the kata.
It's not possible for you to get points for this particular kata anymore. Do not worry though, because there's plenty of blue kata for you to train on.
One more note though:
Please remember that when you struggle with some task and want to see solutions, forfeiting is the designed, safe way of achieving this. "submitting someone else's solution and expecting to get it flagged" may not always work as intended, particularly if points were awarded in the process. Not recommended to make it a habit.
This comment has been hidden.
You should read the description better, the cause of all this "strangeness" is explained there. This kata has some... interesting understanding of what "square of size 0" is :(
You should also read about code formatting: https://github.com/codewars/codewars.com/wiki/Markdown-Formatting#block-code-formatting
Prolog translation kumited.
Has anybody solved this with a Rust solution?
I ask because I suspect the testing mechanism is broken.
For example:
The
right
here corresponds with the expected solution, but clearly it's incorrect if you look at the input.I made the Rust translation a while back. It seems to have been reviewed and approved recently. Thanks to your comment I was able to find a major bug in the test suite.
Just now I attempted to fix that bug. Does the issue still persist?
Apparently not. Closing.
Nice Kata
There is an issue with either the notes or the tests.
Test.assert_equals(snail([[]]), [])
Per Note 2, the result should be [[]], though [] makes more sense. I recommend editing Note 2.
No, it is ok, because the expected result is always a 1-dimensional array. The note is about the input.
You're right - my mistake.
This comment has been hidden.
Not if the kata hasn't Node 12.x enabled.
Ok, thanks.
This comment has been hidden.
Your append fails in this case:
yeah, thx, i fixed that, but it still doesn't work with a lot of random tests
for the rest of the test cases did you TLE? because seems like the parts that go "array[a][b] not in res" is going to be O(n-squared) time complexity due to searching through the whole array every time you try to put something in.
if its TLE, try to think of a faster algo?
OP solved it, closing