# Код на ruby 2.2.3p173
def zadanie(line, time)
line = line.gsub(/[^MD]/, "")
time.times { |i|
k = 0
while k < line.size - 1 do
if line[k..k + 1] == "MD"
line[k], line[k + 1] = line[k + 1], line[k]
k+=1
end
k+=1
end
p [i,line]
}
return line
end
# # Примеры применения
p zadanie("MMDMDDDDDMMDDDMDDDMD", 10)
Вывод
[1, "MDMDMDDDDMDMDDDMDDDM"]
[2, "DMDMDMDDDDMDMDDDMDDM"]
[3, "DDMDMDMDDDDMDMDDDMDM"]
[4, "DDDMDMDMDDDDMDMDDDMM"]
[5, "DDDDMDMDMDDDDMDMDDMM"]
[6, "DDDDDMDMDMDDDDMDMDMM"]
[7, "DDDDDDMDMDMDDDDMDMMM"]
[8, "DDDDDDDMDMDMDDDDMMMM"]
[9, "DDDDDDDDMDMDMDDDMMMM"]
[10, "DDDDDDDDDMDMDMDDMMMM"]
"DDDDDDDDDMDMDMDDMMMM"