From 83afa475fd90c0ba6c5baacf0e59c41dbb894061 Mon Sep 17 00:00:00 2001 From: ilms Date: Sun, 23 Feb 2020 16:32:23 +0200 Subject: [PATCH] solved C# test --- csharp/CDSPractical/Constants.cs | 7 ++ csharp/CDSPractical/Questions.cs | 191 ++++++++++++++++++++---------- csharp/CDSPracticalTests/Tests.cs | 75 ++++++++---- 3 files changed, 189 insertions(+), 84 deletions(-) create mode 100644 csharp/CDSPractical/Constants.cs diff --git a/csharp/CDSPractical/Constants.cs b/csharp/CDSPractical/Constants.cs new file mode 100644 index 0000000..7701a29 --- /dev/null +++ b/csharp/CDSPractical/Constants.cs @@ -0,0 +1,7 @@ +namespace CDSPractical +{ + public static class Constants + { + public const double KILOMETERS_IN_MILE = 1.6; + } +} \ No newline at end of file diff --git a/csharp/CDSPractical/Questions.cs b/csharp/CDSPractical/Questions.cs index b6c1f8f..8b50617 100644 --- a/csharp/CDSPractical/Questions.cs +++ b/csharp/CDSPractical/Questions.cs @@ -1,19 +1,22 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading; -namespace CDSPractical { - public class Questions { +namespace CDSPractical +{ + public class Questions + { /// - /// Given an enumerable of strings, attempt to parse each string and if + /// Given an enumerable of strings, attempt to parse each string and if /// it is an integer, add it to the returned enumerable. - /// + /// /// For example: - /// + /// /// ExtractNumbers(new List { "123", "hello", "234" }); - /// + /// /// ; would return: - /// + /// /// { /// 123, /// 234 @@ -21,15 +24,22 @@ public class Questions { /// /// An enumerable containing words /// - public IEnumerable ExtractNumbers(IEnumerable source) { - throw new NotImplementedException(); + public IEnumerable ExtractNumbers(IEnumerable source) + { + foreach (var item in source) + { + if (int.TryParse(item, out int number)) + { + yield return number; + } + } } /// /// Given two enumerables of strings, find the longest common word. - /// + /// /// For example: - /// + /// /// LongestCommonWord( /// new List { /// "love", @@ -60,80 +70,88 @@ public IEnumerable ExtractNumbers(IEnumerable source) { /// "ear" /// } /// ); - /// + /// /// ; would return "wandering" as the longest common word. /// /// First list of words /// Second list of words /// - public string LongestCommonWord(IEnumerable first, IEnumerable second) { - throw new NotImplementedException(); + public string LongestCommonWord(IEnumerable first, IEnumerable second) + { + return first.Intersect(second, StringComparer.OrdinalIgnoreCase) + .OrderByDescending(x => x.Length) + .FirstOrDefault(); } /// /// Write a method that converts kilometers to miles, given that there are /// 1.6 kilometers per mile. - /// + /// /// For example: - /// + /// /// DistanceInMiles(16.00); - /// + /// /// ; would return 10.00; /// /// distance in kilometers /// - public double DistanceInMiles(double km) { - throw new NotImplementedException(); + public double DistanceInMiles(double km) + { + return km / Constants.KILOMETERS_IN_MILE; } /// /// Write a method that converts miles to kilometers, give that there are /// 1.6 kilometers per mile. - /// + /// /// For example: - /// + /// /// DistanceInKm(10.00); - /// + /// /// ; would return 16.00; /// /// distance in miles /// - public double DistanceInKm(double miles) { - throw new NotImplementedException(); + public double DistanceInKm(double miles) + { + return miles * Constants.KILOMETERS_IN_MILE; } /// /// Write a method that returns true if the word is a palindrome, false if /// it is not. - /// + /// /// For example: - /// + /// /// IsPalindrome("bolton"); - /// + /// /// ; would return false, and: - /// + /// /// IsPalindrome("Anna"); - /// + /// /// ; would return true. - /// + /// /// Also complete the related test case for this method. /// /// The word to check /// - public bool IsPalindrome(string word) { - throw new NotImplementedException(); + public bool IsPalindrome(string word) + { + word = word.ToLower(); + + return word.SequenceEqual(word.Reverse()); } /// /// Write a method that takes an enumerable list of objects and shuffles /// them into a different order. - /// + /// /// For example: - /// + /// /// Shuffle(new List{ "one", "two" }); - /// + /// /// ; would return: - /// + /// /// { /// "two", /// "one" @@ -141,70 +159,121 @@ public bool IsPalindrome(string word) { /// /// /// - public IEnumerable Shuffle(IEnumerable source) { - throw new NotImplementedException(); + public IEnumerable Shuffle(IEnumerable source) + { + var elements = source.ToList(); + var randomizer = new Random(); + + for (int i = elements.Count - 1; i >= 0; i--) + { + int swapIndex = randomizer.Next(i + 1); + yield return elements[swapIndex]; + elements[swapIndex] = elements[i]; + } } /// /// Write a method that sorts an array of integers into ascending /// order - do not use any built in sorting mechanisms or frameworks. - /// + /// /// Complete the test for this method. /// /// /// - public int[] Sort(int[] source) { - throw new NotImplementedException(); - } + public int[] Sort(int[] source) + { + int temp; + + for (int write = 0; write < source.Length; write++) + { + for (int sort = 0; sort < source.Length - 1; sort++) + { + if (source[sort] > source[sort + 1]) + { + temp = source[sort + 1]; + source[sort + 1] = source[sort]; + source[sort] = temp; + } + } + } + + return source; + } /// - /// Each new term in the Fibonacci sequence is generated by adding the + /// Each new term in the Fibonacci sequence is generated by adding the /// previous two terms. By starting with 1 and 2, the first 10 terms will be: /// /// 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... /// - /// By considering the terms in the Fibonacci sequence whose values do + /// By considering the terms in the Fibonacci sequence whose values do /// not exceed four million, find the sum of the even-valued terms. /// /// - public int FibonacciSum() { - throw new NotImplementedException(); + public int FibonacciSum() + { + int previousTerm = 1; + int currentTerm = 2; + int sum = 0; + + while (currentTerm < 4000000) + { + if (currentTerm % 2 == 0) + { + sum += currentTerm; + } + + (currentTerm, previousTerm) = (currentTerm + previousTerm, currentTerm); + } + + return sum; } /// /// Generate a list of integers from 1 to 100. - /// + /// /// This method is currently broken, fix it so that the tests pass. /// /// - public IEnumerable GenerateList() { + public IEnumerable GenerateList() + { var ret = new List(); var numThreads = 2; + var lockObject = new object(); Thread[] threads = new Thread[numThreads]; - for (var i = 0; i < numThreads; i++) { - threads[i] = new Thread(() => { + for (var i = 0; i < numThreads; i++) + { + threads[i] = new Thread(() => + { var complete = false; - while (!complete) { - var next = ret.Count + 1; - Thread.Sleep(new Random().Next(1, 10)); - if (next <= 100) { - ret.Add(next); - } + while (!complete) + { + lock (lockObject) + { + var next = ret.Count + 1; + Thread.Sleep(new Random().Next(1, 10)); + if (next <= 100) + { + ret.Add(next); + } - if (ret.Count >= 100) { - complete = true; + if (ret.Count >= 100) + { + complete = true; + } } - } + } }); threads[i].Start(); } - for (var i = 0; i < numThreads; i++) { + for (var i = 0; i < numThreads; i++) + { threads[i].Join(); } return ret; } } -} +} \ No newline at end of file diff --git a/csharp/CDSPracticalTests/Tests.cs b/csharp/CDSPracticalTests/Tests.cs index dac6bfd..73d84c1 100644 --- a/csharp/CDSPracticalTests/Tests.cs +++ b/csharp/CDSPracticalTests/Tests.cs @@ -1,14 +1,18 @@ using CDSPractical; using System; using System.Collections.Generic; +using System.Linq; using Xunit; -namespace CDSPracticalTests { - public class Tests { - Questions instance = new Questions(); +namespace CDSPracticalTests +{ + public class Tests + { + private Questions instance = new Questions(); [Fact] - public void CanExtractNumbers() { + public void CanExtractNumbers() + { Assert.Equal(new List { 123, 234 @@ -18,7 +22,7 @@ public void CanExtractNumbers() { "234" })); - Assert.Equal(new List {}, instance.ExtractNumbers(new List { + Assert.Equal(new List { }, instance.ExtractNumbers(new List { "hello", "there" })); @@ -36,7 +40,8 @@ public void CanExtractNumbers() { } [Fact] - public void CanGetLongestCommonWord() { + public void CanGetLongestCommonWord() + { Assert.Equal("wandering", instance.LongestCommonWord( new List { "love", @@ -70,57 +75,81 @@ public void CanGetLongestCommonWord() { } [Fact] - public void CanGetDistanceInMiles() { + public void CanGetDistanceInMiles() + { Assert.Equal(10.00, instance.DistanceInMiles(16.00)); } [Fact] - public void CanGetDistanceInKilometers() { + public void CanGetDistanceInKilometers() + { Assert.Equal(16.00, instance.DistanceInKm(10.00)); } [Fact] - public void IsPalindrome() { - var palindromes = new List { - + public void IsPalindrome() + { + var palindromes = new List + { + "Anna", + "Test1tset", + "123$321" }; - var invalid = new List { - + var invalid = new List + { + "bolton", + "Test1test", + "123$123" }; - foreach (var word in palindromes) { + foreach (var word in palindromes) + { Assert.True(instance.IsPalindrome(word)); } - foreach (var word in invalid) { + foreach (var word in invalid) + { Assert.False(instance.IsPalindrome(word)); } } [Fact] - public void CanShuffle() { - Assert.Equal(new List { "two", "one" }, instance.Shuffle(new List { "one", "two" })); + public void CanShuffle() + { + var initialList = new List { "one", "two", "three", "four", "five" }; + var shuffledList = instance.Shuffle(initialList); + + Assert.False(initialList.SequenceEqual(shuffledList)); } [Fact] - public void CanSort() { - throw new NotImplementedException(); + public void CanSort() + { + int[] randomArray = { 7, 4, 2, 12, 8 }; + int[] sortedArray = { 2, 4, 7, 8, 12 }; + + randomArray = instance.Sort(randomArray); + + Assert.True(sortedArray.SequenceEqual(randomArray)); } [Fact] - public void CanSumFibonacciNumbers() { + public void CanSumFibonacciNumbers() + { Assert.Equal(4613732, instance.FibonacciSum()); } [Fact] - public void CanGenerateListOfNumbers() { + public void CanGenerateListOfNumbers() + { var list = instance.GenerateList(); var current = 1; - foreach (var num in list) { + foreach (var num in list) + { Assert.Equal(current, num); current++; } } } -} +} \ No newline at end of file