The exercise is Simple Linked List. The core of the issue is specific to C# track, that’s why I put it in Programming > C#
part of the forum. But now that I looked more into the issue there are more interesting quirks to it, some of them might be of interest to people maintaining problem specifications / stories.
Problem 1 - C# and IEnumerable<T>
implementation
The task requires implementation of IEnumerable<T>
interface - a very common thing C#. But the test for it is not adequate.
[Fact]
public void Implements_enumerable() {
var values = new SimpleLinkedList<int>(2).Add(1);
Assert.Equal(new[] { 2, 1 }, values);
}
A common implementation is something like this:
public SimpleLinkedList<T> Add(T value)
{
/* this is incorrect but enough for the tests to pass */
this.next = new SimpleLinkedList<T>(value);
return this;
}
public IEnumerator<T> GetEnumerator() {
var current = this;
while(current != null) {
yield return (T)current.Value;
current = current.Next;
}
}
And the above test will pass, however if we extend it by just adding one extra number it will fail, as the GetEnumerator()
will return the first and the last element from the list.
Pull request with a proposed fix (2098).
Problem 2 - dissimilarity between instructions and tests
The instructions contain this fragment:
This variant of linked lists is often used to represent sequences or push-down stacks (also called a LIFO stack; Last In, First Out).
However, the tests require First in, First Out structure. In many cases, including the snipped shown above the tests expect FIFO behaviour which may be confusing because of the dissimilarities between instructions and expectations.
I don’t see an obvious, low impact solution.
- Change description - lower impact on existing solutions but will go against current efforts to standardise the problem statements
- Change the tests to expect LIFO behaviour will invalidate all submitted solutions
To make it more obvious Remove
method could be added so in tests it is obvious where after adding 1, 2, 3
in that order whether 1
or 3
should be returned first.
Problem 3 - hints with malformed link.
The instructions.append.md
has a malformed link.
Pull request to fix the issue (2099).
Something else to consider
Some of the other language tracks I have looked at implement a different type of simple linked list, one that consists of the wrapping type and a node type. This allows for more robust solutions. Adding and LIFO behaviour without problems with hanging references because the external reference is always to a wrapper not to a specific element that might be removed.