diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..566f33c 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -13,143 +13,283 @@ static void Main(string[] args) { } + private static void checkNull(Object arr) + { + if (arr == null) + { + throw new ArgumentNullException(); + } + } + public static long SumArray(IEnumerable arr) { - // return the sum of all the values in the array - // TODO - return 0; + checkNull(arr); + long result = 0; + + foreach (int i in arr) + { + result += i; + } + return result; } public static long SumArrayOddValues(IEnumerable arr) { - // return the sum of all the values in the array that are odd - // TODO - return 0; + checkNull(arr); + int sum = 0; + foreach (int i in arr) + { + if (i % 2 == 1 || i%2 == -1) + { + sum += i; + } + } + 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; + checkNull(arr); + int sum = 0; + List list = arr.ToList(); + for (int i = 0; i < list.Count; i++ ) + { + if (i % 2 == 1) + { + sum += list[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; + 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.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; + return arrA.Except(arrB).Union(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 + if (arr == null) + { + throw new NullReferenceException(); + } + if (arr.Count() <= 1) + { + return false; + } + List list = arr.ToList(); + list.Sort(); + int left = 0; + int right = list.Count()-1; + + while (left < right) + { + int sum = list[left] + list[right]; + + if(sum == target) + { + return true; + } + if (sum > target) + { + right--; + } + if (sum < target) + { + left++; + } + } + 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; + return arr.GroupBy(x => x) + .Where(g => g.Count() <= 1) + .Select(g => g.Key).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(); + } + String newS = ""; + foreach (char c in s) + { + newS += c + "" + c; + } + + return newS; } public static int CountChars(String s, char c) { - // return the count of how many times char c occurs in string s - return 0; + checkNull(s); + int occur = 0; + foreach (char ch in s) + { + if (ch == c) + { + occur++; + } + } + return occur; } 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; + int sum = 0; + if (s == null) + { + throw new ArgumentNullException(); + } + foreach (char c in s) + { + if (Char.IsDigit(c)) { + sum += Int32.Parse(c.ToString()); + } + } + 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(); + } + string[] split = Regex.Split(s, @"\D+"); + long value = 0; + foreach(string i in split) { + if (i.Length > 0) { + value += long.Parse(i); + } + } + return value; } 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(); + } + + if (s1.Length != s2.Length) + { + return false; + } + char[] c1 = s1.ToCharArray(); + char[] c2 = s2.ToCharArray(); + Array.Sort(c1); + Array.Sort(c2); + + for (int i = 0; i < c1.Length; i++) + { + if (c1[i] != c2[i]) + { + return false; + } + } + return true; + } + + private static int blackHelper(List arr) { + List newArr = arr.Where(i => i <= 21).ToList(); + if (newArr.Count > 0) + { + return newArr.OrderBy(j => 21 - j).First(); + } + return 0; } 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; + List arr = new List(); + arr.Add(count1); + arr.Add(count2); + return blackHelper(arr); + } 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; + List arr = new List(); + arr.Add(count1); + arr.Add(count2); + arr.Add(count3); + arr.Add(count4); + arr.Add(count5); + return blackHelper(arr); } 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; + return blackHelper(counts.ToList()); } 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 dict = new Dictionary(); + foreach (String s in arr) + { + if (dict.ContainsKey(s)) + { + dict[s] = dict[s]+1; + + } + else + { + dict.Add(s, 1); + } + } + return dict; } 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 <= 1) + { + return 1; + } + else + { + 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; + List result = new List(); + for (int i = 1; i <= n; i++) + { + string add = ""; + if (i % 3 == 0) + { + add += "Fizz"; + } + if( i % 5 == 0) { + add += "Buzz"; + } + if(add.Length == 0) { + add = i + ""; + } + result.Add(add); + } + return result; } } }