Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created March 27, 2019 21:08
Show Gist options
  • Save primaryobjects/680012aed6e843bcbfaa0d743ec1f5fc to your computer and use it in GitHub Desktop.
Save primaryobjects/680012aed6e843bcbfaa0d743ec1f5fc to your computer and use it in GitHub Desktop.
[2019-02-11] Challenge #375 [Easy] Print a new number by adding one to each digit. Demo at https://repl.it/repls/SickInnocentCoding
#
# [2019-02-11] Challenge #375 [Easy] Print a new number by adding one to each of its digit
# For example, 998 becomes 10109.
#
# Bonus
# Solve without using strings.
#
# https://www.reddit.com/r/dailyprogrammer/comments/aphavc/20190211_challenge_375_easy_print_a_new_number_by/
#
oneToEachDigit <- function(n) {
result <- 0
factor <- 1
current <- abs(n)
repeat {
# Get the current digit and add 1.
val <- (current %% 10) + 1
# Add the new value to the current result in the proper placement.
result <- result + (val * factor)
# Determine the next factor to apply.
if (val != 10) {
nextFactor <- 10
}
else {
nextFactor <- 100
}
factor <- factor * (if (val != 10) 10 else 100)
# Get the next digit.
current <- floor(current / 10)
if (current < 1)
break
}
result * (if (n < 0) -1 else 1)
}
oneToEachDigitStr <- function(n) {
result <- c()
current <- abs(n)
repeat {
# Get the current digit.
digit <- current %% 10
# Insert the new value into the list.
result <- c(digit + 1, result)
# Get the next digit.
current <- floor(current / 10)
if (current < 1)
break
}
# Convert the list into a string and then a number.
as.numeric(paste0(result, collapse='')) * (if (n < 0) -1 else 1)
}
## Test client.
testData <- 1:10000
# Iterate across each value and verify the two functions produce the same result.
results <- sapply(testData, function(n) {
result <- oneToEachDigit(n)
isSuccess = oneToEachDigitStr(n) == result
print(paste('Testing', n, 'Result', result, 'Alternate', isSuccess))
c(isSuccess)
})
print(paste('All success?', length(which(results)) == length(testData)))
[1] "Testing 1 Result 2 Alternate TRUE"
[1] "Testing 2 Result 3 Alternate TRUE"
[1] "Testing 3 Result 4 Alternate TRUE"
[1] "Testing 4 Result 5 Alternate TRUE"
[1] "Testing 5 Result 6 Alternate TRUE"
[1] "Testing 6 Result 7 Alternate TRUE"
[1] "Testing 7 Result 8 Alternate TRUE"
[1] "Testing 8 Result 9 Alternate TRUE"
[1] "Testing 9 Result 10 Alternate TRUE"
[1] "Testing 10 Result 21 Alternate TRUE"
[1] "Testing 11 Result 22 Alternate TRUE"
[1] "Testing 12 Result 23 Alternate TRUE"
[1] "Testing 13 Result 24 Alternate TRUE"
[1] "Testing 14 Result 25 Alternate TRUE"
[1] "Testing 15 Result 26 Alternate TRUE"
[1] "Testing 16 Result 27 Alternate TRUE"
[1] "Testing 17 Result 28 Alternate TRUE"
[1] "Testing 18 Result 29 Alternate TRUE"
[1] "Testing 19 Result 210 Alternate TRUE"
[1] "Testing 20 Result 31 Alternate TRUE"
[1] "Testing 21 Result 32 Alternate TRUE"
[1] "Testing 22 Result 33 Alternate TRUE"
[1] "Testing 23 Result 34 Alternate TRUE"
[1] "Testing 24 Result 35 Alternate TRUE"
[1] "Testing 25 Result 36 Alternate TRUE"
[1] "Testing 26 Result 37 Alternate TRUE"
[1] "Testing 27 Result 38 Alternate TRUE"
[1] "Testing 28 Result 39 Alternate TRUE"
[1] "Testing 29 Result 310 Alternate TRUE"
[1] "Testing 30 Result 41 Alternate TRUE"
[1] "Testing 31 Result 42 Alternate TRUE"
[1] "Testing 32 Result 43 Alternate TRUE"
[1] "Testing 33 Result 44 Alternate TRUE"
[1] "Testing 34 Result 45 Alternate TRUE"
[1] "Testing 35 Result 46 Alternate TRUE"
[1] "Testing 36 Result 47 Alternate TRUE"
[1] "Testing 37 Result 48 Alternate TRUE"
[1] "Testing 38 Result 49 Alternate TRUE"
[1] "Testing 39 Result 410 Alternate TRUE"
[1] "Testing 40 Result 51 Alternate TRUE"
[1] "Testing 41 Result 52 Alternate TRUE"
[1] "Testing 42 Result 53 Alternate TRUE"
[1] "Testing 43 Result 54 Alternate TRUE"
[1] "Testing 44 Result 55 Alternate TRUE"
[1] "Testing 45 Result 56 Alternate TRUE"
[1] "Testing 46 Result 57 Alternate TRUE"
[1] "Testing 47 Result 58 Alternate TRUE"
[1] "Testing 48 Result 59 Alternate TRUE"
[1] "Testing 49 Result 510 Alternate TRUE"
[1] "Testing 50 Result 61 Alternate TRUE"
[1] "Testing 51 Result 62 Alternate TRUE"
[1] "Testing 52 Result 63 Alternate TRUE"
[1] "Testing 53 Result 64 Alternate TRUE"
[1] "Testing 54 Result 65 Alternate TRUE"
[1] "Testing 55 Result 66 Alternate TRUE"
[1] "Testing 56 Result 67 Alternate TRUE"
[1] "Testing 57 Result 68 Alternate TRUE"
[1] "Testing 58 Result 69 Alternate TRUE"
[1] "Testing 59 Result 610 Alternate TRUE"
[1] "Testing 60 Result 71 Alternate TRUE"
[1] "Testing 61 Result 72 Alternate TRUE"
[1] "Testing 62 Result 73 Alternate TRUE"
[1] "Testing 63 Result 74 Alternate TRUE"
[1] "Testing 64 Result 75 Alternate TRUE"
[1] "Testing 65 Result 76 Alternate TRUE"
[1] "Testing 66 Result 77 Alternate TRUE"
[1] "Testing 67 Result 78 Alternate TRUE"
[1] "Testing 68 Result 79 Alternate TRUE"
[1] "Testing 69 Result 710 Alternate TRUE"
[1] "Testing 70 Result 81 Alternate TRUE"
[1] "Testing 71 Result 82 Alternate TRUE"
[1] "Testing 72 Result 83 Alternate TRUE"
[1] "Testing 73 Result 84 Alternate TRUE"
[1] "Testing 74 Result 85 Alternate TRUE"
[1] "Testing 75 Result 86 Alternate TRUE"
[1] "Testing 76 Result 87 Alternate TRUE"
[1] "Testing 77 Result 88 Alternate TRUE"
[1] "Testing 78 Result 89 Alternate TRUE"
[1] "Testing 79 Result 810 Alternate TRUE"
[1] "Testing 80 Result 91 Alternate TRUE"
[1] "Testing 81 Result 92 Alternate TRUE"
[1] "Testing 82 Result 93 Alternate TRUE"
[1] "Testing 83 Result 94 Alternate TRUE"
[1] "Testing 84 Result 95 Alternate TRUE"
[1] "Testing 85 Result 96 Alternate TRUE"
[1] "Testing 86 Result 97 Alternate TRUE"
[1] "Testing 87 Result 98 Alternate TRUE"
[1] "Testing 88 Result 99 Alternate TRUE"
[1] "Testing 89 Result 910 Alternate TRUE"
[1] "Testing 90 Result 101 Alternate TRUE"
[1] "Testing 91 Result 102 Alternate TRUE"
[1] "Testing 92 Result 103 Alternate TRUE"
[1] "Testing 93 Result 104 Alternate TRUE"
[1] "Testing 94 Result 105 Alternate TRUE"
[1] "Testing 95 Result 106 Alternate TRUE"
[1] "Testing 96 Result 107 Alternate TRUE"
[1] "Testing 97 Result 108 Alternate TRUE"
[1] "Testing 98 Result 109 Alternate TRUE"
[1] "Testing 99 Result 1010 Alternate TRUE"
[1] "Testing 100 Result 211 Alternate TRUE"
[1] "All success? TRUE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment