Answer the question
In order to leave comments, you need to log in
Reverse engineering: Computing the initial algorithm of a Ruby program. What does the code written starting on the third line mean?
There is a task:
Дана последовательность:
1
11
21
1211
111221
312211
Нужно чтобы ваша программа могла продолжить данную последовательность.
Можете в реализации использовать любые библиотеки.
There is a solution:value = '1'
puts value
10.times do
next_value = value.chars.inject([]) do |arr, next_char|
if !arr.empty? && arr.last.has_key?(next_char)
arr.last[next_char] += 1
else
arr << {next_char => 1}
end
arr
end
value = ''
next_value.map do |hash|
hash.each_pair{ |k,v| value << "#{v}#{k}" }
end
puts value
end
10.times do
arr << {next_char => 1}
next_value = value.chars.inject([]) do |arr, next_char|
if !arr.empty? && arr.last.has_key?(next_char)
arr.last[next_char] += 1
else
arr << {next_char => 1}
end
arr
From all this, I saw only the do-end and if-else construction , nothing else. I will say right away that I am not a ruby developer, but I want to become one. value = '1'
puts value
Answer the question
In order to leave comments, you need to log in
If you want to become a Ruby developer, you should read about the language itself and the main constructs.
One gets the feeling that you have 0 with English and with programming as well. Take time for theory to get started.
After these lines, I realized that you are trolling:
On the first line, the value variable is created. On the second, it is interpolated.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question