diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..fb3584d 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -16,77 +16,224 @@ static void Main(string[] args) public static long SumArray(IEnumerable arr) { // return the sum of all the values in the array - // TODO - return 0; + if (!arr.Any()) + return 0; + + long sum = 0; + foreach (var i in arr) + { + sum += i; + } + return sum; } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd - // TODO - return 0; + if (!arr.Any()) + return 0; + + long sum = 0; + foreach (var i in arr) + { + if (i % 2 == 1 || i % 2 == -1) + { + sum += i; + } + } + return sum; } 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; + if (!arr.Any()) + return 0; + + long sum = 0; + for (int i = 0; i < arr.Count(); i++) + { + if ((i + 1) % 2 == 0) + { + sum += arr.ElementAt(i); + } + } + return sum; } public static IEnumerable GetUniqueValues(IEnumerable arr) { // return an array that contains only unique values from the passed in array - // TODO - return null; + var uniqueArray = new List(); + if (!arr.Any()) + return uniqueArray; + + foreach (var i in arr) + { + if (!uniqueArray.Contains(i)) + { + uniqueArray.Add(i); + } + } + return uniqueArray; } 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; + var uniqueArray = new List(); + if (!arrA.Any() && !arrB.Any()) + return uniqueArray; + + foreach (var i in arrA) + { + if (!uniqueArray.Contains(i)) + { + if (arrB.Contains(i)) + uniqueArray.Add(i); + } + } + + return uniqueArray; } 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; + var uniqueArray = new List(); + if (!arrA.Any() && !arrB.Any()) + return uniqueArray; + + foreach (var i in arrA) + { + if (!uniqueArray.Contains(i)) + { + if (!arrB.Contains(i)) + uniqueArray.Add(i); + } + } + + foreach (var i in arrB) + { + if (!uniqueArray.Contains(i)) + { + if (!arrA.Contains(i)) + uniqueArray.Add(i); + } + } + + return uniqueArray; } public static Boolean HasSum(IEnumerable arr, long target) { // return true if any 2 values in the array have a sum equal to the target value - // TODO - return false; + if (arr == null) + throw new NullReferenceException(); + + if (!arr.Any()) + return false; + + var hasSum = false; + foreach (var i in arr) + { + var diff = target - i; + if (arr.Contains(Convert.ToInt32(diff))) + { + if (diff == i) + { + try + { + //if there is only one value that matches then we can't sum + int singleValue = arr.Single(num => num == diff); + } + catch (InvalidOperationException) + { + //there are multiples, which means we can sum two values + hasSum = true; + break; + } + } + else + { + hasSum = true; + break; + } + } + } + + return hasSum; } 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; + if (!arr.Any()) + return 0; + + var uniqueArray = new List(); + long sum = 0; + foreach (var i in arr) + { + try + { + //if there is only one value that matches then add it to the sum + int singleValue = arr.Single(num => num == i); + sum += i; + } + catch (InvalidOperationException) + { + //there are multiple instances of this value, so do not include it in the sum + } + } + return sum; } public static String DoubleString(String 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; + if(s == null) + throw new ArgumentNullException(); + + var outputString = new StringBuilder(); + foreach (var c in s) + { + outputString.Append(c, 2); + } + return outputString.ToString(); } public static int CountChars(String s, char c) { // return the count of how many times char c occurs in string s - return 0; + if (s == null) + throw new ArgumentNullException(); + + var sum = 0; + foreach (var character in s) + { + if (character == c) + sum++; + } + return sum; } public static long SumDigits(String 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; + if (s == null) + throw new ArgumentNullException(); + + long sum = 0; + foreach (var c in s) + { + if (Char.IsDigit(c)) + sum += Convert.ToInt32(Char.GetNumericValue(c)); + } + return sum; } public static long SumNumbers(String s) @@ -94,7 +241,25 @@ 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; + if (s == null) + throw new ArgumentNullException(); + + long sum = 0; + var value = string.Empty; + var charArray = s.ToCharArray(); + for (int i = 0; i < charArray.Length; i++) + { + if (Char.IsDigit(charArray[i])) + { + value += charArray[i]; + if (i + 1 == charArray.Length || !Char.IsDigit(charArray[i+1])) + { + sum += Convert.ToInt32(value); + value = string.Empty; + } + } + } + return sum; } public static Boolean IsAnagram(String s1, String s2) @@ -103,7 +268,17 @@ 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; + if (s1 == null || s2 == null) + throw new NullReferenceException(); + + if (s1.Length != s2.Length) + return false; + + var charArray1 = s1.ToCharArray(); + var charArray2 = s2.ToCharArray(); + Array.Sort(charArray1); + Array.Sort(charArray2); + return charArray1.SequenceEqual(charArray2); } public static int BlackJack(int count1, int count2) @@ -111,7 +286,16 @@ 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; + if (count1 > 21 && count2 > 21) + return 0; + + if (count1 > 21) + return count2; + + if (count2 > 21) + return count1; + + return count1 > count2 ? count1 : count2; } public static int FivePlayerBlackJack(int count1, int count2, int count3, int count4, int count5) @@ -119,6 +303,14 @@ 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. + var playerHands = new int[] {count1, count2, count3, count4, count5}; + Array.Sort(playerHands); + for (int i = (playerHands.Length - 1); i >= 0; i--) + { + if (playerHands[i] <= 21) + return playerHands[i]; + } + return 0; } @@ -127,6 +319,14 @@ 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. + var playerHands = counts.ToArray(); + Array.Sort(playerHands); + for (int i = (playerHands.Length - 1); i >= 0; i--) + { + if (playerHands[i] <= 21) + return playerHands[i]; + } + return 0; } @@ -134,7 +334,24 @@ public static Dictionary WordCount(IEnumerable 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; + if (arr == null) + throw new NullReferenceException(); + + var output = new Dictionary(); + foreach (var word in arr) + { + var count = 1; + if (output.ContainsKey(word)) + { + count = output[word]; + output[word] = count + 1; + } + else + { + output.Add(word, count); + } + } + return output; } public static int Factorial(int n)