-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Synced tests * Synced tests implementation with Exercism standard * Changed example.R implementation to pass new tests * example.R reformated for linter
- Loading branch information
1 parent
e335623
commit 7924628
Showing
3 changed files
with
71 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,10 @@ | ||
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) | ||
str | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters