diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..1b046cd 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -16,49 +16,123 @@ static void Main(string[] args) public static long SumArray(IEnumerable arr) { // return the sum of all the values in the array - // TODO - return 0; + long sum = 0; + + if (arr == null) + { + throw new ArgumentNullException(); + } + + foreach (int arrInt in arr) + { + sum += arrInt; + } + + return sum; } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd - // TODO - return 0; + long sum = 0; + + if (arr == null) + { + throw new ArgumentNullException(); + } + + foreach (int arrInt in arr) + { + if (arrInt % 2 != 0) + { + sum += arrInt; + } + } + + 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; + long sum = 0; + + if (arr == null) + { + throw new ArgumentNullException(); + } + + for (int i = 0; i < arr.Count(); i++) + { + if (i % 2 == 1) + { + 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; + try + { + return arr.Distinct(); + } + catch (Exception e) + { + throw e; + } } 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; + try + { + return arrA.Intersect(arrB); + } + catch (Exception e) + { + throw e; + } } 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; + IEnumerable returnArr; + + try + { + returnArr = arrA.Except(arrB); + + return returnArr.Union(arrB.Except(arrA)); + } + catch (Exception e) + { + throw e; + } } 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 true if any 2 values in the array have a sum equal to the target value + if (arr == null) + { + throw new NullReferenceException(); + } + + for (int i = 0; i < arr.Count(); i++) + { + for (int j = i + 1; j < arr.Count(); j++) + { + if (arr.ElementAt(i) + arr.ElementAt(j) == target) + return true; + } + } + return false; } @@ -66,27 +140,86 @@ 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; + List distinctArr = new List(); + List removedArr = new List(); + + if (arr == null) + { + throw new ArgumentNullException(); + } + + foreach (int intArr in arr) + { + if (!removedArr.Contains(intArr)) + { + if (distinctArr.Contains(intArr)) + { + distinctArr.Remove(intArr); + removedArr.Add(intArr); + } + else + { + distinctArr.Add(intArr); + } + } + } + + return SumArray(distinctArr); } 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(); + } + + string substr; + string doubledString = s; + + for (int i = 0; i < s.Length; i++) + { + substr = s.Substring(i, 1); + doubledString = doubledString.Insert(i * 2, substr); + } + + 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; + if (s == null) + { + throw new ArgumentNullException(); + } + + IEnumerable charArr = s.ToCharArray(); + + return charArr.Count(character => character == 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; + if (s == null) + { + throw new ArgumentNullException(); + } + + IEnumerable charArr = s.ToCharArray(); + IEnumerable digitOnlyArr = charArr.Where(character => char.IsDigit(character)); + List digitList = new List(); + + foreach (char digit in digitOnlyArr) + { + digitList.Add((int)Char.GetNumericValue(digit)); + } + + return SumArray(digitList); } public static long SumNumbers(String s) @@ -94,7 +227,45 @@ 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(); + } + + int startChar = 0; + int numDigits = 1; + string number; + List numbers = new List(); + + while (startChar + numDigits <= s.Length) + { + int result; + + number = s.Substring(startChar, numDigits); + + int.TryParse(number, out result); + + if (result > 0) + { + numDigits++; + } + else + { + number = s.Substring(startChar, numDigits - 1); + + int.TryParse(number, out result); + + if (result > 0) + { + numbers.Add(result); + } + + startChar = startChar + numDigits; + numDigits = 1; + } + } + + return SumArray(numbers); } public static Boolean IsAnagram(String s1, String s2)