From 6f5a2d58ffbd8cb842884fcbcfd73a0b53569a70 Mon Sep 17 00:00:00 2001 From: etanvir Date: Thu, 30 Jun 2016 13:40:20 -0500 Subject: [PATCH 1/2] Method 1 --- problem-solving/Program.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index 15e54d4..a22c778 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -17,13 +17,30 @@ 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; } public static long SumArrayOddValues(IEnumerable arr) { // return the sum of all the values in the array that are odd // TODO + int value = 0; + long sum = 0; + for (int i = 0; i < arr.Count(); i++) + { + value = arr.ElementAt(i); + sum = sum + value; + + } return 0; } From 01e9ec7ce16841c34f8c6160ec7a3203484aa1c5 Mon Sep 17 00:00:00 2001 From: etanvir Date: Thu, 30 Jun 2016 15:41:06 -0500 Subject: [PATCH 2/2] Finish --- problem-solving/Program.cs | 54 +++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/problem-solving/Program.cs b/problem-solving/Program.cs index a22c778..9cef298 100644 --- a/problem-solving/Program.cs +++ b/problem-solving/Program.cs @@ -13,22 +13,38 @@ static void Main(string[] args) { } - public static long SumArray(IEnumerable arr) + public static long Sum(IEnumerable numbers) { - // return the sum of all the values in the array - // TODO int value = 0; long sum = 0; - for (int i = 0; i < arr.Count(); i++) + for (int i = 0; i < numbers.Count(); i++) { - value = arr.ElementAt(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 + //int value = 0; + //long sum = 0; + //for (int i = 0; i < arr.Count(); i++) + //{ + // value = arr.ElementAt(i); + // sum = sum + value; + + //} - return sum; + 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 @@ -38,31 +54,49 @@ public static long SumArrayOddValues(IEnumerable arr) for (int i = 0; i < arr.Count(); i++) { value = arr.ElementAt(i); + if(IsOdd(value)) sum = sum + value; } - return 0; + 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)