7 kyu
Linked Lists - Get Nth Node
2,135 of 5,998JDeBolt
Loading description...
Linked Lists
Data Structures
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.
This comment has been hidden.
no that is.. correct. it'll cause a throw when a throw is asked for which otherwise wouldn't happen.
if you look at my (python) solution though, THAT is incorrectly passing, as it doesn't return the nth node, but a node containing the value that the nth node contains
this issue probably applies to most if not all languages.
one might argue that it's out of scope. it's not. they ask for the node, not the data. if they wanted the data they should have asked for the data. and then test for what's asked for. nor is it a nitpick about python's concept of identity. this function spec is similar to
drop
, but that is not at all what my solution does.bonus issue: poor formatting in python's initial code. never fails to annoy me.
WAT. No way it causes the correct type of error?
Unspecified. The choices of excption type look strange across the board. java accepts any exception type according to description (haven't looked at tests) and C# doesn't use IndexOutOfRangeException which I assume is more appropriate and PHP might have something like that exception too instead of InvalidArgumentException .. OutOfRangeException looks to be a thing. Ruby accepts both throw and raise for some reason.
Some languages return the data which seems more in line of the intent of the kata, while others the entire node, but don't bother checking if it's the correct node.
This is fine.
As a bonus this solution works for infinite lists and negative index because it'll hit recursion limit. (doesn't work as great for high indices though)
I made two forks that address mutability:
Both expect specifically
IndexError
. Unclear whether that change should be made though. It's not necessarily incorrect by codewars standards to raise an unspecified exception, and this breaks the vast majority of solutions, including ones that I would consider completely correct with the current spec.I'm getting an exception error when running the code in the browser, but I took the same code and ran it in the intellij and the result is different. Anyone else with this problem? (JAVA)
Python fork
approved.
CoffeeScript translation
Java: I think we have return a Node
This comment has been hidden.
throw error?
Java: Expected and actual are swapped in assertions.
fixed
COBOL translation, including random tests.
approved
no random tests in any language
But C? ;)
C#: tests throw warnings:
Additionally:
Node
class is buggy because it ovrridesEquals
but notGetHashCode
.fixed
This should be removed from the description, this is just confusing if you are not training on the katas in order
" (may be in beta)" should be removed too.
done
PHP, Java: the kata expects the node data while the description and other languages require a node.
Swift: we must be provided some information about the exception to throw.
I see that a couple other people were asking about throwing an exception in JS. I understand WHEN to throw the exception, but I don't understand WHAT I need to throw to get this to pass. If I throw a string it fails, if I throw a new Error() the whole Kata errors out....not sure what you mean by 'exception'
throw 'whatever'
works.throw new Error()
works, but your current code only does this... Please read the description.C translation
Approved :)
JavaScript: The instructions aren't clear as to how to handle invalid index value and null linked list. I'm throwing an Error on those cases, and it's not clear why I'm still not passing tests. Would be great if instructions were either clear, or not in JavaScript's list of Katas so that us JavaScript users don't waste their time trying to decipher the issue after handling everything properly...
The index should be in the range [0..length-1]. If it is not, GetNth() should throw/raise an exception
NO!
For Swift: The return type cannot be
nil
, so why not make itNode
instead ofNode?
?It can be nil if you calling function without do-catch. For instance: var node = try? getNth(node, index)
Duplicate suggestion below
For Java: I believe the requirement of the function is to return a
Node
instance, not thedata
of an instance.That's what the kata asks to do.
I misread your message, I thought you said the contrary. I raised an issue for similar reasons with PHP, I'll add Java to it.
I have two issues with this Kata (Ruby version)
very bad explanation and code quality (especially tests) java version of this kata
Regarding the Java problem, the Kata descrition says that the function should return a Node, but this kata's code skeleton and tests expect an integer (i.e., the node's data). Should probably be corrected.
Hi, I made a C# translation for this Kata, please review and approve.
approved at some point
This comment has been hidden.
This comment has been hidden.
This comment has been hidden.
Passed 4 cases while testOther failed. Don't get it what is this.Any hint.
PHP Translation Kumited - please accept :D
Approved some time ago
This comment has been hidden.
This comment has been hidden.
NOw they don't
Hi! i am throwing exception as throw new Error() and i am printing it in console.log in catch. But i could not pass the test case for exception handling.
This comment has been hidden.
The same is happening to me.
OP solved it, closing
This comment has been hidden.
Raising Exception should work. There is probably some other fault in your code. Try printing the input to your function to see what value your function is tripping at.
try throw new Error() instead of raise Exception()
Thank you guys. I was returning the data value instead of the node object.
A lot of people are throwing stack overflow errors when index < 0. This technically passes because an error is thrown, but it would be better if the test made sure they failed early and didn't blow out the stack.
Could use some random tests.
Ruby translation kumited.
@JDeBolt, could you provide some feedback on #160, in case you didn't get a notification (apart from @NaMe613's comment).
Approved some time ago
Why not giving link to the other pre-requisities katas in the description?
Great feedback, thanks. I've updated all of the descriptions with links to related kata.
The descriptions says "...and returns the data value stored in the node...", while the unit tests expect the node to be returned (in JavaScript kata).
Ahh, nice catch. Fixed, thanks!
Can this issue be marked as resolved? It doesn't look like I can mark this as resolved. Thanks.
Sorry for nitpicking, but... The kata's name is "get nth node", not the node value. I suggest to modify the kata details instead of the test cases (ie. to describe that you expect to find the ntn node and let the test cases verify the function returns the node). The test case modification you made just invalidated some good solutions that had been already submitted. Anyway, it's your kata and the discrepancy between the details and test cases is resolved. If I need to resolve it somehow (ie. to click somewhere), please tell me. This was my first comment here on codewars and I don't know the good customs yet.
No worries. Thanks for the feedback. That's why this is beta :)
I reverted the return value back to the actual node. Unfortunately, this has caused some of the Python solutions to break, but I think it makes more sense to return the node rather than the data.
I think you may be able to reply and check the "mark issue as resolve" checkbox?
Thanks for the modification. I marked this kata as "ready". As I didn't label my original question as an issue, I believe there's nothing to resolve (at least I don't see the possibility in the reply).
Thanks! Yes, it looks like the issue was resolved, possibly by someone else.
Hi! Nice kata.
The only down side is that you are not following the rules for convetional naming of the Python code.
Should really be:
For clarity, you might want to consider defining your
Node
class before the method under test too.Cool, thanks. Fixed. I couldn't find any language style guides on CodeWars, so I was using the naming conventions that I use at work. I should have known this because the CW Python test framework uses underscores. I'll stick with the underscore function/method convention.
Can this issue be marked as resolved? It doesn't look like I can mark this as resolved. Thanks.
Done