Files
hello-algo/ru/codes/ruby/chapter_computational_complexity/iteration.rb
T
Yudong JinandClaude Opus 5.5 083dfdc7bc Fix code block headers that pulled in the previous function (#2031)
When the header comment above a cited function did not match the extractor's
format, the extractor kept scanning upward and the book showed the end of the
previous function too. Fix those comments in all editions:

- ruby min_cost_climbing_stairs_dp.rb: `# ...` -> `### ... ###` (all editions);
- ruby unbounded_knapsack.rb: `### ... ##3` -> `### ... ###` (zh, ja, ko, ru);
- c top_k.c: `// ...` above topKHeap -> `/* ... */` (all editions);
- ru Ruby: restore the `###` prefix written as `# ##` on 25 lines in 12 files,
  following the Chinese original line by line.

Also normalize whitespace-only lines inside the code tabs of space_complexity.md
and suggestions.md (30 lines per edition) to empty lines; the built HTML of those
pages is unchanged. zh-hant is regenerated by OpenCC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 22:48:52 +08:00

80 lines
1.7 KiB
Ruby

=begin
File: iteration.rb
Created Time: 2024-03-30
Author: Xuan Khoa Tu Nguyen ([email protected]), Cy ([email protected])
=end
### Цикл for ###
def for_loop(n)
res = 0
# Циклическое суммирование 1, 2, ..., n-1, n
for i in 1..n
res += i
end
res
end
### Цикл while ###
def while_loop(n)
res = 0
i = 1 # Инициализация условной переменной
# Циклическое суммирование 1, 2, ..., n-1, n
while i <= n
res += i
i += 1 # Обновить условную переменную
end
res
end
### Цикл while (двойное обновление) ###
def while_loop_ii(n)
res = 0
i = 1 # Инициализация условной переменной
# Циклическое суммирование 1, 4, 10, ...
while i <= n
res += i
# Обновить условную переменную
i += 1
i *= 2
end
res
end
### Двойной цикл for ###
def nested_for_loop(n)
res = ""
# Цикл по i = 1, 2, ..., n-1, n
for i in 1..n
# Цикл по j = 1, 2, ..., n-1, n
for j in 1..n
res += "(#{i}, #{j}), "
end
end
res
end
### Driver Code ###
if __FILE__ == $0
n = 5
res = for_loop(n)
puts "\nРезультат суммирования в цикле for res = #{res}"
res = while_loop(n)
puts "\nРезультат суммирования в цикле while res = #{res}"
res = while_loop_ii(n)
puts "\nРезультат суммирования в цикле while (двойное обновление) res = #{res}"
res = nested_for_loop(n)
puts "\nРезультат обхода в двойном цикле for #{res}"
end