diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsExamples.cs b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsExamples.cs new file mode 100644 index 0000000000..c36b2700c4 --- /dev/null +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsExamples.cs @@ -0,0 +1,52 @@ +namespace ListExistsInAnotherCSharp; + +public static class CompareListsExamples +{ + public static bool CompareListUsingIteration(List firstList, List secondList) + { + foreach (var item in firstList) + { + if (secondList.Contains(item)) + { + return true; + } + } + + return false; + } + + public static bool CompareListUsingIntersect(List firstList, List secondList) + { + return firstList.Intersect(secondList).Any(); + } + + public static bool CompareListUsingAnyContains(List firstList, List secondList) + { + return secondList.Any(firstList.Contains); + } + + public static bool CompareListUsingExcept(List firstList, List secondList) + { + return secondList.Except(firstList).Count() != secondList.Distinct().Count(); + } + + public static bool CompareListUsingWhereAny(List firstList, List secondList) + { + return secondList.Where(firstList.Contains).Any(); + } + + public static bool ContainsAllUsingAll(List firstList, List secondList) + { + return firstList.All(secondList.Contains); + } + + public static bool ContainsAllUsingExcept(List firstList, List secondList) + { + return !firstList.Except(secondList).Any(); + } + + public static bool ContainsAllUsingHashSet(List firstList, List secondList) + { + return secondList.ToHashSet().IsSupersetOf(firstList); + } +} diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsMethods.cs b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsMethods.cs index 70373c0b99..2912451f89 100644 --- a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsMethods.cs +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareListsMethods.cs @@ -4,86 +4,60 @@ namespace ListExistsInAnotherCSharp; +[InProcess] [MemoryDiagnoser] [RankColumn] [Orderer(SummaryOrderPolicy.FastestToSlowest)] [GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)] public class CompareListsMethods { - public IEnumerable SampleLists() - { - yield return new object[] - { - GenerateIntegerList(false, false, 0, 1000000), - GenerateIntegerList(true, false, 0, 1000000), - "Reversed" - }; + private const int ListSize = 10_000; - yield return new object[] - { - GenerateIntegerList(false, false, 0, 1000000), - GenerateIntegerList(false, false, 1000001, 1000000), - "Exclusive" - }; + private List _firstList = []; + private List _secondList = []; + + [Params("Reversed", "Exclusive", "Middle")] + public string ListName { get; set; } = "Reversed"; - yield return new object[] + [GlobalSetup] + public void Setup() + { + _firstList = GenerateIntegerList(false, false, 0, ListSize); + _secondList = ListName switch { - GenerateIntegerList(false, false, 0, 1000000), - GenerateIntegerList(false, true, 1000001, 1000000), - "Middle" + "Reversed" => GenerateIntegerList(true, false, 0, ListSize), + "Exclusive" => GenerateIntegerList(false, false, ListSize + 1, ListSize), + _ => GenerateIntegerList(false, true, ListSize + 1, ListSize) }; } - [ArgumentsSource(nameof(SampleLists))] [Benchmark] - public bool CompareListUsingIntersect(List firstList, List secondList, string listName) - { - return firstList.Intersect(secondList).Any(); - } + public bool CompareListUsingIntersect() + => CompareListsExamples.CompareListUsingIntersect(_firstList, _secondList); - [ArgumentsSource(nameof(SampleLists))] [Benchmark] - public bool CompareListUsingAnyContains(List firstList, List secondList, string listName) - { - return secondList.Any(firstList.Contains); - } + public bool CompareListUsingAnyContains() + => CompareListsExamples.CompareListUsingAnyContains(_firstList, _secondList); - [ArgumentsSource(nameof(SampleLists))] [Benchmark] - public bool CompareListUsingIteration(List firstList, List secondList, string listName) - { - foreach (var item in firstList) - { - if (secondList.Contains(item)) - { - return true; - } - } - - return false; - } + public bool CompareListUsingIteration() + => CompareListsExamples.CompareListUsingIteration(_firstList, _secondList); - [ArgumentsSource(nameof(SampleLists))] [Benchmark] - public bool CompareListUsingExcept(List firstList, List secondList, string listName) - { - return secondList.Except(firstList).Count() != secondList.Count; - } + public bool CompareListUsingExcept() + => CompareListsExamples.CompareListUsingExcept(_firstList, _secondList); - [ArgumentsSource(nameof(SampleLists))] [Benchmark] - public bool CompareListUsingWhereAny(List firstList, List secondList, string listName) - { - return secondList.Where(firstList.Contains).Any(); - } + public bool CompareListUsingWhereAny() + => CompareListsExamples.CompareListUsingWhereAny(_firstList, _secondList); - public List GenerateIntegerList(bool useReverse, bool useMiddle, int startIndex, int size) + public static List GenerateIntegerList(bool useReverse, bool useMiddle, int startIndex, int size) { var integerList = new List(size); var i = 0; - while(i < size) - { + while (i < size) + { integerList.Add(startIndex); startIndex++; i++; @@ -103,4 +77,4 @@ public List GenerateIntegerList(bool useReverse, bool useMiddle, int startI return integerList; } -} \ No newline at end of file +} diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareStudentsExamples.cs b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareStudentsExamples.cs index 5eb4acc586..e0d59db9ad 100644 --- a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareStudentsExamples.cs +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/CompareStudentsExamples.cs @@ -1,22 +1,7 @@ namespace ListExistsInAnotherCSharp; -public class CompareStudentsExamples +public static class CompareStudentsExamples { - public static bool CompareStudentsUsingIntersect(List firstList, List secondList) - { - return firstList - .Select(student => $"{student.FirstName} {student.LastName}") - .Intersect(secondList.Select(student => $"{student.FirstName} {student.LastName}")) - .Any(); - } - - public static bool CompareStudentsUsingAnyContains(List firstList, List secondList) - { - return secondList.Any(student => - firstList.Any(s => - s.FirstName == student.FirstName && s.LastName == student.LastName)); - } - public static bool CompareStudentsUsingIteration(List firstList, List secondList) { foreach (var student in firstList) @@ -31,19 +16,36 @@ public static bool CompareStudentsUsingIteration(List firstList, List firstList, List secondList) + { + return firstList + .Select(student => (student.FirstName, student.LastName)) + .Intersect(secondList.Select(student => (student.FirstName, student.LastName))) + .Any(); + } + + public static bool CompareStudentsUsingAnyContains(List firstList, List secondList) + { + return secondList.Any(student => + firstList.Any(s => + s.FirstName == student.FirstName && s.LastName == student.LastName)); + } + public static bool CompareStudentsUsingExcept(List firstList, List secondList) { - return secondList - .Select(student => $"{student.FirstName} {student.LastName}") - .Except(firstList.Select(student => $"{student.FirstName} {student.LastName}")) - .Count() != secondList.Count; + var secondNames = secondList.Select(student => (student.FirstName, student.LastName)); + + return secondNames + .Except(firstList.Select(student => (student.FirstName, student.LastName))) + .Count() != secondNames.Distinct().Count(); } public static bool CompareStudentsUsingWhereAny(List firstList, List secondList) { return secondList - .Any(student => + .Where(student => firstList.Any(s => - s.FirstName == student.FirstName && s.LastName == student.LastName)); + s.FirstName == student.FirstName && s.LastName == student.LastName)) + .Any(); } } diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp.csproj b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp.csproj index 272dc0e806..1a81128a7d 100644 --- a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp.csproj +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp/ListExistsInAnotherCSharp.csproj @@ -1,14 +1,14 @@ - + Exe - net8.0 + net11.0 enable enable - + diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherCSharpTests.csproj b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherCSharpTests.csproj index 3f323bf228..9f85dfbe7b 100644 --- a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherCSharpTests.csproj +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherCSharpTests.csproj @@ -1,7 +1,7 @@ - net8.0 + net11.0 enable enable @@ -10,10 +10,10 @@ - - - - + + + + diff --git a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherUnitTests.cs b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherUnitTests.cs index baa7e89d0c..719bbb5ab3 100644 --- a/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherUnitTests.cs +++ b/collections-lists/ListExistsInAnotherCSharp/ListExistsInAnotherCSharpTests/ListExistsInAnotherUnitTests.cs @@ -3,11 +3,9 @@ namespace ListExistsInAnotherCSharpTests; [TestClass] public class ListExistsInAnotherUnitTests { - private readonly CompareListsMethods _compareListsMethods = new(); private readonly List _firstList; private readonly List _secondList; private readonly List _thirdList; - private readonly string _listName; private readonly List _artStudents; private readonly List _scienceStudents; private readonly List _businessStudents; @@ -17,7 +15,6 @@ public ListExistsInAnotherUnitTests() _firstList = new List { 0, 1, 2, 3, 4 }; _secondList = new List { 5, 6, 7, 8, 9 }; _thirdList = new List { 0, 1, 2, 3, 5 }; - _listName = string.Empty; _artStudents = new List { new() { Id = 1, FirstName = "John", LastName = "Doe" }, @@ -54,36 +51,36 @@ public ListExistsInAnotherUnitTests() [TestMethod] public void GivenTwoLists_WhenComparedUsingIntersect_ValidateCorrectResults() { - Assert.IsTrue(_compareListsMethods.CompareListUsingIntersect(_firstList, _thirdList, _listName)); - Assert.IsFalse(_compareListsMethods.CompareListUsingIntersect(_firstList, _secondList, _listName)); + Assert.IsTrue(CompareListsExamples.CompareListUsingIntersect(_firstList, _thirdList)); + Assert.IsFalse(CompareListsExamples.CompareListUsingIntersect(_firstList, _secondList)); } [TestMethod] public void GivenTwoLists_WhenComparedUsingIteration_ValidateCorrectResults() { - Assert.IsTrue(_compareListsMethods.CompareListUsingIteration(_firstList, _thirdList, _listName)); - Assert.IsFalse(_compareListsMethods.CompareListUsingIteration(_firstList, _secondList, _listName)); + Assert.IsTrue(CompareListsExamples.CompareListUsingIteration(_firstList, _thirdList)); + Assert.IsFalse(CompareListsExamples.CompareListUsingIteration(_firstList, _secondList)); } [TestMethod] public void GivenTwoLists_WhenComparedUsingAnyContains_ValidateCorrectResults() { - Assert.IsTrue(_compareListsMethods.CompareListUsingAnyContains(_firstList, _thirdList, _listName)); - Assert.IsFalse(_compareListsMethods.CompareListUsingAnyContains(_firstList, _secondList, _listName)); + Assert.IsTrue(CompareListsExamples.CompareListUsingAnyContains(_firstList, _thirdList)); + Assert.IsFalse(CompareListsExamples.CompareListUsingAnyContains(_firstList, _secondList)); } [TestMethod] public void GivenTwoLists_WhenComparedUsingExcept_ValidateCorrectResults() { - Assert.IsTrue(_compareListsMethods.CompareListUsingExcept(_firstList, _thirdList, _listName)); - Assert.IsFalse(_compareListsMethods.CompareListUsingExcept(_firstList, _secondList, _listName)); + Assert.IsTrue(CompareListsExamples.CompareListUsingExcept(_firstList, _thirdList)); + Assert.IsFalse(CompareListsExamples.CompareListUsingExcept(_firstList, _secondList)); } [TestMethod] public void GivenTwoLists_WhenComparedUsingWhereAny_ValidateCorrectResults() { - Assert.IsTrue(_compareListsMethods.CompareListUsingWhereAny(_firstList, _thirdList, _listName)); - Assert.IsFalse(_compareListsMethods.CompareListUsingWhereAny(_firstList, _secondList, _listName)); + Assert.IsTrue(CompareListsExamples.CompareListUsingWhereAny(_firstList, _thirdList)); + Assert.IsFalse(CompareListsExamples.CompareListUsingWhereAny(_firstList, _secondList)); } [TestMethod] @@ -124,7 +121,7 @@ public void GivenTwoStudentLists_WhenComparedUsingWhereAny_ValidateCorrectResult [TestMethod] public void GivenListSize_WhenMiddleElementOptionSelected_ValidateCorrectResults() { - var testList = _compareListsMethods.GenerateIntegerList(false, true, 100, 100); + var testList = CompareListsMethods.GenerateIntegerList(false, true, 100, 100); Assert.AreEqual(100, testList.Count); Assert.AreEqual(50, testList[50]); @@ -133,9 +130,46 @@ public void GivenListSize_WhenMiddleElementOptionSelected_ValidateCorrectResults [TestMethod] public void GivenListSize_WhenReversedElementOptionSelected_ValidateCorrectResults() { - var testList = _compareListsMethods.GenerateIntegerList(true, false, 100, 100); + var testList = CompareListsMethods.GenerateIntegerList(true, false, 100, 100); Assert.AreEqual(199, testList.First()); Assert.AreEqual(100, testList.Last()); } -} \ No newline at end of file + + [TestMethod] + public void GivenDuplicatesAndNoCommonItems_WhenComparedUsingExcept_ReturnsFalse() + { + var firstList = new List { 1, 2, 3 }; + var secondList = new List { 4, 4 }; + + Assert.IsTrue(secondList.Except(firstList).Count() != secondList.Count); + Assert.IsFalse(CompareListsExamples.CompareListUsingExcept(firstList, secondList)); + } + + [TestMethod] + public void GivenNamesThatJoinToTheSameString_WhenComparedUsingIntersect_ReturnsFalse() + { + var firstList = new List { new() { Id = 1, FirstName = "Mary Ann", LastName = "Lee" } }; + var secondList = new List { new() { Id = 2, FirstName = "Mary", LastName = "Ann Lee" } }; + + Assert.IsTrue(firstList + .Select(student => $"{student.FirstName} {student.LastName}") + .Intersect(secondList.Select(student => $"{student.FirstName} {student.LastName}")) + .Any()); + Assert.IsFalse(CompareStudentsExamples.CompareStudentsUsingIntersect(firstList, secondList)); + Assert.IsFalse(CompareStudentsExamples.CompareStudentsUsingExcept(firstList, secondList)); + } + + [TestMethod] + public void GivenTwoLists_WhenCheckedForAllItems_ValidateCorrectResults() + { + var subset = new List { 3, 1, 1 }; + + Assert.IsTrue(CompareListsExamples.ContainsAllUsingAll(subset, _firstList)); + Assert.IsTrue(CompareListsExamples.ContainsAllUsingExcept(subset, _firstList)); + Assert.IsTrue(CompareListsExamples.ContainsAllUsingHashSet(subset, _firstList)); + Assert.IsFalse(CompareListsExamples.ContainsAllUsingAll(_thirdList, _firstList)); + Assert.IsFalse(CompareListsExamples.ContainsAllUsingExcept(_thirdList, _firstList)); + Assert.IsFalse(CompareListsExamples.ContainsAllUsingHashSet(_thirdList, _firstList)); + } +}