Skip to content

Commit

Permalink
Changed example.R implementation to pass new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerwosolek committed Apr 18, 2024
1 parent 819b737 commit 2859733
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions exercises/practice/phone-number/.meta/example.R
Original file line number Diff line number Diff line change
@@ -1,35 +1,7 @@
parse_phone_number <- function(number_string) {

# If the number is less than 10 digits assume that it is bad number
# If the number is 10 digits assume that it is good, unless area or exchange
# code are not between 2 to 9
# If the number is 11 digits and the first number is 1, use the last 10 digits
# If the number is 11 digits and the first number is not 1, it is a bad number
# If the number is more than 11 digits assume that it is a bad number

# Check for letters
if (grepl("[A-z]", number_string)) {
return (NULL)
}

# Remove non-digit characters from number string
cleaned <- gsub("[^0-9]", "", number_string)

if (nchar(cleaned) < 10) {
NULL
}
else if (nchar(cleaned) == 10 & (substr(cleaned, 1, 1) %in% c("0", "1") |
substr(cleaned, 4, 4) %in% c("0", "1"))) {
NULL
}
else if (nchar(cleaned) == 10) {
cleaned
}
else if (nchar(cleaned) == 11 & substr(cleaned, 1, 1) == "1") {
substr(cleaned, 2, 11)
}
else {
NULL
}

str <- gsub("\\D", replacement = "", number_string)
if (substr(str, 1, 1) == '1') str <- substr(str, 2, nchar(str))
if (nchar(str) != 10) return(NULL)
if (strtoi(substr(str, 1, 1)) < 2 || strtoi(substr(str, 4, 4)) < 2) return(NULL)

Check warning on line 5 in exercises/practice/phone-number/.meta/example.R

View workflow job for this annotation

GitHub Actions / precheck

file=/home/runner/work/r/r/exercises/practice/phone-number/.meta/example.R,line=5,col=81,[a] Lines should not be more than 80 characters. This line is 82 characters.

Check warning on line 5 in exercises/practice/phone-number/.meta/example.R

View workflow job for this annotation

GitHub Actions / precheck

file=/home/runner/work/r/r/exercises/practice/phone-number/.meta/example.R,line=5,col=81,[a] Lines should not be more than 80 characters. This line is 82 characters.
str
}

0 comments on commit 2859733

Please sign in to comment.