-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsumUpNumbers.R
executable file
·30 lines (29 loc) · 998 Bytes
/
sumUpNumbers.R
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
# CodeMaster has just returned from shopping. He scanned the check of the items
# he bought and gave the resulting string to Ratiorg to figure out the total
# number of purchased items. Since Ratiorg is a bot he is definitely going to
# automate it, so he needs a program that sums up all the numbers which appear
# in the given input.
#
# Help Ratiorg by writing a function that returns the sum of numbers that appear
# in the given inputString.
#
# Example
#
# For inputString = "2 apples, 12 oranges", the output should be
# sumUpNumbers(inputString) = 14.
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] string inputString
#
# Guaranteed constraints: 0 ≤ inputString.length ≤ 105.
#
# [output] integer
inputString = "abcdefghijklmnopqrstuvwxyz1AbCdEfGhIjKlMnOpqrstuvwxyz23,74 -"
sumUpNumbers <- function(inputString) {
onlyNumbers <- strsplit(inputString,"[^0-9]")[[1]]
onlyNumbers <- lapply(onlyNumbers,as.numeric)
return(sum(unlist(onlyNumbers),na.rm = T))
}