Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Python fork adding random tests.
The type hints in the Python setup code are misleading. Since
None
is a valid argument for thenode
argument in both functions, the type hint should benode: Node | None
instead ofnode: Node
.Also, since you're using type hints, you could make the
Node
class generic:Then
count
could look like the following:done
import
s in initial code and testsNode
class in the initial codeThe tests in Python do not use the definition of
Node
from the solution. The class definition should either be removed or commented out of the setup code or be tested in the test suite.BTW are you on Codewars Discord? I wanted to ask one thing, could you please poke me with a DM if you are there?
fixed
thanks for the explanation. in hindsight it was foolish of me, of course the hash code should change if
data
is mutated. i changedGetHashCode()
to simplyreturn Data.GetHashCode();
.The contract of
GetHashCode
is based on two rules:The above rules enable a couple of possible patterns of implementing sane
GetHashCode
:GetHashCode
andEquals
,GetHashCode
use a subset of data used inEquals
,Your implemntation of
GetHashCode
is good in a way that it conforms to above rules: it usesData
, which is also used byEquals
. IfData
of two elements will be different,Equals
will returnfalse
, and also hash codes will be different, which is good (it might be not great for other reasons like performance, but it is at least not against rules).The "hash code should stay constant for the lifetime of the object" is something different, it's a pattern/rule not enforced by
GetHashCode
itself, but an advice which helps to avoid some nasty bugs. Generally it would translate to: "avoid hash codes calculated from mutable fields". However in case of this challenge and theNode
class, it is not possible: theData
field is mutable, and it's a part of equality. IfData
changes, then its equality with other instances might also change. This is why precalculated and cached hash code becomes wrong whenData
changes.For the
Node
class, this means that one possible implementation forGetHashCode
isreturn this.ToString().GetHashCode()
(uses the same data asEquals
, but not great performance wise), another isreturn Data.GetHashCode()
(easy to calculate, might have poor distribution but should be good enough for a kata), but should not be cached because it will be wrong whenData
changes.C# warning in the preloaded section:
(OP was solving in JavaScript)
your implementation of
count()
has a bug, it skips the last element of the list because yourwhile(head.next)
is not entered for the penultimate element. not a kata issue.I added the following
GetHashCode()
implementation in preloaded, which cleared the warning. I don't know whether it is idiomatic enough though, I read that the hash code should stay constant for the lifetime of the object, which is not guaranteed here if we use theNode
fields for the hashcode computation, as they are mutable. I tried to work around that.This should not be a 7.
Same issue here, I wonder why it is still in draft...
Additionally:
Node
class is buggy because it ovrridesEquals
but notGetHashCode
.C# test suite generates a warning, and the preloaded
Node
class is missingGetHashCode
.Loading more items...