-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfirstDigit.R
More file actions
executable file
·31 lines (29 loc) · 823 Bytes
/
firstDigit.R
File metadata and controls
executable file
·31 lines (29 loc) · 823 Bytes
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
# Find the leftmost digit that occurs in a given string.
#
# Example
#
# For inputString = "var_1__Int", the output should be
# firstDigit(inputString) = '1';
# For inputString = "q2q-q", the output should be
# firstDigit(inputString) = '2';
# For inputString = "0ss", the output should be
# firstDigit(inputString) = '0'.
#
# Input/Output
#
# [execution time limit] 5 seconds (r)
#
# [input] string inputString
#
# A string containing at least one digit.
#
# Guaranteed constraints:
# 3 ≤ inputString.length ≤ 10.
#
# [output] char
# inputString = "var_1__Int"
firstDigit <- function(inputString) {
#todo: search till first digit is found (not the whole string). return that digit.
posoffirstdigit = gregexpr("[0-9]",inputString)[[1]][1]
return(substr(inputString,posoffirstdigit,posoffirstdigit))
}