diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..9b7c464 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -9,147 +9,349 @@ namespace problem_solving { public class Program { + private const int BlackjackLimit = 21; + static void Main(string[] args) { } + private static void ThrowExceptionIfNull(object arr, bool throwNullReferenceInstead = false) + { + if (arr == null) + { + if (throwNullReferenceInstead) + { + throw new NullReferenceException(); + } + + throw new ArgumentNullException(); + } + } + public static long SumArray(IEnumerable arr) { - // return the sum of all the values in the array - // TODO - return 0; + ThrowExceptionIfNull(arr); + + return Array.ConvertAll(arr.ToArray(), i => (long)i).Sum(); } public static long SumArrayOddValues(IEnumerable arr) { - // return the sum of all the values in the array that are odd - // TODO - return 0; + ThrowExceptionIfNull(arr); + + return arr.Sum(v => (v % 2 == 0) ? 0 : v); } 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; + ThrowExceptionIfNull(arr); + + return arr.Select((value, index) => new { value, index }).Sum(x => (x.index % 2 == 1) ? x.value : 0); } public static IEnumerable GetUniqueValues(IEnumerable arr) { - // return an array that contains only unique values from the passed in array - // TODO - return null; + ThrowExceptionIfNull(arr); + + 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; + ThrowExceptionIfNull(arrA); + ThrowExceptionIfNull(arrB); + + 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; + ThrowExceptionIfNull(arrA); + ThrowExceptionIfNull(arrB); + + var nonIntersectingValues = new List(); + + foreach (var valueA in arrA) + { + if (!arrB.Contains(valueA)) + { + nonIntersectingValues.Add(valueA); + } + } + + foreach (var valueB in arrB) + { + if (!arrA.Contains(valueB)) + { + nonIntersectingValues.Add(valueB); + } + } + + return nonIntersectingValues; } 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 + ThrowExceptionIfNull(arr, true); + + //var arr.Select((value, index) => new { value, index }) + + var array = arr.ToArray(); + + for (int i = 0; i < array.Count(); i++) + { + var element1 = array[i]; + + for (int j = 0; j < array.Count(); j++) + { + if (i == j) + { + continue; + } + + var element2 = array[j]; + + if (element1 + element2 == target) + { + return true; + } + } + } + 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; + ThrowExceptionIfNull(arr); + + return arr.Where(x => arr.Count(c => c == x) == 1).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; + ThrowExceptionIfNull(s); + + string doubledString = ""; + + foreach (var c in s) + { + doubledString += c.ToString() + c.ToString(); + } + + return doubledString; } public static int CountChars(String s, char c) { - // return the count of how many times char c occurs in string s - return 0; + ThrowExceptionIfNull(s); + + return s.Count(l => l == 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; + ThrowExceptionIfNull(s); + + return s.Sum(c => Char.IsNumber(c) ? int.Parse(c.ToString()) : 0); } 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; + ThrowExceptionIfNull(s); + + var numbersToAdd = new List(); + var currentNumberString = string.Empty; + + foreach (var c in s) + { + if (Char.IsNumber(c)) + { + currentNumberString += c.ToString(); + } + else + { + if (!string.IsNullOrEmpty(currentNumberString)) + { + numbersToAdd.Add(int.Parse(currentNumberString)); + currentNumberString = string.Empty; + } + } + } + + if (!string.IsNullOrEmpty(currentNumberString)) + { + numbersToAdd.Add(int.Parse(currentNumberString)); + } + + return numbersToAdd.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; + foreach (var c in s1) + { + var characterIndex = s2.IndexOf(c); + + if (characterIndex == -1) + { + return false; + } + + s2 = RemoveIndex(s2, characterIndex); + } + + return (s2.Length == 0); + } + + private static string RemoveIndex(string s, int index) + { + var firstPartOfString = s.Substring(0, index); + + if (index + 1 == s.Length) + { + return firstPartOfString; + } + + var lastPartOfString = s.Substring(index + 1); + + return firstPartOfString + lastPartOfString; } 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 > BlackjackLimit && count2 > BlackjackLimit) + { + return 0; + } + + if (count1 > BlackjackLimit) + { + return count2; + } + + if (count2 > BlackjackLimit) + { + return count1; + } + + return Math.Max(count1, count2); } 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 numbers = new List();; + + if (count1 <= BlackjackLimit) + { + numbers.Add(count1); + } + + if (count2 <= BlackjackLimit) + { + numbers.Add(count2); + } + + if (count3 <= BlackjackLimit) + { + numbers.Add(count3); + } + + if (count4 <= BlackjackLimit) + { + numbers.Add(count4); + } + + if (count5 <= BlackjackLimit) + { + numbers.Add(count5); + } + + if (numbers.Count == 0) + { + return 0; + } + + return numbers.Max(); } 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; + var validCounts = counts.Where(n => n <= 21); + + if (validCounts.Count() == 0) + { + return 0; + } + + return validCounts.Max(); } 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 (var word in arr) + { + if (!dictionary.ContainsKey(word)) + { + dictionary.Add(word, 1); + } + else + { + dictionary[word] += 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; + } + + var factorial = n; + + for (int i = n - 1; i >= 1; i--) + { + factorial *= i; + } + + return factorial; } 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 listOfStrings = new List(); + + for (int i = 1; i <= n; i++) + { + var divisibleBy3 = (i % 3 == 0); + var divisibleBy5 = (i % 5 == 0); + + if (divisibleBy3 && divisibleBy5) + { + listOfStrings.Add("FizzBuzz"); + } + else if (divisibleBy3) + { + listOfStrings.Add("Fizz"); + } + else if (divisibleBy5) + { + listOfStrings.Add("Buzz"); + } + else + { + listOfStrings.Add(i.ToString()); + } + } + + return listOfStrings; } } }