From 734d2c4078e50a1b061bdf4cd38b2ec8477904c2 Mon Sep 17 00:00:00 2001 From: "f.areias" Date: Wed, 25 Sep 2024 18:30:24 -0300 Subject: [PATCH] add solutions --- CodeWars/Array-diff.cs | 22 +++++++++ CodeWars/LinkedLists-InsertNthNode.cs | 48 +++++++++++++++++++ CodeWars/Parse-a-linked-list-from-a-string.cs | 34 +++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 CodeWars/Array-diff.cs create mode 100644 CodeWars/LinkedLists-InsertNthNode.cs create mode 100644 CodeWars/Parse-a-linked-list-from-a-string.cs diff --git a/CodeWars/Array-diff.cs b/CodeWars/Array-diff.cs new file mode 100644 index 00000000..31d8898b --- /dev/null +++ b/CodeWars/Array-diff.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +public class Kata +{ + public static int[] ArrayDiff(int[] a, int[] b) + { + var hashSet = new HashSet(b); + var response = new List(); + + foreach (var x in a) + { + if (!hashSet.Contains(x)) + { + response.Add(x); + } + } + + return response.ToArray(); + } +} \ No newline at end of file diff --git a/CodeWars/LinkedLists-InsertNthNode.cs b/CodeWars/LinkedLists-InsertNthNode.cs new file mode 100644 index 00000000..700a3ae4 --- /dev/null +++ b/CodeWars/LinkedLists-InsertNthNode.cs @@ -0,0 +1,48 @@ +using System; + +public partial class Node +{ + public int Data; + public Node Next; + + public Node(int data) + { + this.Data = data; + this.Next = null; + } + + public static Node InsertNth(Node head, int index, int data) + { + + if (index < 0) + throw new ArgumentOutOfRangeException(nameof(index), "Index is out of the range of the list."); + + var newNode = new Node(data); + + if (index == 0) + { + newNode.Next = head; + return newNode; + } + + var current = head; + int count = 0; + + while (current != null && count < index - 1) + { + current = current.Next; + count++; + } + + if (current == null) + { + throw new ArgumentOutOfRangeException(nameof(index), "Index is out of the range of the list."); + } + + newNode.Next = current.Next; + current.Next = newNode; + + return head; + + } +} \ No newline at end of file diff --git a/CodeWars/Parse-a-linked-list-from-a-string.cs b/CodeWars/Parse-a-linked-list-from-a-string.cs new file mode 100644 index 00000000..b8d1d2b4 --- /dev/null +++ b/CodeWars/Parse-a-linked-list-from-a-string.cs @@ -0,0 +1,34 @@ +using System; + +public static class Kata +{ + public static Node Parse(string nodes) + { + Node head = null; + Node prev = null; + var array = nodes.Split(" -> "); + + foreach (var current in array) + { + if (current.Trim() == "null") + break; + + var value = int.Parse(current.Trim()); + + var newNode = new Node(value); + + if (head == null) + { + head = newNode; + } + else + { + prev.Next = newNode; + } + + prev = newNode; + } + + return head; + } +} \ No newline at end of file