-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find_the_middle_element.cs
54 lines (39 loc) · 1.5 KB
/
Find_the_middle_element.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodingChallenges;
[TestClass]
public class Find_the_middle_element {
[TestMethod]
public void Test() {
/*
Find the middle element
As a part of this challenge, you need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements.
The input to the function will be an array of three distinct numbers.
For example:
gimme([2, 3, 1]) => 0
2 is the number that fits between 1 and 3 and the index of 2 in the input array is 0.
Another example (just to make sure it is clear):
gimme([5, 10, 14]) => 1
10 is the number that fits between 5 and 14 and the index of 10 in the input array is 1.
*/
Assert.AreEqual(0, Gimme(new double[] { 2, 3, 1 }));
Assert.AreEqual(1, Gimme(new double[] { 5, 10, 14 }));
Assert.AreEqual(0, Gimme_v2(new double[] { 2, 3, 1 }));
Assert.AreEqual(1, Gimme_v2(new double[] { 5, 10, 14 }));
}
public static int Gimme(double[] inputArray) => inputArray
.Select((a, i) => (a, i))
.OrderBy(x => x.Item1)
.Select(x => x.Item2)
.Skip(1)
.FirstOrDefault();
public static int Gimme_v2(double[] inputArray) => inputArray
.Select((a, i) => (a, i))
.OrderBy(x => x.a)
.Skip(1)
.FirstOrDefault()
.i;
}