From 9fa8e109c2a473175caee80a0bd6ddfaa832050a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 29 Mar 2023 22:26:18 +0100 Subject: [PATCH 01/19] sample lifo list implementation --- .../simple-linked-list/.meta/Example.cs | 64 ++++++--------- .../SimpleLinkedListTests.cs | 80 +++++++------------ 2 files changed, 56 insertions(+), 88 deletions(-) diff --git a/exercises/practice/simple-linked-list/.meta/Example.cs b/exercises/practice/simple-linked-list/.meta/Example.cs index c7466ceeb3..e3b1f69314 100644 --- a/exercises/practice/simple-linked-list/.meta/Example.cs +++ b/exercises/practice/simple-linked-list/.meta/Example.cs @@ -1,63 +1,49 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; public class SimpleLinkedList : IEnumerable { - public SimpleLinkedList(T value) : - this(new[] { value }) - { + private class Node { + public T Value { get; set; } + public Node Next { get; set; } } + + private Node head; - public SimpleLinkedList(IEnumerable 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)) - { + public SimpleLinkedList(params T[] values) + { + foreach(var value in values) { Add(value); } } - public T Value { get; } - - public SimpleLinkedList Next { get; private set; } - - public SimpleLinkedList Add(T value) + public void Add(T value) { - var last = this; + var node = new Node { Value = value, Next = this.head }; + this.head = node; + } - while (last.Next != null) - { - last = last.Next; + public T Remove() + { + if (this.head == null) { + throw new InvalidOperationException("List is empty!"); } - - last.Next = new SimpleLinkedList(value); - - return this; + var value = head.Value; + head = head.Next; + return value; } public IEnumerator GetEnumerator() { - yield return Value; - - foreach (var next in Next?.AsEnumerable() ?? Enumerable.Empty()) - { - 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(); } \ No newline at end of file diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index 35a89822d2..9da2e463a2 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -1,5 +1,6 @@ using System.Linq; using Xunit; +using System.Collections.Generic; public class SimpleLinkedListTests { @@ -7,77 +8,58 @@ public class SimpleLinkedListTests public void Single_item_list_value() { var list = new SimpleLinkedList(1); - Assert.Equal(1, list.Value); + Assert.Equal(1, list.Remove()); } - [Fact(Skip = "Remove this Skip property to run this test")] - public void Single_item_list_has_no_next_item() - { - var list = new SimpleLinkedList(1); - Assert.Null(list.Next); - } - - [Fact(Skip = "Remove this Skip property to run this test")] + [Fact] public void Two_item_list_first_value() { - var list = new SimpleLinkedList(2).Add(1); - Assert.Equal(2, list.Value); + var list = new SimpleLinkedList(1, 2); + Assert.Equal(2, list.Remove()); } - [Fact(Skip = "Remove this Skip property to run this test")] + [Fact] public void Two_item_list_second_value() { - var list = new SimpleLinkedList(2).Add(1); - Assert.Equal(1, list.Next.Value); + var list = new SimpleLinkedList(1, 2); + list.Remove(); + Assert.Equal(1, list.Remove()); } - [Fact(Skip = "Remove this Skip property to run this test")] - public void Two_item_list_second_item_has_no_next() - { - var list = new SimpleLinkedList(2).Add(1); - Assert.Null(list.Next.Next); - } - - [Fact(Skip = "Remove this Skip property to run this test")] - public void Implements_enumerable() + [Fact] + public void Multivalue_initialisation() { - var values = new SimpleLinkedList(2).Add(1); - Assert.Equal(new[] { 2, 1 }, values); + var values = new SimpleLinkedList(2, 1, 3); + Assert.Equal(new[] { 3, 1, 2 }, values); } - [Fact(Skip = "Remove this Skip property to run this test")] + [Fact] public void From_enumerable() { var list = new SimpleLinkedList(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); + Assert.Equal(2, list.Remove()); + Assert.Equal(3, list.Remove()); + Assert.Equal(5, list.Remove()); + Assert.Equal(7, list.Remove()); + Assert.Equal(11, list.Remove()); } - [Theory(Skip = "Remove this Skip property to run this test")] - [InlineData(1)] - [InlineData(2)] - [InlineData(10)] - [InlineData(100)] - public void Reverse(int length) + [Fact] + public void Reverse_enumerable() { - var values = Enumerable.Range(1, length).ToArray(); + var values = Enumerable.Range(1, 5).ToArray(); var list = new SimpleLinkedList(values); - var reversed = list.Reverse(); - Assert.Equal(values.Reverse(), reversed); + var enumerable = Assert.IsAssignableFrom>(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] + public void Roundtrip() { - var values = Enumerable.Range(1, length); - var listValues = new SimpleLinkedList(values); - Assert.Equal(values, listValues); + var values = Enumerable.Range(1, 7); + var list = new SimpleLinkedList(values.ToArray()); + var enumerable = Assert.IsAssignableFrom>(list); + Assert.Equal(values.Reverse(), enumerable); } } \ No newline at end of file From 1d4d50f5d6d011fad1581aed628474f1cad4b59a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 11 Apr 2023 19:57:04 +0100 Subject: [PATCH 02/19] it is push and pop, not add --- .../simple-linked-list/.meta/Example.cs | 6 +- .../simple-linked-list/SimpleLinkedList.cs | 55 ++++++++++--------- .../SimpleLinkedListTests.cs | 20 +++---- 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/exercises/practice/simple-linked-list/.meta/Example.cs b/exercises/practice/simple-linked-list/.meta/Example.cs index e3b1f69314..19c539afde 100644 --- a/exercises/practice/simple-linked-list/.meta/Example.cs +++ b/exercises/practice/simple-linked-list/.meta/Example.cs @@ -16,17 +16,17 @@ public SimpleLinkedList() { } public SimpleLinkedList(params T[] values) { foreach(var value in values) { - Add(value); + Push(value); } } - public void Add(T value) + public void Push(T value) { var node = new Node { Value = value, Next = this.head }; this.head = node; } - public T Remove() + public T Pop() { if (this.head == null) { throw new InvalidOperationException("List is empty!"); diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index bb6682f740..19c539afde 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -1,48 +1,49 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Linq; public class SimpleLinkedList : IEnumerable { - public SimpleLinkedList(T value) - { - throw new NotImplementedException("You need to implement this function."); + private class Node { + public T Value { get; set; } + public Node Next { get; set; } } + + private Node head; - public SimpleLinkedList(IEnumerable values) - { - throw new NotImplementedException("You need to implement this function."); - } + public SimpleLinkedList() { } - public T Value - { - get - { - throw new NotImplementedException("You need to implement this function."); - } + public SimpleLinkedList(params T[] values) + { + foreach(var value in values) { + Push(value); + } } - public SimpleLinkedList Next - { - get - { - throw new NotImplementedException("You need to implement this function."); - } + public void Push(T value) + { + var node = new Node { Value = value, Next = this.head }; + this.head = node; } - public SimpleLinkedList Add(T value) + public T Pop() { - throw new NotImplementedException("You need to implement this function."); + if (this.head == null) { + throw new InvalidOperationException("List is empty!"); + } + var value = head.Value; + head = head.Next; + return value; } public IEnumerator GetEnumerator() { - throw new NotImplementedException("You need to implement this function."); + var current = this.head; + while(current != null) { + yield return current.Value; + current = current.Next; + } } - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException("You need to implement this function."); - } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } \ No newline at end of file diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index 9da2e463a2..b19c9eefe8 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -1,6 +1,6 @@ using System.Linq; -using Xunit; using System.Collections.Generic; +using Xunit; public class SimpleLinkedListTests { @@ -8,22 +8,22 @@ public class SimpleLinkedListTests public void Single_item_list_value() { var list = new SimpleLinkedList(1); - Assert.Equal(1, list.Remove()); + Assert.Equal(1, list.Pop()); } [Fact] public void Two_item_list_first_value() { var list = new SimpleLinkedList(1, 2); - Assert.Equal(2, list.Remove()); + Assert.Equal(2, list.Pop()); } [Fact] public void Two_item_list_second_value() { var list = new SimpleLinkedList(1, 2); - list.Remove(); - Assert.Equal(1, list.Remove()); + list.Pop(); + Assert.Equal(1, list.Pop()); } [Fact] @@ -37,11 +37,11 @@ public void Multivalue_initialisation() public void From_enumerable() { var list = new SimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); - Assert.Equal(2, list.Remove()); - Assert.Equal(3, list.Remove()); - Assert.Equal(5, list.Remove()); - Assert.Equal(7, list.Remove()); - Assert.Equal(11, list.Remove()); + 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()); } [Fact] From 6042c384c9c59fdfb4b7d4d6a213d65e540adb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 11 Apr 2023 22:44:46 +0100 Subject: [PATCH 03/19] new set of tests for the new take Implementing LIFO, with Count and more standard Push(), Pop() methods to align more with the new standard problem specification and other tracks' implementation --- .../simple-linked-list/.meta/config.json | 3 +- .../simple-linked-list/SimpleLinkedList.cs | 4 ++ .../SimpleLinkedListTests.cs | 52 ++++++++++++++----- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/exercises/practice/simple-linked-list/.meta/config.json b/exercises/practice/simple-linked-list/.meta/config.json index 4a0d1bc2be..a3e1e533c7 100644 --- a/exercises/practice/simple-linked-list/.meta/config.json +++ b/exercises/practice/simple-linked-list/.meta/config.json @@ -6,7 +6,8 @@ "bressain", "j2jensen", "robkeim", - "wolf99" + "wolf99", + "michalporeba" ], "files": { "solution": [ diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index 19c539afde..7b3757c272 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -20,10 +20,13 @@ public SimpleLinkedList(params T[] values) } } + public int Count { get; private set; } = 0; + public void Push(T value) { var node = new Node { Value = value, Next = this.head }; this.head = node; + this.Count++; } public T Pop() @@ -33,6 +36,7 @@ public T Pop() } var value = head.Value; head = head.Next; + this.Count--; return value; } diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index b19c9eefe8..0f1c143f70 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -4,33 +4,61 @@ public class SimpleLinkedListTests { - [Fact] - public void Single_item_list_value() + [Fact] + public void Empty_list_has_no_elements() { - var list = new SimpleLinkedList(1); - Assert.Equal(1, list.Pop()); + var list = new SimpleLinkedList(); + Assert.Equal(0, list.Count); } [Fact] - public void Two_item_list_first_value() + public void Pushing_elements_to_the_list_increases_the_count() { - var list = new SimpleLinkedList(1, 2); - Assert.Equal(2, list.Pop()); + var list = new SimpleLinkedList(); + list.Push(0); + Assert.Equal(1, list.Count); + list.Push(0); + Assert.Equal(2, list.Count); } [Fact] - public void Two_item_list_second_value() + public void Popping_elements_from_the_list_decreases_the_count() { - var list = new SimpleLinkedList(1, 2); + var list = new SimpleLinkedList(); + list.Push(0); + list.Push(0); + Assert.Equal(2, list.Count); list.Pop(); - Assert.Equal(1, list.Pop()); + Assert.Equal(1, list.Count); + } + + [Fact] + public void Elements_pop_back_in_lifo_order() + { + var list = new SimpleLinkedList(); + list.Push(3); + list.Push(5); + Assert.Equal(5, list.Pop()); + list.Push(7); + Assert.Equal(7, list.Pop()); + Assert.Equal(3, list.Pop()); + } + + [Fact] + public void Singlevalue_initialisation() + { + var list = new SimpleLinkedList(7); + Assert.Equal(1, list.Count); + Assert.Equal(7, list.Pop()); } [Fact] public void Multivalue_initialisation() { - var values = new SimpleLinkedList(2, 1, 3); - Assert.Equal(new[] { 3, 1, 2 }, values); + var list = new SimpleLinkedList(2, 1, 3); + Assert.Equal(3, list.Pop()); + Assert.Equal(1, list.Pop()); + Assert.Equal(2, list.Pop()); } [Fact] From bf21ddb995b30cff88114b4e211299368a0ae255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 11 Apr 2023 22:53:43 +0100 Subject: [PATCH 04/19] updating Example.cs to pass the tests It is not the final version, there is more to do before this PR will be ready for proper review, but the tests should pass anyway --- exercises/practice/simple-linked-list/.meta/Example.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/exercises/practice/simple-linked-list/.meta/Example.cs b/exercises/practice/simple-linked-list/.meta/Example.cs index 19c539afde..7b3757c272 100644 --- a/exercises/practice/simple-linked-list/.meta/Example.cs +++ b/exercises/practice/simple-linked-list/.meta/Example.cs @@ -20,10 +20,13 @@ public SimpleLinkedList(params T[] values) } } + public int Count { get; private set; } = 0; + public void Push(T value) { var node = new Node { Value = value, Next = this.head }; this.head = node; + this.Count++; } public T Pop() @@ -33,6 +36,7 @@ public T Pop() } var value = head.Value; head = head.Next; + this.Count--; return value; } From 4eec924e7d548cb53882b54917db0d97a56ae744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 11 Apr 2023 23:16:03 +0100 Subject: [PATCH 05/19] proposed tasks to lead the implementation --- .../simple-linked-list/SimpleLinkedListTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index 0f1c143f70..f0ecc4564c 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -1,10 +1,12 @@ using System.Linq; using System.Collections.Generic; +using Exercism.Tests; using Xunit; public class SimpleLinkedListTests { [Fact] + [Task(1)] public void Empty_list_has_no_elements() { var list = new SimpleLinkedList(); @@ -12,6 +14,7 @@ public void Empty_list_has_no_elements() } [Fact] + [Task(2)] public void Pushing_elements_to_the_list_increases_the_count() { var list = new SimpleLinkedList(); @@ -22,6 +25,7 @@ public void Pushing_elements_to_the_list_increases_the_count() } [Fact] + [Task(3)] public void Popping_elements_from_the_list_decreases_the_count() { var list = new SimpleLinkedList(); @@ -33,6 +37,7 @@ public void Popping_elements_from_the_list_decreases_the_count() } [Fact] + [Task(4)] public void Elements_pop_back_in_lifo_order() { var list = new SimpleLinkedList(); @@ -45,6 +50,7 @@ public void Elements_pop_back_in_lifo_order() } [Fact] + [Task(5)] public void Singlevalue_initialisation() { var list = new SimpleLinkedList(7); @@ -53,6 +59,7 @@ public void Singlevalue_initialisation() } [Fact] + [Task(5)] public void Multivalue_initialisation() { var list = new SimpleLinkedList(2, 1, 3); @@ -62,6 +69,7 @@ public void Multivalue_initialisation() } [Fact] + [Task(5)] public void From_enumerable() { var list = new SimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); @@ -73,6 +81,7 @@ public void From_enumerable() } [Fact] + [Task(6)] public void Reverse_enumerable() { var values = Enumerable.Range(1, 5).ToArray(); @@ -83,6 +92,7 @@ public void Reverse_enumerable() } [Fact] + [Task(6)] public void Roundtrip() { var values = Enumerable.Range(1, 7); From 2687eab12143e3906b2afb09911a674a98f044dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 11 Apr 2023 23:37:52 +0100 Subject: [PATCH 06/19] count should be encapsulated --- exercises/practice/simple-linked-list/.meta/Example.cs | 2 +- .../practice/simple-linked-list/SimpleLinkedList.cs | 2 +- .../practice/simple-linked-list/SimpleLinkedListTests.cs | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/exercises/practice/simple-linked-list/.meta/Example.cs b/exercises/practice/simple-linked-list/.meta/Example.cs index 7b3757c272..ba5e08b6b1 100644 --- a/exercises/practice/simple-linked-list/.meta/Example.cs +++ b/exercises/practice/simple-linked-list/.meta/Example.cs @@ -21,7 +21,7 @@ public SimpleLinkedList(params T[] values) } public int Count { get; private set; } = 0; - + public void Push(T value) { var node = new Node { Value = value, Next = this.head }; diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index 7b3757c272..ba5e08b6b1 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -21,7 +21,7 @@ public SimpleLinkedList(params T[] values) } public int Count { get; private set; } = 0; - + public void Push(T value) { var node = new Node { Value = value, Next = this.head }; diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index f0ecc4564c..a50055754f 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -13,6 +13,15 @@ public void Empty_list_has_no_elements() Assert.Equal(0, list.Count); } + [Fact] + [Task(1)] + public void Count_cannot_be_changed_from_the_outside() + { + var count = typeof(SimpleLinkedList<>).GetProperty("Count"); + Assert.True(count?.GetGetMethod().IsPublic); + Assert.False(count?.GetSetMethod(true).IsPublic); + } + [Fact] [Task(2)] public void Pushing_elements_to_the_list_increases_the_count() From 4a915f6c9b42a04ea7ff4c9aeade23b0d02c22eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 12 Apr 2023 07:59:53 +0100 Subject: [PATCH 07/19] reflection instead of direct constructor call This allows for gradual tests without providing the constructor early in the development --- .../SimpleLinkedListTests.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index a50055754f..f6f2bfef8b 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Collections.Generic; using Exercism.Tests; @@ -58,11 +59,26 @@ public void Elements_pop_back_in_lifo_order() Assert.Equal(3, list.Pop()); } + private static SimpleLinkedList CreateSimpleLinkedList(int value) + { + var type = typeof(SimpleLinkedList<>).MakeGenericType(typeof(int)); + var constructor = type.GetConstructor(new Type[] { typeof(int) }); + return (SimpleLinkedList)constructor?.Invoke(new object[]{ value }) + ?? CreateSimpleLinkedList(new int[] { value }); + } + + private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) + { + var type = typeof(SimpleLinkedList<>).MakeGenericType(typeof(int)); + var constructor = type.GetConstructor(new Type[]{typeof(int[])}); + return (SimpleLinkedList)constructor.Invoke(new object[]{ values }); + } + [Fact] [Task(5)] public void Singlevalue_initialisation() { - var list = new SimpleLinkedList(7); + var list = CreateSimpleLinkedList(7); Assert.Equal(1, list.Count); Assert.Equal(7, list.Pop()); } @@ -71,7 +87,7 @@ public void Singlevalue_initialisation() [Task(5)] public void Multivalue_initialisation() { - var list = new SimpleLinkedList(2, 1, 3); + var list = CreateSimpleLinkedList(2, 1, 3); Assert.Equal(3, list.Pop()); Assert.Equal(1, list.Pop()); Assert.Equal(2, list.Pop()); @@ -81,7 +97,7 @@ public void Multivalue_initialisation() [Task(5)] public void From_enumerable() { - var list = new SimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); + var list = CreateSimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); Assert.Equal(2, list.Pop()); Assert.Equal(3, list.Pop()); Assert.Equal(5, list.Pop()); @@ -94,7 +110,7 @@ public void From_enumerable() public void Reverse_enumerable() { var values = Enumerable.Range(1, 5).ToArray(); - var list = new SimpleLinkedList(values); + var list = CreateSimpleLinkedList(values); var enumerable = Assert.IsAssignableFrom>(list); var reversed = enumerable.Reverse(); Assert.Equal(values, reversed); @@ -105,7 +121,7 @@ public void Reverse_enumerable() public void Roundtrip() { var values = Enumerable.Range(1, 7); - var list = new SimpleLinkedList(values.ToArray()); + var list = CreateSimpleLinkedList(values.ToArray()); var enumerable = Assert.IsAssignableFrom>(list); Assert.Equal(values.Reverse(), enumerable); } From 1d4e0bd1bb954711170ca51a65ade0b148280d95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 12 Apr 2023 08:04:43 +0100 Subject: [PATCH 08/19] back to the starting point this could be a good starting point for the exercise --- .../simple-linked-list/SimpleLinkedList.cs | 43 ++----------------- .../SimpleLinkedListTests.cs | 16 +++---- 2 files changed, 11 insertions(+), 48 deletions(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index ba5e08b6b1..7a283fd49f 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -2,52 +2,15 @@ using System.Collections; using System.Collections.Generic; -public class SimpleLinkedList : IEnumerable +public class SimpleLinkedList { - private class Node { - public T Value { get; set; } - public Node Next { get; set; } - } - - private Node head; - - public SimpleLinkedList() { } - - public SimpleLinkedList(params T[] values) - { - foreach(var value in values) { - Push(value); - } - } - - public int Count { get; private set; } = 0; - public void Push(T value) { - var node = new Node { Value = value, Next = this.head }; - this.head = node; - this.Count++; + throw new NotImplementedException(); } public T Pop() { - if (this.head == null) { - throw new InvalidOperationException("List is empty!"); - } - var value = head.Value; - head = head.Next; - this.Count--; - return value; + throw new NotImplementedException(); } - - public IEnumerator GetEnumerator() - { - var current = this.head; - while(current != null) { - yield return current.Value; - current = current.Next; - } - } - - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } \ No newline at end of file diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index f6f2bfef8b..8340c92433 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -23,7 +23,7 @@ public void Count_cannot_be_changed_from_the_outside() Assert.False(count?.GetSetMethod(true).IsPublic); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(2)] public void Pushing_elements_to_the_list_increases_the_count() { @@ -34,7 +34,7 @@ public void Pushing_elements_to_the_list_increases_the_count() Assert.Equal(2, list.Count); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(3)] public void Popping_elements_from_the_list_decreases_the_count() { @@ -46,7 +46,7 @@ public void Popping_elements_from_the_list_decreases_the_count() Assert.Equal(1, list.Count); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(4)] public void Elements_pop_back_in_lifo_order() { @@ -74,7 +74,7 @@ private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) return (SimpleLinkedList)constructor.Invoke(new object[]{ values }); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(5)] public void Singlevalue_initialisation() { @@ -83,7 +83,7 @@ public void Singlevalue_initialisation() Assert.Equal(7, list.Pop()); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(5)] public void Multivalue_initialisation() { @@ -93,7 +93,7 @@ public void Multivalue_initialisation() Assert.Equal(2, list.Pop()); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(5)] public void From_enumerable() { @@ -105,7 +105,7 @@ public void From_enumerable() Assert.Equal(11, list.Pop()); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(6)] public void Reverse_enumerable() { @@ -116,7 +116,7 @@ public void Reverse_enumerable() Assert.Equal(values, reversed); } - [Fact] + [Fact(Skip = "Remove this Skip property to run this test")] [Task(6)] public void Roundtrip() { From 510d68cf45ddbc1b8b2e06375c7b2874eed80178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 12 Apr 2023 08:22:39 +0100 Subject: [PATCH 09/19] you need to implement this function --- exercises/practice/simple-linked-list/SimpleLinkedList.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index 7a283fd49f..f56712b5cb 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -6,11 +6,11 @@ public class SimpleLinkedList { public void Push(T value) { - throw new NotImplementedException(); + throw new NotImplementedException("You need to implement this function."); } public T Pop() { - throw new NotImplementedException(); + throw new NotImplementedException("You need to implement this function."); } } \ No newline at end of file From f78c5422ecaaf71cd82355ff60214630bd0967ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 12 Apr 2023 18:30:51 +0100 Subject: [PATCH 10/19] removing tasks for now --- .../simple-linked-list/SimpleLinkedList.cs | 4 ++-- .../simple-linked-list/SimpleLinkedListTests.cs | 15 ++------------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedList.cs b/exercises/practice/simple-linked-list/SimpleLinkedList.cs index f56712b5cb..457d8e4b77 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedList.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedList.cs @@ -1,9 +1,9 @@ using System; -using System.Collections; -using System.Collections.Generic; public class SimpleLinkedList { + public int Count => throw new NotImplementedException("You need to implement this function."); + public void Push(T value) { throw new NotImplementedException("You need to implement this function."); diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index 8340c92433..f5f3ceb7b2 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -1,21 +1,18 @@ using System; using System.Linq; using System.Collections.Generic; -using Exercism.Tests; using Xunit; public class SimpleLinkedListTests { - [Fact] - [Task(1)] + [Fact(Skip = "Remove this Skip property to run this test")] public void Empty_list_has_no_elements() { var list = new SimpleLinkedList(); Assert.Equal(0, list.Count); } - [Fact] - [Task(1)] + [Fact(Skip = "Remove this Skip property to run this test")] public void Count_cannot_be_changed_from_the_outside() { var count = typeof(SimpleLinkedList<>).GetProperty("Count"); @@ -24,7 +21,6 @@ public void Count_cannot_be_changed_from_the_outside() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(2)] public void Pushing_elements_to_the_list_increases_the_count() { var list = new SimpleLinkedList(); @@ -35,7 +31,6 @@ public void Pushing_elements_to_the_list_increases_the_count() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(3)] public void Popping_elements_from_the_list_decreases_the_count() { var list = new SimpleLinkedList(); @@ -47,7 +42,6 @@ public void Popping_elements_from_the_list_decreases_the_count() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(4)] public void Elements_pop_back_in_lifo_order() { var list = new SimpleLinkedList(); @@ -75,7 +69,6 @@ private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(5)] public void Singlevalue_initialisation() { var list = CreateSimpleLinkedList(7); @@ -84,7 +77,6 @@ public void Singlevalue_initialisation() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(5)] public void Multivalue_initialisation() { var list = CreateSimpleLinkedList(2, 1, 3); @@ -94,7 +86,6 @@ public void Multivalue_initialisation() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(5)] public void From_enumerable() { var list = CreateSimpleLinkedList(new[] { 11, 7, 5, 3, 2 }); @@ -106,7 +97,6 @@ public void From_enumerable() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(6)] public void Reverse_enumerable() { var values = Enumerable.Range(1, 5).ToArray(); @@ -117,7 +107,6 @@ public void Reverse_enumerable() } [Fact(Skip = "Remove this Skip property to run this test")] - [Task(6)] public void Roundtrip() { var values = Enumerable.Range(1, 7); From fac82db1320178f71bb4fb0afef81c9014d58340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Wed, 12 Apr 2023 18:37:22 +0100 Subject: [PATCH 11/19] some instructions to guide implementation --- .../simple-linked-list/.docs/instructions.append.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/exercises/practice/simple-linked-list/.docs/instructions.append.md b/exercises/practice/simple-linked-list/.docs/instructions.append.md index 457bb58d0f..7c57159bb7 100644 --- a/exercises/practice/simple-linked-list/.docs/instructions.append.md +++ b/exercises/practice/simple-linked-list/.docs/instructions.append.md @@ -1,4 +1,10 @@ # Hints -This exercise requires you to create a linked list data structure which can be iterated. This requires you to implement the IEnumerable\ 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 `Count` property - it should not be possible to change its value from the outside +2. Implement `Push(T value)` method that adds a value to the list at its head. +3. Implement `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` 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. From bd9517b499b2d7949131d02b8a83f214cece4c23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:20:49 +0100 Subject: [PATCH 12/19] Update exercises/practice/simple-linked-list/.docs/instructions.append.md Co-authored-by: Erik Schierboom --- .../practice/simple-linked-list/.docs/instructions.append.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/.docs/instructions.append.md b/exercises/practice/simple-linked-list/.docs/instructions.append.md index 7c57159bb7..e30f066886 100644 --- a/exercises/practice/simple-linked-list/.docs/instructions.append.md +++ b/exercises/practice/simple-linked-list/.docs/instructions.append.md @@ -2,7 +2,7 @@ This exercise requires you to create a linked list data structure which can be iterated. -1. Implement `Count` property - it should not be possible to change its value from the outside +1. Implement the `Count` property - it should not be possible to change its value from the outside 2. Implement `Push(T value)` method that adds a value to the list at its head. 3. Implement `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 From 486e65911a688cd0c430e7c6f7647a8f2ac28b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:21:04 +0100 Subject: [PATCH 13/19] Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs Co-authored-by: Erik Schierboom --- exercises/practice/simple-linked-list/SimpleLinkedListTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index f5f3ceb7b2..e6a1a5f26c 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -61,7 +61,7 @@ private static SimpleLinkedList CreateSimpleLinkedList(int value) ?? CreateSimpleLinkedList(new int[] { value }); } - private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) + private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) { var type = typeof(SimpleLinkedList<>).MakeGenericType(typeof(int)); var constructor = type.GetConstructor(new Type[]{typeof(int[])}); From 990df6ca275974b7a29bfedf7eb88989103a9239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:21:30 +0100 Subject: [PATCH 14/19] Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs Co-authored-by: Erik Schierboom --- exercises/practice/simple-linked-list/SimpleLinkedListTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index e6a1a5f26c..f6d8c807b1 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -77,7 +77,7 @@ public void Singlevalue_initialisation() } [Fact(Skip = "Remove this Skip property to run this test")] - public void Multivalue_initialisation() + public void Multi_value_initialisation() { var list = CreateSimpleLinkedList(2, 1, 3); Assert.Equal(3, list.Pop()); From 97a7736487a59d91efb156f974484d3e7377f0c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:21:42 +0100 Subject: [PATCH 15/19] Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs Co-authored-by: Erik Schierboom --- exercises/practice/simple-linked-list/SimpleLinkedListTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index f6d8c807b1..4f1292adb4 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -69,7 +69,7 @@ private static SimpleLinkedList CreateSimpleLinkedList(params int[] values) } [Fact(Skip = "Remove this Skip property to run this test")] - public void Singlevalue_initialisation() + public void Single_value_initialisation() { var list = CreateSimpleLinkedList(7); Assert.Equal(1, list.Count); From 37c0b2251773e9780a9f8e758a603ed385ce335f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:22:02 +0100 Subject: [PATCH 16/19] Update exercises/practice/simple-linked-list/SimpleLinkedListTests.cs Co-authored-by: Erik Schierboom --- exercises/practice/simple-linked-list/SimpleLinkedListTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs index 4f1292adb4..3d78d198dc 100644 --- a/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs +++ b/exercises/practice/simple-linked-list/SimpleLinkedListTests.cs @@ -5,7 +5,7 @@ public class SimpleLinkedListTests { - [Fact(Skip = "Remove this Skip property to run this test")] + [Fact] public void Empty_list_has_no_elements() { var list = new SimpleLinkedList(); From eeee6231b5df255cc614a6173c3a3e67cf6b9481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:22:21 +0100 Subject: [PATCH 17/19] Update exercises/practice/simple-linked-list/.docs/instructions.append.md Co-authored-by: Erik Schierboom --- .../practice/simple-linked-list/.docs/instructions.append.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/.docs/instructions.append.md b/exercises/practice/simple-linked-list/.docs/instructions.append.md index e30f066886..195ee999a1 100644 --- a/exercises/practice/simple-linked-list/.docs/instructions.append.md +++ b/exercises/practice/simple-linked-list/.docs/instructions.append.md @@ -3,7 +3,7 @@ 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 `Push(T value)` method that adds a value to the list at its head. +2. Implement the `Push(T value)` method that adds a value to the list at its head. 3. Implement `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` interface. For more information, see [this page](https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.ienumerable-1). From 892d55c43d97944f7f11f6b695bc4514262d2017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:22:47 +0100 Subject: [PATCH 18/19] Update exercises/practice/simple-linked-list/.docs/instructions.append.md Co-authored-by: Erik Schierboom --- .../practice/simple-linked-list/.docs/instructions.append.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/.docs/instructions.append.md b/exercises/practice/simple-linked-list/.docs/instructions.append.md index 195ee999a1..a052e16721 100644 --- a/exercises/practice/simple-linked-list/.docs/instructions.append.md +++ b/exercises/practice/simple-linked-list/.docs/instructions.append.md @@ -1,4 +1,4 @@ -# Hints +# Instruction append This exercise requires you to create a linked list data structure which can be iterated. From 945cd787730078750e4c7f87c1d6dd21a10f0e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Por=C4=99ba?= Date: Tue, 18 Apr 2023 13:23:28 +0100 Subject: [PATCH 19/19] Update exercises/practice/simple-linked-list/.docs/instructions.append.md Co-authored-by: Erik Schierboom --- .../practice/simple-linked-list/.docs/instructions.append.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/simple-linked-list/.docs/instructions.append.md b/exercises/practice/simple-linked-list/.docs/instructions.append.md index a052e16721..e095289d61 100644 --- a/exercises/practice/simple-linked-list/.docs/instructions.append.md +++ b/exercises/practice/simple-linked-list/.docs/instructions.append.md @@ -4,7 +4,7 @@ This exercise requires you to create a linked list data structure which can be i 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 `Pop()` method which removes and returns a value from the 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` 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.