From 66e8965bdc22fdc94f09d9f0ebfbfe83fbfc1df8 Mon Sep 17 00:00:00 2001 From: TMason11095 Date: Tue, 4 Jul 2023 21:47:28 -0500 Subject: [PATCH] Submit Ch3 HW 7 & 8: -Implemented Enumberable extension methods Shuffle() and SnapFingers(Predicate). -Implemented a LINQ demo that demonstrated LINQ functions: Any, Count, Order, Union, Intersect, and Except. --- Src/BootCamp.Chapter/EnumerableExtensions.cs | 39 +++++++++ Src/BootCamp.Chapter/Program.cs | 87 ++++++++++++++++++-- 2 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 Src/BootCamp.Chapter/EnumerableExtensions.cs diff --git a/Src/BootCamp.Chapter/EnumerableExtensions.cs b/Src/BootCamp.Chapter/EnumerableExtensions.cs new file mode 100644 index 000000000..0327a9ea9 --- /dev/null +++ b/Src/BootCamp.Chapter/EnumerableExtensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace BootCamp.Chapter +{ + public static class EnumerableExtensions + { + public static IEnumerable Shuffle(this IEnumerable values) + { + List list = new List(values); + Random rnd = new Random(); + int count = list.Count(); + //Fisher Yates Shuffle + for (int i = 0; i < count; i++) + { + int swapIndex = rnd.Next(i, count); + T swapValue = list[swapIndex]; + list[swapIndex] = list[i]; + list[i] = swapValue; + } + + return list; + } + + public static IEnumerable SnapFingers(this IEnumerable values, Predicate predicate) + { + //Apply predicate + List list = values.Where(value => predicate(value)).ToList(); + + //Remove half + int count = list.Count(); + int remove = count / 2; + return list.Take(count - remove); + } + } +} diff --git a/Src/BootCamp.Chapter/Program.cs b/Src/BootCamp.Chapter/Program.cs index 0da10eb6e..6f072cf15 100644 --- a/Src/BootCamp.Chapter/Program.cs +++ b/Src/BootCamp.Chapter/Program.cs @@ -1,12 +1,87 @@ using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; namespace BootCamp.Chapter { - class Program - { - static void Main(string[] args) - { + class Program + { + static void Main(string[] args) + { + List names = new List() + { + "Tyler", + "Andrew", + "Jakey", + "Shea", + "Michael" + }; - } - } + //names = names.Shuffle().ToList(); + + //names = names.SnapFingers(n => true).ToList(); + + LinqStringDemo(names); + + Console.ReadLine(); + } + + public static void LinqStringDemo(List names) + { + List names2 = new List() + { + "Tylor", + "Josh", + "Shea", + "Kyle", + "Tyler" + }; + + Console.WriteLine(GetPrintableList(names)); + + //Any + bool hasNameWithA = names.Any(name => name.Contains('a')); + Console.WriteLine($"Any names contain a?: {hasNameWithA}"); + bool hasNameWithZ = names.Any(name => name.Contains("z")); + Console.WriteLine($"Any names contain z?: {hasNameWithZ}"); + + //Count + Console.WriteLine($"Number of names containing a: {names.Count(name => name.Contains('a'))}"); + + //Order + Console.WriteLine($"abc order: {GetPrintableList(names.OrderBy(name => name).ToList())}"); + + //Sets + Console.WriteLine($"Set 1: {GetPrintableList(names)}"); + Console.WriteLine($"Set 2: {GetPrintableList(names2)}"); + + //Union + Console.WriteLine($"All names in both sets: {GetPrintableList(names.Union(names2).ToList())}"); + + //Intersection + Console.WriteLine($"Names in both sets: {GetPrintableList(names.Intersect(names2).ToList())}"); + + //Substraction + Console.WriteLine($"Names that are in set 1, but not set 2: {GetPrintableList(names.Except(names2).ToList())}"); + + //All 3 + Console.WriteLine($"Names that aren't in both sets: {GetPrintableList(names.Union(names2).Except(names.Intersect(names2)).ToList())}"); + } + + public static string GetPrintableList(List names) + { + StringBuilder sb = new StringBuilder(); + foreach (string name in names.GetRange(0, names.Count - 1)) + { + sb.Append(name); + sb.Append(", "); + } + sb.Append(names[^1]); + + return sb.ToString(); + } + } }