Created
August 31, 2011 15:31
-
-
Save varnie/1183832 to your computer and use it in GitHub Desktop.
Yet another programming challenge to blow up your brains
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
# 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