From c336436b7464d3a637dbb67f6dd6b8d5cc235c37 Mon Sep 17 00:00:00 2001 From: Glen Miller Date: Thu, 7 Jul 2016 12:57:20 -0500 Subject: [PATCH] Problem solving training solution --- UnitTestProject1/UnitTest1.cs | 2 +- problem-solving/Program.cs | 158 ++++++++++++++++++++++++++++------ 2 files changed, 133 insertions(+), 27 deletions(-) diff --git a/UnitTestProject1/UnitTest1.cs b/UnitTestProject1/UnitTest1.cs index cb605f9..6323fbf 100644 --- a/UnitTestProject1/UnitTest1.cs +++ b/UnitTestProject1/UnitTest1.cs @@ -75,7 +75,7 @@ public void Test_GetUniqueValues(int[] arr, int[] result) [TestCase(new int[] { }, new int[] { }, new int[] { })] [TestCase(null, null, null, ExpectedException = typeof(ArgumentNullException))] [TestCase(new int[] { 1, 2 }, new int[] { 3, 4 }, new int[] { })] - public void Test_IntersectArrays(int[] arrA, int[] arrB, int[] result) + public void Test_GetArrayIntersect(int[] arrA, int[] arrB, int[] result) { Assert.AreEqual(result, Program.GetArrayIntersect(arrA, arrB)); } diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..5cd0c33 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -13,52 +13,86 @@ static void Main(string[] args) { } + private static bool IsEven(int i) + { + return i % 2 == 0; + } + + private static bool IsOdd(int i) + { + return !IsEven(i); + } + public static long SumArray(IEnumerable arr) { + if (arr == null) + { + throw new ArgumentNullException("ar"); + } + // return the sum of all the values in the array - // TODO - return 0; + + //x return arr.Sum(); + //x return arr.Aggregate((sum, i) => unchecked(sum + i)); + + long result = 0; + + foreach (int a in arr) + { + result += a; + } + + return result; } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd - // TODO - return 0; + return arr.Sum(a => IsOdd(a) ? a : 0); } public static long SumArrayEverySecondValue(IEnumerable arr) { - // return the sum of every second value in the array. i.e. the 2nd value + the 4th value + the 6th value ... - // TODO - return 0; + return arr.Where((a, i) => IsOdd(i)).Sum(); } public static IEnumerable GetUniqueValues(IEnumerable arr) { // return an array that contains only unique values from the passed in array - // TODO - return null; + return arr.Distinct(); } public static IEnumerable GetArrayIntersect(IEnumerable arrA, IEnumerable arrB) { // return an array that contains all the values that are in array A and array B - // TODO - return null; + return arrA.Intersect(arrB); } public static IEnumerable GetArrayNotIntersect(IEnumerable arrA, IEnumerable arrB) { // return an array that contains all the values that are in array A or array B but not in both array A and array B - // TODO - return null; + return arrA.Union(arrB).Except(arrA.Intersect(arrB)); } public static Boolean HasSum(IEnumerable arr, long target) { + if (arr == null) + { + throw new NullReferenceException("arr"); + } + // return true if any 2 values in the array have a sum equal to the target value - // TODO + for (int i = 0; i < arr.Count(); i++) + { + for (int j = 0; j < arr.Count(); j++) + { + if (i != j && arr.ElementAt(i) + arr.ElementAt(j) == target) + { + return true; + } + } + } + return false; } @@ -66,27 +100,47 @@ public static long LoneSum(IEnumerable arr) { // Given an array of int values, return their sum. // However, if any of the values is the same as another of the values, it does not count towards the sum. - return 0; + return arr.GroupBy(x => x) + .Where(g => g.Count() < 2) + .Select(g => g.Key) + .Sum(); } public static String DoubleString(String s) { + if (s == null) + { + throw new ArgumentNullException("s"); + } + // return a string that is the original string with each character in the string repeated twice // e.g. for input "ABCDE", return "AABBCCDDEE" - return null; + String result = ""; + + for (int i = 0; i < s.Length; i++) + { + result = result + s[i] + s[i]; + } + + return result; } public static int CountChars(String s, char c) { // return the count of how many times char c occurs in string s - return 0; + return s.AsEnumerable().Count(x => x == c); } public static long SumDigits(String s) { + if (s == null) + { + throw new ArgumentNullException("s"); + } + // return the sum of the digits 0-9 that appear in the string, ignoring all other characters // e.g. "123" return 6 - return 0; + return s.ToCharArray().Where(x => x >= '0' && x <= '9').Select(a => a - '0').Sum(); } public static long SumNumbers(String s) @@ -94,7 +148,20 @@ public static long SumNumbers(String s) // return the sum of the numbers that appear in the string, ignoring all other characters // a number is a series of 1 or more digits in a row // e.g. "11 22" returns 33 - return 0; + s = Regex.Replace(s, "[^0-9]", " "); + String[] arr = s.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); + return arr.Select(x => Int32.Parse(x)).Sum(); + } + + private static Boolean IsFirstAnagramOfSecond(String s1, String s2) + { + foreach (char c in s1) + { + if (s2.Count(x => x == c) != s1.Count(x => x == c)) + return false; + } + + return true; } public static Boolean IsAnagram(String s1, String s2) @@ -103,7 +170,7 @@ public static Boolean IsAnagram(String s1, String s2) // An anagram is produced by rearranging the letters of one string into another // e.g. care is an anagram of race // cat is not an anagram of rat - return false; + return IsFirstAnagramOfSecond(s1, s2) && IsFirstAnagramOfSecond(s2, s1); } public static int BlackJack(int count1, int count2) @@ -111,7 +178,7 @@ public static int BlackJack(int count1, int count2) // Given 2 integer values greater than 0, // return whichever value is nearest to 21 without going over. // Return 0 if they both go over. - return 0; + return NPlayerBlackJack(new int[] { count1, count2 }); } public static int FivePlayerBlackJack(int count1, int count2, int count3, int count4, int count5) @@ -119,7 +186,7 @@ public static int FivePlayerBlackJack(int count1, int count2, int count3, int co // Given 5 integer values greater than 0, // return whichever value is nearest to 21 without going over. // Return 0 if they all go over. - return 0; + return NPlayerBlackJack(new int[] { count1, count2, count3, count4, count5 }); } public static int NPlayerBlackJack(IEnumerable counts) @@ -127,20 +194,37 @@ public static int NPlayerBlackJack(IEnumerable counts) // Given a list of integer values greater than 0, // return whichever value is nearest to 21 without going over. // Return 0 if they all go over. - return 0; + if (counts.All(x => x > 21)) + { + return 0; + } + + return counts.Where(x => x <= 21).Max(); } public static Dictionary WordCount(IEnumerable arr) { + if (arr == null) + { + throw new NullReferenceException("arr"); + } + // Given an array of Strings, // return a dictionary keyed on the string with the count of how many times each string appears in the array - return null; + return arr.Distinct().ToDictionary(x => x, x => arr.Count(y => y == x)); } public static int Factorial(int n) { // Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1 - return 0; + int result = 1; + + for (int i = n; i > 0; i--) + { + result *= i; + } + + return result; } public static List FB(int n) @@ -149,7 +233,29 @@ public static List FB(int n) // If the number is divisable by 3, replace it with the word "Fizz" // If the number is divisable by 5, replace it with the word "Buzz" // If the number is divisable by both 3 and 5, replace it with the word "FizzBuzz" - return null; + List result = new List(); + + for (int i = 1; i <= n; i++) + { + if (i % 3 == 0 && i % 5 == 0) + { + result.Add("FizzBuzz"); + } + else if (i % 3 == 0) + { + result.Add("Fizz"); + } + else if (i % 5 == 0) + { + result.Add("Buzz"); + } + else + { + result.Add(Convert.ToString(i)); + } + } + + return result; } } }