-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSum_of_integers_in_string.cs
More file actions
40 lines (27 loc) · 1.32 KB
/
Sum_of_integers_in_string.cs
File metadata and controls
40 lines (27 loc) · 1.32 KB
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
using System.Linq;
namespace CodingChallenges;
[TestClass]
public class Sum_of_integers_in_string {
[TestMethod]
public void Test() {
/*
Sum of integers in string
Your task is to implement a function that calculates the sum of the integers inside a string.
For example, in the string
"The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog",
the sum of the integers is 3635.
Note: only positive integers will be tested.
*/
Assert.AreEqual(5, SumOfIntegersInString("2 + 3 = "));
Assert.AreEqual(1, SumOfIntegersInString("Our company made approximately 1 million in gross revenue last quarter."));
Assert.AreEqual(3868, SumOfIntegersInString("The Great Depression lasted from 1929 to 1939."));
Assert.AreEqual(0, SumOfIntegersInString("Dogs are our best friends."));
Assert.AreEqual(18, SumOfIntegersInString("C4t5 are 4m4z1ng."));
Assert.AreEqual(3635, SumOfIntegersInString("The30quick20brown10f0x1203jumps914ov3r1349the102l4zy dog"));
}
public static int SumOfIntegersInString(string s) =>
string.Concat(s.Select(c => char.IsDigit(c) ? c : ' '))
.Split(' ')
.Where(s => !string.IsNullOrWhiteSpace(s))
.Sum(n => int.TryParse(n, out int val) ? val : 0);
}