Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Created December 28, 2015 16:47
Show Gist options
  • Save karlosmid/7460c9c1def2ac2d1207 to your computer and use it in GitHub Desktop.
Save karlosmid/7460c9c1def2ac2d1207 to your computer and use it in GitHub Desktop.
ruby counterstrings
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