diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..9cef298 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -13,39 +13,90 @@ static void Main(string[] args) { } + public static long Sum(IEnumerable numbers) + { + int value = 0; + long sum = 0; + for (int i = 0; i < numbers.Count(); i++) + { + value = numbers.ElementAt(i); + sum = sum + value; + + } + return sum; + } public static long SumArray(IEnumerable arr) { // return the sum of all the values in the array // TODO - return 0; + //int value = 0; + //long sum = 0; + //for (int i = 0; i < arr.Count(); i++) + //{ + // value = arr.ElementAt(i); + // sum = sum + value; + + //} + + return Sum(arr); } + public static bool IsOdd(int value) + { + return value % 2 != 0; + } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd // TODO - return 0; + int value = 0; + long sum = 0; + for (int i = 0; i < arr.Count(); i++) + { + value = arr.ElementAt(i); + if(IsOdd(value)) + sum = sum + value; + + } + 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; + int value = 0; + long sum = 0; + int count = arr.Count(); + for (int i = 0; i < count; i++) + { + if(i<=count-2) + { + value = arr.ElementAt(++i); + sum = sum + value; + } + + } + 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)