# Код на 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+1,line] } return line end # # Примеры применения p zadanie("DMMDMDMDMMMDMMMM", 9)
# Код на 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("MMDMMMDDDMDDDMD", 10)
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+1,line]
}
return line
end
# # Примеры применения
p zadanie("DMMDMDMDMMMDMMMM", 9)
Вывод
[1, "DMDMDMDMMMDMMDMDDDMM"]
[2, "DDMDMDMMMDMMDMDMDDMM"]
[3, "DDDMDMMMDMMDMDMDMDMM"]
[4, "MMMDMMDMDMDMDMMM"]
[5, "MMDMMDMDMDMD"]
[6, "MDMMDMDMDMD"]
[7, "MMDMDMDMD"]
[8, "MDMDMDMD"]
[9, "MDMDMD"]
"MDMDMD"
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("MMDMMMDDDMDDDMD", 10)
Вывод
[1, "MDMDMMDMDDDMDDDM"]
[2, "DMDMDMMDMDDDMDDM"]
[3, "DDMDMDMMDMDDDMDM"]
[4, "DDDMDMDMMDMDDDMM"]
[5, "MDMDMMDMDDMM"]
[6, "MDMDMMDMDMM"]
[7, "MDMDMMDMMM"]
[8, "MDMDM"]
[9, "MDMDMDDD"]
[10, "MDMDMDD"]
"MDMDMDD"