diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..1e33ea2 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -15,94 +15,249 @@ 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(); + } + + List bigints = new List(); + foreach (int val in arr) + { + bigints.Add(val); + } + return bigints.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(); + } + + var scooby = 0; + foreach(int val in arr) + { + if(val % 2 != 0) + { + scooby = scooby + val; + } + } + return scooby; } 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(); + } + + var scooby = 0; + var skipOdd = 0; + foreach (int val in arr) + { + skipOdd++; + if (skipOdd % 2 == 0) + { + scooby = scooby + val; + } + } + return scooby; } public static IEnumerable GetUniqueValues(IEnumerable arr) { - // return an array that contains only unique values from the passed in array - // TODO - return null; + if (arr == null) + { + throw new ArgumentNullException(); + } + + IEnumerable SoUniqueMuchValues = new List(); + SoUniqueMuchValues = arr.ToList().Distinct(); + return SoUniqueMuchValues; } 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(); + } + + IEnumerable crissCross = arrA.Intersect(arrB); + return crissCross; } 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(); + } + + IEnumerable divergent = arrA.Except(arrB).Union(arrB.Except(arrA)); + return divergent; } 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 (target == null) + { + throw new ArgumentNullException(); + } + + var index = 0; + foreach(int val in arr) + { + index++; + var remainingList = arr.Skip(index).ToList(); + var searchValue = Convert.ToInt32(target - val); + if (remainingList.Contains(searchValue)) + { + 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; + if (arr == null) + { + throw new ArgumentNullException(); + } + var newDupes = new List(); + foreach (int val in arr) + { + var testList = arr.ToList().FindAll(x => x.Equals(val)); + if(testList.Count == 1) + { + newDupes.Add(val); + } + + } + + return SumArray(newDupes); } 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(); + } + + var retValu = ""; + + if (s.Length > 0) + { + foreach (char c in s.ToArray()) + { + retValu = retValu + c.ToString() + c.ToString(); + } + } + + return retValu; } 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 || c == null) + { + throw new ArgumentNullException(); + } + var testList = s.ToList().FindAll(x => x.Equals(c)); + return testList.Count; } 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(); + } + var numNumNum = new List(); + + foreach (char c in s.ToArray()) + { + int number = 0; + if(int.TryParse(c.ToString(),out number)) + { + numNumNum.Add(number); + } + } + + return SumArray(numNumNum); } public static long SumNumbers(String s) { + if (s == null) + { + throw new ArgumentNullException(); + } + var longCount = new List(); + string toAdd = ""; // 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; + foreach (char c in s.ToArray()) + { + int number = 0; + + if (int.TryParse(c.ToString(), out number)) + { + toAdd = toAdd + c.ToString(); + } + else + { + if (toAdd.Length > 0) + { + longCount.Add(int.Parse(toAdd)); + toAdd = ""; + } + } + } + if (toAdd.Length > 0) + { + longCount.Add(int.Parse(toAdd)); + } + + + return SumArray(longCount); } 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 + if (s1 == null || s2 == null) + { + throw new NullReferenceException(); + } + + if(s1.Length != s2.Length) + { + return false; + } + + var arrayOfString = s2.ToList(); + + foreach(char c in s1.ToArray()) + { + var index = arrayOfString.IndexOf(c); + if(index >= 0) + { + arrayOfString.RemoveAt(index); + } + } + + if (arrayOfString.Count == 0) + { + return true; + } + return false; } @@ -111,45 +266,137 @@ 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 == null || count2 == null) + { + throw new ArgumentNullException(); + } + var retval = 0; + + retval = count1 > 21? + (count2 > 21? + 0 : count2) + :(count2 > 21? + count1: (count1 >= count2 ? + count1:count2)); + + + return retval; } 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 players = new List(); + if (count1 <= 21) players.Add(count1); + if (count2 <= 21) players.Add(count2); + if (count3 <= 21) players.Add(count3); + if (count4 <= 21) players.Add(count4); + if (count5 <= 21) players.Add(count5); + + if (players.Count == 0) return 0; + + var ordered = players.OrderByDescending(x => x); + + return ordered.First(); } 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(); + } + + var players = new List(); + foreach(int val in counts) + { + if (val <= 21) players.Add(val); + } + if (players.Count == 0) return 0; + + var ordered = players.OrderByDescending(x => x); + + return ordered.First(); } public static Dictionary WordCount(IEnumerable arr) { + if (arr == null) + { + throw new NullReferenceException(); + } // 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 keyMan = new Dictionary(); + List workArr = arr.ToList(); + + foreach (string wordUp in arr) + { + var testList = workArr.ToList().FindAll(x => x.Equals(wordUp)); + if (testList.Count > 0) + { + keyMan.Add(wordUp, testList.Count); + workArr.RemoveAll(x => x == wordUp); + } + } + return keyMan; } public static int Factorial(int n) { + if (n == null) + { + throw new ArgumentNullException(); + } + var retVal = n; + + if (n <= 1) retVal = 1; + else + { + for (int i = 1; i < n; i++) + { + retVal = retVal * i; + } + } + // Given n, return the factorial of n, which is n * (n-1) * (n-2) ... 1 - return 0; + return retVal; } public static List FB(int n) { + if (n == null) + { + throw new ArgumentNullException(); + } // 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 threeAndFives = new List(); + + for (int i = 1; i <= n; i++) + { + var num = i.ToString(); + if(i%3 == 0) + { + num = "Fizz"; + } + if (i % 5 == 0) + { + num = "Buzz"; + } + if ((i % 3 == 0) && (i%5==0)) + { + num = "FizzBuzz"; + } + threeAndFives.Add(num); + } + + + + return threeAndFives; } } }