diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..5140f0d 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -9,56 +9,99 @@ namespace problem_solving { public class Program { + 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; + foreach (int val in arr) + { + sum += val; + } + + 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; + foreach (int val in arr) + { + if ((val % 2) != 0) + { + sum += val; + } + } + + 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; + int index = 1; + + foreach (int val in arr) + { + if ((index % 2) == 0) + { + sum += val; + } + index = index + 1; + } + + 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 GetArrayNotIntersect(arrA, arrB); } 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 + + foreach (int val1 in arr) + { + foreach (int val2 in arr) + { + if (val1 + val2 == target) + return true; + + } + } + return false; } @@ -66,27 +109,57 @@ 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; + IEnumerable arr2 = arr.Distinct(); + long sum = 0; + foreach (int val in arr2) + { + sum += val; + } + 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 result = null; + foreach (char ch in s) + { + result = ch + "" + ch; + } + return result; } public static int CountChars(String s, char c) { // return the count of how many times char c occurs in string s - return 0; + int count = 0; + foreach (char ch in s) + { + if (ch == c) + { + count++; + } + + } + return 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; + int count = 0; + long sum = 0; + foreach (char ch in s) + { + if (char.IsDigit(ch)) + { + sum += (long)char.GetNumericValue(ch); + } + + } + return sum; } public static long SumNumbers(String s) @@ -94,6 +167,7 @@ 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; } @@ -103,7 +177,10 @@ public static Boolean IsAnagram(String s1, String s2) // 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.Reverse() == s2) + return true; + else + return false; } public static int BlackJack(int count1, int count2) @@ -140,7 +217,9 @@ public static Dictionary WordCount(IEnumerable arr) 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) @@ -149,7 +228,13 @@ public static List FB(int n) // 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; + + for(int i; i