Skip to content
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

Submit Ch3 HW 7 & 8 #800

Open
wants to merge 1 commit into
base: Chapter3/Homework/7And8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Src/BootCamp.Chapter/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<T> Shuffle<T>(this IEnumerable<T> values)
{
List<T> list = new List<T>(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<T> SnapFingers<T>(this IEnumerable<T> values, Predicate<T> predicate)
{
//Apply predicate
List<T> list = values.Where(value => predicate(value)).ToList();

//Remove half
int count = list.Count();
int remove = count / 2;
return list.Take(count - remove);
}
}
}
87 changes: 81 additions & 6 deletions Src/BootCamp.Chapter/Program.cs
Original file line number Diff line number Diff line change
@@ -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<string> names = new List<string>()
{
"Tyler",
"Andrew",
"Jakey",
"Shea",
"Michael"
};

}
}
//names = names.Shuffle().ToList();

//names = names.SnapFingers(n => true).ToList();

LinqStringDemo(names);

Console.ReadLine();
}

public static void LinqStringDemo(List<string> names)
{
List<string> names2 = new List<string>()
{
"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<string> 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();
}
}
}