about | help | code help+videos | done | prefs |
Write a method that finds the sum of all the single-digit numbers which are "immediately" after the word "add". By "immediately" I mean that the only thing between the word "add" and the single-digit number could be spaces. For example: "Please add 7 and add 2." would result in 9. This would also be true for "add7add2" or "add 7 then add 2". If a number does not have "add" immediately in front of it, you do NOT add that number into the total. You only have to look for all lowercase "add". Any time "add" appears it WILL have a number immediately after it, with the possible exception of "add" appearing at the end of the string, possibly with spaces after it. So "add 5 add 2 add " would result in 7 and must not generate an error. Note that there could be multiple-digit numbers, but you only add the first digit after the word "add". So "add 27 add 31" would result in 5. Look at the test data for further examples. test2021_04_22_SLHL_sum("This is fun but add2 then add5") → 7 test2021_04_22_SLHL_sum("add2add5add8add3add1") → 19 test2021_04_22_SLHL_sum("x") → 0 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 250
Copyright Nick Parlante 2017 - privacy