diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..efb57ba 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -13,80 +13,150 @@ static void Main(string[] args) { } + public static void NullCheck(Object arr) + { + if (arr == null) + throw new ArgumentNullException("arr"); + } + public static long SumArray(IEnumerable arr) { // return the sum of all the values in the array - // TODO - return 0; + + NullCheck(arr); + + long sum = 0; + foreach (var item in arr) + { + sum += item; + } + return sum; + + + //return arr.Sum; } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd - // TODO - return 0; + NullCheck(arr); + long sum = 0; + foreach (var item in arr) + { + if (item % 2 == 1) + { + sum += item; + } + } + 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; + NullCheck(arr); + long sum = 0; + for (int i = 1; i < arr.Count(); i += 2) + { + sum += arr.ToArray()[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; + NullCheck(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; + NullCheck(arrA); + NullCheck(arrB); + + return arrA.Intersect(arrB); } + class NotInteresectCompare : IEqualityComparer + { + public bool Equals(int x, int y) + { + return x != y; + } + public int GetHashCode(int codeh) + { + return codeh.GetHashCode(); + } + } + 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; + NullCheck(arrA); + NullCheck(arrB); + + return arrA.Intersect(arrB, new NotInteresectCompare()); } 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; + + + 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; + NullCheck(arr); + return arr.Distinct().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; + NullCheck(s); + string output = ""; + foreach (var item in s) + { + output += item.ToString() + item.ToString(); + } + return output; } public static int CountChars(String s, char c) { + // return the count of how many times char c occurs in string s - return 0; + int output = 0; + foreach (char item in s) + { + if (item == c) + output++; + } + return output; } 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; + long output = 0; + foreach (char item in s) + { + if (char.IsNumber(item)) + { + output += int.Parse(item.ToString()); + } + } + return output; } public static long SumNumbers(String s) @@ -124,6 +194,7 @@ public static int FivePlayerBlackJack(int count1, int count2, int count3, int co public static int NPlayerBlackJack(IEnumerable counts) { + NullCheck(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. @@ -132,6 +203,7 @@ public static int NPlayerBlackJack(IEnumerable counts) public static Dictionary WordCount(IEnumerable arr) { + NullCheck(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;