-
Notifications
You must be signed in to change notification settings - Fork 0
/
Most_digits.cs
36 lines (26 loc) · 1.06 KB
/
Most_digits.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
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class Most_digits {
[TestMethod]
public void Test() {
/*
Most digits
Find the number with the most digits.
If two numbers in the argument array have the same number of digits, return the first one in the array.
*/
Assert.AreEqual(100, FindLongest(new int[] { 1, 10, 100 }));
Assert.AreEqual(8000, FindLongest(new int[] { 8000, 8, 9000 }));
Assert.AreEqual(900, FindLongest(new int[] { 8, 900, 500 }));
Assert.AreEqual(100, FindLongest_v2(new int[] { 1, 10, 100 }));
Assert.AreEqual(8000, FindLongest_v2(new int[] { 8000, 8, 9000 }));
Assert.AreEqual(900, FindLongest_v2(new int[] { 8, 900, 500 }));
}
public static int FindLongest(int[] number) => number
.MaxBy(x => $"{x}"
.Where(c => char.IsDigit(c))
.Count()
);
public static int FindLongest_v2(int[] number) => number
.Aggregate(0, (a, b) => $"{a}".Length < $"{b}".Length ? b : a);
}