diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..3f9748c 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -15,141 +15,188 @@ static void Main(string[] args) public static long SumArray(IEnumerable arr) { - // return the sum of all the values in the array - // TODO - return 0; + return arr.Select(n => (long)n).Sum(); } public static long SumArrayOddValues(IEnumerable arr) { - // return the sum of all the values in the array that are odd - // TODO - return 0; + return arr.Where(number => number % 2 != 0).Select(n => (long)n).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; + return arr.Where((number, ix) => ix % 2 != 0).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.Distinct().Where(number => arrB.Contains(number)); } 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.Except(arrB).Concat(arrB.Except(arrA)); } 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 + var set = new HashSet(); + foreach (int number in arr) + { + if(set.Contains((int)target-number)) + { + return true; + } + set.Add(number); + } return false; } 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 == null) throw new ArgumentNullException(); + + var addedNums = new HashSet(); + var duplicates = new HashSet(); + int sum = 0; + foreach (int number in arr) + { + if(!addedNums.Contains(number)) + { + sum += number; + addedNums.Add(number); + } + else if (!duplicates.Contains(number)) + { + duplicates.Add(number); + sum -= number; + } + } + 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(); + + char[] charArray = new char[s.Length*2]; + int ix = 0; + foreach (char c in s) + { + charArray[ix++] = c; + charArray[ix++] = c; + } + return new String(charArray); } 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.Count(charInString => charInString == c); } 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; + return s.Where(charInString => charInString > '0' && charInString <= '9') + .Select(charVal => charVal - '0') + .Sum(); } 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; + String[] numbers = Regex.Split(s, "[^.0-9]"); + return numbers.Where(stringVal => stringVal != "") + .Select(stringVal => Int32.Parse(stringVal)) + .Sum(); } public static Boolean IsAnagram(String s1, String s2) { - // return true if String s1 is an anagram of s2, otherwise return false - // 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.Length != s2.Length) return false; + foreach(char s1Char in s1) + { + int matchingIx = s2.IndexOf(s1Char); + if (matchingIx < 0) + { + return false; + } + else + { + s2 = s2.Remove(matchingIx, 1); + } + } + return true; } 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; + var counts = new List(); + counts.Add(count1); + counts.Add(count2); + return NPlayerBlackJack(counts); } public static int FivePlayerBlackJack(int count1, int count2, int count3, int count4, int count5) { - // 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; + var counts = new List(); + counts.Add(count1); + counts.Add(count2); + counts.Add(count3); + counts.Add(count4); + counts.Add(count5); + return NPlayerBlackJack(counts); } 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; + IEnumerable validHands = counts.Where(val => val <= 21 && val >= 0); + + return validHands.Count() > 0 ? validHands.Max() : 0; } 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; + var dictionary = new Dictionary(); + foreach(String word in arr) + { + int currentCount; + dictionary.TryGetValue(word, out currentCount); + dictionary[word] = currentCount + 1; + } + return dictionary; } public static int Factorial(int n) { - // Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1 - return 0; + if (n <= 0) return 1; + return n * Factorial(n - 1); } public static List FB(int n) { - // Given n, print the numbers from 1 to n as a string to a List of strings, with the following exceptions: - // 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; + var output = new List(); + for (int i = 1; i <= n; i++) + { + String thisString = ""; + if (i % 3 == 0) + { + thisString += "Fizz"; + } + if (i % 5 == 0) + { + thisString += "Buzz"; + } + if (thisString.Length == 0) + { + thisString = i.ToString(); + } + output.Add(thisString); + } + return output; } } }