Skip to content

Instantly share code, notes, and snippets.

@varnie
Created August 31, 2011 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varnie/1183832 to your computer and use it in GitHub Desktop.
Save varnie/1183832 to your computer and use it in GitHub Desktop.
Yet another programming challenge to blow up your brains
# coding: utf-8
src = '{Пожалуйста|Просто} сделайте так, чтобы это {удивительное|крутое|простое} тестовое предложение {изменялось {быстро|мгновенно} случайным образом|менялось каждый раз}';
r = Regexp.new(/\{[^\{\}]*\}/)
while (match = r.match(src)) do
match.length.times { |i|
offsets = match.offset(i)
chunk = src[offsets[0],offsets[1]-offsets[0]]
bracelessChunk = chunk[1..-2] #remove leading '{' and trailing '}'
alternatives = bracelessChunk.split("|")
selectedAlternative = alternatives[rand(alternatives.size())]
src[chunk]=selectedAlternative
}
end
puts src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment