diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..e6dab33 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -15,141 +15,451 @@ 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 == null) + throw new ArgumentNullException(); + + long sum = 0; + + foreach (int value in arr) + { + sum += value; + } + + 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 == null) + throw new ArgumentNullException(); + + long sum = 0; + + foreach (int value in arr) + { + if (value % 2 != 0) + { + sum += value; + } + } + + 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 == null) + throw new ArgumentNullException(); + + long sum = 0; + int count = 0; + + foreach (int value in arr) + { + count++; + if (count % 2 == 0) + { + sum += value; + } + } + + return sum; } public static IEnumerable GetUniqueValues(IEnumerable arr) { - // return an array that contains only unique values from the passed in array - // TODO - return null; + List uniqueValuesArray = new List(); + + if (arr == null) + throw new ArgumentNullException(); + + foreach (int value in arr) + { + if (!uniqueValuesArray.Contains(value)) + { + uniqueValuesArray.Add(value); + } + } + + return uniqueValuesArray; } 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; + if (arrA == null || arrB == null) + throw new ArgumentNullException(); + + List intersectArray = new List(); + + foreach (int valueA in arrA) + { + foreach (int valueB in arrB) + { + if (valueA == valueB && !intersectArray.Contains(valueA)) + { + intersectArray.Add(valueA); + break; + } + } + } + + return intersectArray; } 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; + if (arrA == null || arrB == null) + throw new ArgumentNullException(); + List listArrA = new List(arrA); + List listArrB = new List(arrB); + List notIntersectArray = new List(); + + foreach (int valueA in listArrA) + { + if (!listArrB.Contains(valueA)) + { + notIntersectArray.Add(valueA); + } + } + + foreach (int valueB in listArrB) + { + if (!listArrA.Contains(valueB)) + { + notIntersectArray.Add(valueB); + } + } + + return notIntersectArray; } 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 || target == null) + throw new NullReferenceException(); + + bool hasSum = false; + int[] array = arr.ToArray(); + int count = 1; + + for(int a = 0; a < array.Length; a++) + { + for (int i = count; i < array.Length; i++) + { + if (a != i) + { + if (array[a] + array[i] == target) + { + hasSum = true; + break; + } + } + } + + if (hasSum) + 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 == null) + throw new ArgumentNullException(); + + Dictionary arrayValues = new Dictionary(); + + long sum = 0; + + foreach (int value in arr) + { + if (!arrayValues.ContainsKey(value)) + { + arrayValues.Add(value, 1); + sum += value; + } + else + { + if (arrayValues[value] == 1) + { + sum -= value; + } + + arrayValues[value]++; + } + } + + 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; + string returnString = string.Empty; + + if (s != null) + { + foreach (char sChar in s) + { + returnString += sChar + "" + sChar; + } + } + else + { + throw new ArgumentNullException(); + } + + return returnString; } public static int CountChars(String s, char c) { - // return the count of how many times char c occurs in string s - return 0; + int charCount = 0; + + if (s != null) + { + foreach (char sChar in s) + { + if (sChar == c) + charCount++; + } + } + else + { + throw new ArgumentNullException(); + } + + return charCount; } 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 (char sChar in s) + { + int parsedChar; + + if(Int32.TryParse(sChar.ToString(), out parsedChar)) + { + sum += parsedChar; + } + } + + return 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; + if (s == null) + throw new ArgumentNullException(); + + long sum = 0; + int parsedChar; + string currentNumber = string.Empty; + + foreach (char sChar in s) + { + if (Int32.TryParse(sChar.ToString(), out parsedChar)) + { + currentNumber += "" + sChar; + } + else + { + if (currentNumber != string.Empty) + { + sum += Int32.Parse(currentNumber); + currentNumber = string.Empty; + } + } + } + + if (currentNumber != string.Empty) + { + sum += Int32.Parse(currentNumber); + currentNumber = string.Empty; + } + + return 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 == null || s2 == null) + throw new NullReferenceException(); + + char[] sortedArrayOfS1 = s1.ToArray(); + char[] sortedArrayOfS2 = s2.ToArray(); + + Array.Sort(sortedArrayOfS1); + Array.Sort(sortedArrayOfS2); + + s1 = new String(sortedArrayOfS1); + s2 = new String(sortedArrayOfS2); + + return s1.Equals(s2); } 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; + int returnValue; + const int Twenty_One = 21; + + if (count1 > Twenty_One && count2 > Twenty_One) + { + returnValue = 0; + } + else if(count1 > Twenty_One) + { + returnValue = count2; + } + else if (count2 > Twenty_One) + { + returnValue = count1; + } + else if (count1 > count2) + { + returnValue = count1; + } + else + { + returnValue = count2; + } + + return returnValue; } 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; + int returnValue; + const int Twenty_One = 21; + int[] values = new int[] { count1, count2, count3, count4, count5 }; + + if (count1 > Twenty_One && count2 > Twenty_One && count3 > Twenty_One && count4 > Twenty_One && count5 > Twenty_One) + { + returnValue = 0; + } + else + { + if (values[0] <= 21) + returnValue = values[0]; + else + returnValue = 0; + + for (int i = 1; i < values.Length; i++) + { + if (values[i] > values[i - 1] && values[i] > returnValue && values[i] <= Twenty_One) + { + returnValue = values[i]; + } + } + } + return returnValue; } 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 == null) + throw new ArgumentNullException(); + + int returnValue = 0; + const int Twenty_One = 21; + int[] values = counts.ToArray(); + + if (values.Length == 0 || values[0] > 21) + returnValue = 0; + else + returnValue = values[0]; + + for (int i = 1; i < values.Length; i++) + { + if (returnValue == 0 && values[i] > 21) + { + returnValue = 0; + } + else + { + if (values[i] > values[i - 1] && values[i] > returnValue && values[i] <= Twenty_One) + { + returnValue = values[i]; + } + } + } + + return returnValue; } 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; + Dictionary stringCount = new Dictionary(); + + foreach (String strValue in arr) + { + if (!stringCount.ContainsKey(strValue)) + { + stringCount.Add(strValue, 1); + } + else + { + stringCount[strValue]++; + } + } + + return stringCount ; } - public static int Factorial(int n) + public static long Factorial(int n) { - // Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1 - return 0; + long sum = n; + + if (n > 0) + { + for (int i = n - 1; i > 1; i--) + { + sum = sum * i; + } + } + else + { + sum = 1; + } + + return sum; } 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; + List returnString = new List(); + const string divisibleBy3 = "Fizz"; + const string divisibleBy5 = "Buzz"; + const string divisibleBy3And5 = "FizzBuzz"; + + for(int i=1; i <= n; i++) + { + if (i % 3 == 0 && i % 5 == 0) + { + returnString.Add(divisibleBy3And5); + } + else if (i % 3 == 0) + { + returnString.Add(divisibleBy3); + } + else if (i % 5 == 0) + { + returnString.Add(divisibleBy5); + } + else + { + returnString.Add(""+i); + } + } + + return returnString; } } }