-
-
Save askl56/e5bfa38db1a1da93ac84 to your computer and use it in GitHub Desktop.
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
# Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string. | |
# Examples: | |
# foo -> foo1 | |
# foobar23 -> foobar24 | |
# foo0042 -> foo0043 | |
# foo9 -> foo10 | |
# foo099 -> foo100 | |
# Attention: If the number has leading zeros the amount of digits should be considered. | |
def increment_string(input) | |
if input.count("0-9") > 0 | |
input.sub(/\d+/, &:next) | |
else | |
input << "1" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test Passed: Value == "foo1"
Test Passed: Value == "foobar002"
Test Passed: Value == "foobar2"
Test Passed: Value == "foobar01"
Test Passed: Value == "foobar100"
Test Passed: Value == "1"
Expected: "f00bar1", instead got: "f01bar"
Expected: "f00b4r1", instead got: "f01b4r"
Test Passed: Value == "foobar001"
Test Passed: Value == "foobar1000"
Test Passed: Value == "foobar01000"
Test Passed: Value == "foobar002"
Test Passed: Value == "foobar2"
Test Passed: Value == "2"
Test Passed: Value == "010"