Created
December 28, 2015 16:47
-
-
Save karlosmid/7460c9c1def2ac2d1207 to your computer and use it in GitHub Desktop.
ruby counterstrings
This file contains hidden or 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
def counter_strings length | |
if length.nil? | |
return "" | |
elsif length == 1 | |
return '*' | |
else | |
result = [] | |
position = 2 | |
increment = 2 | |
while position <= length | |
result << "#{position}*" | |
if (position + increment)%(10**(increment-1)) < position%(10**(increment-1)) | |
increment = increment + 1 | |
end | |
position = position + increment | |
end | |
result_as_string = result.join('') | |
if result_as_string.length < length | |
return "#{result_as_string}#{position}" | |
else | |
return result_as_string | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment