-
-
Notifications
You must be signed in to change notification settings - Fork 374
Simple list with LIFO behaviour #2102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ErikSchierboom
merged 20 commits into
exercism:main
from
michalporeba:simple-list-lifo-way
Apr 18, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9fa8e10
sample lifo list implementation
michalporeba 1d4d50f
it is push and pop, not add
michalporeba 6042c38
new set of tests for the new take
michalporeba bf21ddb
updating Example.cs to pass the tests
michalporeba 4eec924
proposed tasks to lead the implementation
michalporeba 2687eab
count should be encapsulated
michalporeba 4a915f6
reflection instead of direct constructor call
michalporeba 1d4e0bd
back to the starting point
michalporeba 510d68c
you need to implement this function
michalporeba f78c542
removing tasks for now
michalporeba fac82db
some instructions to guide implementation
michalporeba 1853710
Merge branch 'exercism:main' into simple-list-lifo-way
michalporeba bd9517b
Update exercises/practice/simple-linked-list/.docs/instructions.appen…
michalporeba 486e659
Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs
michalporeba 990df6c
Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs
michalporeba 97a7736
Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs
michalporeba 37c0b22
Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs
michalporeba eeee623
Update exercises/practice/simple-linked-list/.docs/instructions.appen…
michalporeba 892d55c
Update exercises/practice/simple-linked-list/.docs/instructions.appen…
michalporeba 945cd78
Update exercises/practice/simple-linked-list/.docs/instructions.appen…
michalporeba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 9 additions & 3 deletions
12
exercises/practice/simple-linked-list/.docs/instructions.append.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| # Hints | ||
| # Instruction append | ||
|
|
||
| This exercise requires you to create a linked list data structure which can be iterated. This requires you to implement the IEnumerable\<T> interface. | ||
| For more information, see [this page](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1). | ||
| This exercise requires you to create a linked list data structure which can be iterated. | ||
|
|
||
| 1. Implement the `Count` property - it should not be possible to change its value from the outside | ||
| 2. Implement the `Push(T value)` method that adds a value to the list at its head. | ||
| 3. Implement the `Pop()` method which removes and returns a value from the head. | ||
| 4. Add a constructor to allow initialisation with a single value, or with an interable | ||
| 5. Implement the `IEnumerable<T>` interface. For more information, see [this page](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1). | ||
| 6. Ensure `Reverse()` method is available. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,53 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
|
||
| public class SimpleLinkedList<T> : IEnumerable<T> | ||
| { | ||
| public SimpleLinkedList(T value) : | ||
| this(new[] { value }) | ||
| { | ||
| private class Node { | ||
| public T Value { get; set; } | ||
| public Node Next { get; set; } | ||
michalporeba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private Node head; | ||
|
|
||
| public SimpleLinkedList(IEnumerable<T> values) | ||
| { | ||
| var array = values.ToArray(); | ||
|
|
||
| if (array.Length == 0) | ||
| { | ||
| throw new ArgumentException("Cannot create tree from empty list"); | ||
| } | ||
|
|
||
| Value = array[0]; | ||
| Next = null; | ||
| public SimpleLinkedList() { } | ||
|
|
||
| foreach (var value in array.Skip(1)) | ||
| { | ||
| Add(value); | ||
| public SimpleLinkedList(params T[] values) | ||
| { | ||
| foreach(var value in values) { | ||
| Push(value); | ||
| } | ||
| } | ||
|
|
||
| public T Value { get; } | ||
|
|
||
| public SimpleLinkedList<T> Next { get; private set; } | ||
|
|
||
| public SimpleLinkedList<T> Add(T value) | ||
| public int Count { get; private set; } = 0; | ||
|
|
||
| public void Push(T value) | ||
| { | ||
| var last = this; | ||
| var node = new Node { Value = value, Next = this.head }; | ||
| this.head = node; | ||
| this.Count++; | ||
| } | ||
|
|
||
| while (last.Next != null) | ||
| { | ||
| last = last.Next; | ||
| public T Pop() | ||
| { | ||
| if (this.head == null) { | ||
| throw new InvalidOperationException("List is empty!"); | ||
| } | ||
|
|
||
| last.Next = new SimpleLinkedList<T>(value); | ||
|
|
||
| return this; | ||
| var value = head.Value; | ||
| head = head.Next; | ||
| this.Count--; | ||
| return value; | ||
| } | ||
|
|
||
| public IEnumerator<T> GetEnumerator() | ||
| { | ||
| yield return Value; | ||
|
|
||
| foreach (var next in Next?.AsEnumerable() ?? Enumerable.Empty<T>()) | ||
| { | ||
| yield return next; | ||
| var current = this.head; | ||
| while(current != null) { | ||
| yield return current.Value; | ||
| current = current.Next; | ||
| } | ||
| } | ||
|
|
||
| IEnumerator IEnumerable.GetEnumerator() | ||
| { | ||
| return GetEnumerator(); | ||
| } | ||
| IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,8 @@ | |
| "bressain", | ||
| "j2jensen", | ||
| "robkeim", | ||
| "wolf99" | ||
| "wolf99", | ||
| "michalporeba" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 78 additions & 44 deletions
122
exercises/practice/simple-linked-list/SimpleLinkedListTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,83 +1,117 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| public class SimpleLinkedListTests | ||
| { | ||
| [Fact] | ||
| public void Single_item_list_value() | ||
| [Fact] | ||
| public void Empty_list_has_no_elements() | ||
| { | ||
| var list = new SimpleLinkedList<int>(1); | ||
| Assert.Equal(1, list.Value); | ||
| var list = new SimpleLinkedList<int>(); | ||
| Assert.Equal(0, list.Count); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Single_item_list_has_no_next_item() | ||
| public void Count_cannot_be_changed_from_the_outside() | ||
| { | ||
| var list = new SimpleLinkedList<int>(1); | ||
| Assert.Null(list.Next); | ||
| var count = typeof(SimpleLinkedList<>).GetProperty("Count"); | ||
| Assert.True(count?.GetGetMethod().IsPublic); | ||
| Assert.False(count?.GetSetMethod(true).IsPublic); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Two_item_list_first_value() | ||
| public void Pushing_elements_to_the_list_increases_the_count() | ||
| { | ||
| var list = new SimpleLinkedList<int>(2).Add(1); | ||
| Assert.Equal(2, list.Value); | ||
| var list = new SimpleLinkedList<int>(); | ||
| list.Push(0); | ||
| Assert.Equal(1, list.Count); | ||
| list.Push(0); | ||
| Assert.Equal(2, list.Count); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Two_item_list_second_value() | ||
| public void Popping_elements_from_the_list_decreases_the_count() | ||
| { | ||
| var list = new SimpleLinkedList<int>(2).Add(1); | ||
| Assert.Equal(1, list.Next.Value); | ||
| var list = new SimpleLinkedList<int>(); | ||
| list.Push(0); | ||
| list.Push(0); | ||
| Assert.Equal(2, list.Count); | ||
| list.Pop(); | ||
| Assert.Equal(1, list.Count); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Two_item_list_second_item_has_no_next() | ||
| public void Elements_pop_back_in_lifo_order() | ||
| { | ||
| var list = new SimpleLinkedList<int>(2).Add(1); | ||
| Assert.Null(list.Next.Next); | ||
| var list = new SimpleLinkedList<int>(); | ||
| list.Push(3); | ||
| list.Push(5); | ||
| Assert.Equal(5, list.Pop()); | ||
| list.Push(7); | ||
| Assert.Equal(7, list.Pop()); | ||
| Assert.Equal(3, list.Pop()); | ||
| } | ||
|
|
||
| private static SimpleLinkedList<int> CreateSimpleLinkedList(int value) | ||
| { | ||
| var type = typeof(SimpleLinkedList<>).MakeGenericType(typeof(int)); | ||
| var constructor = type.GetConstructor(new Type[] { typeof(int) }); | ||
| return (SimpleLinkedList<int>)constructor?.Invoke(new object[]{ value }) | ||
| ?? CreateSimpleLinkedList(new int[] { value }); | ||
| } | ||
|
|
||
| private static SimpleLinkedList<int> CreateSimpleLinkedList(params int[] values) | ||
| { | ||
| var type = typeof(SimpleLinkedList<>).MakeGenericType(typeof(int)); | ||
| var constructor = type.GetConstructor(new Type[]{typeof(int[])}); | ||
| return (SimpleLinkedList<int>)constructor.Invoke(new object[]{ values }); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Implements_enumerable() | ||
| public void Single_value_initialisation() | ||
| { | ||
| var values = new SimpleLinkedList<int>(2).Add(1); | ||
| Assert.Equal(new[] { 2, 1 }, values); | ||
| var list = CreateSimpleLinkedList(7); | ||
| Assert.Equal(1, list.Count); | ||
| Assert.Equal(7, list.Pop()); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Multi_value_initialisation() | ||
| { | ||
| var list = CreateSimpleLinkedList(2, 1, 3); | ||
| Assert.Equal(3, list.Pop()); | ||
| Assert.Equal(1, list.Pop()); | ||
| Assert.Equal(2, list.Pop()); | ||
| } | ||
|
|
||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void From_enumerable() | ||
| { | ||
| var list = new SimpleLinkedList<int>(new[] { 11, 7, 5, 3, 2 }); | ||
| Assert.Equal(11, list.Value); | ||
| Assert.Equal(7, list.Next.Value); | ||
| Assert.Equal(5, list.Next.Next.Value); | ||
| Assert.Equal(3, list.Next.Next.Next.Value); | ||
| Assert.Equal(2, list.Next.Next.Next.Next.Value); | ||
| var list = CreateSimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); | ||
| Assert.Equal(2, list.Pop()); | ||
| Assert.Equal(3, list.Pop()); | ||
| Assert.Equal(5, list.Pop()); | ||
| Assert.Equal(7, list.Pop()); | ||
| Assert.Equal(11, list.Pop()); | ||
| } | ||
|
|
||
| [Theory(Skip = "Remove this Skip property to run this test")] | ||
| [InlineData(1)] | ||
| [InlineData(2)] | ||
| [InlineData(10)] | ||
| [InlineData(100)] | ||
| public void Reverse(int length) | ||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Reverse_enumerable() | ||
| { | ||
| var values = Enumerable.Range(1, length).ToArray(); | ||
| var list = new SimpleLinkedList<int>(values); | ||
| var reversed = list.Reverse(); | ||
| Assert.Equal(values.Reverse(), reversed); | ||
| var values = Enumerable.Range(1, 5).ToArray(); | ||
| var list = CreateSimpleLinkedList(values); | ||
| var enumerable = Assert.IsAssignableFrom<IEnumerable<int>>(list); | ||
| var reversed = enumerable.Reverse(); | ||
| Assert.Equal(values, reversed); | ||
| } | ||
|
|
||
| [Theory(Skip = "Remove this Skip property to run this test")] | ||
| [InlineData(1)] | ||
| [InlineData(2)] | ||
| [InlineData(10)] | ||
| [InlineData(100)] | ||
| public void Roundtrip(int length) | ||
| [Fact(Skip = "Remove this Skip property to run this test")] | ||
| public void Roundtrip() | ||
| { | ||
| var values = Enumerable.Range(1, length); | ||
| var listValues = new SimpleLinkedList<int>(values); | ||
| Assert.Equal(values, listValues); | ||
| var values = Enumerable.Range(1, 7); | ||
| var list = CreateSimpleLinkedList(values.ToArray()); | ||
| var enumerable = Assert.IsAssignableFrom<IEnumerable<int>>(list); | ||
| Assert.Equal(values.Reverse(), enumerable); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not something that students need to implement as they get it for free by implement IEnumerable, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they do it right. If they don't then the test will fail.