mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-30 10:51:33 +08:00
Cite demo-source regions from doc snippets (pilot) (#2034)
Doc snippets for init_linked_list, init_binary_tree, stack_operations and
five list.md entries now reference marked regions of the demo programs
instead of copying code, so the book shows code that is compiled and run.
- Mark regions in the zh demo files and add the snippet comments that the
demos lacked, reusing texts that already have reviewed translations.
- Sync ru by hand, regenerate en/ja/ko from the reviewed dictionaries and
derive zh-hant with OpenCC.
- Convert 97 snippet blocks in six editions to [file]{..}-[region]{..}
references; init_list and the Rust stack block stay literal until their
comments have reviewed translations.
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
8c96e2f30c
commit
849b6a95b9
@@ -50,7 +50,8 @@ def find(head: ListNode, target: int) -> int:
|
||||
|
||||
"""実行用コード"""
|
||||
if __name__ == "__main__":
|
||||
# 連結リストを初期化する
|
||||
# [region]{init_linked_list}
|
||||
# 連結リスト 1 -> 3 -> 2 -> 5 -> 4 を初期化する
|
||||
# 各ノードを初期化する
|
||||
n0 = ListNode(1)
|
||||
n1 = ListNode(3)
|
||||
@@ -62,6 +63,7 @@ if __name__ == "__main__":
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
# [endregion]{init_linked_list}
|
||||
print("初期化した連結リスト")
|
||||
print_linked_list(n0)
|
||||
|
||||
|
||||
@@ -10,47 +10,66 @@ if __name__ == "__main__":
|
||||
nums: list[int] = [1, 3, 2, 5, 4]
|
||||
print("\nリスト nums =", nums)
|
||||
|
||||
# [region]{access_element}
|
||||
# 要素にアクセスする
|
||||
x: int = nums[1]
|
||||
x: int = nums[1] # インデックス 1 の要素にアクセスする
|
||||
# [endregion]{access_element}
|
||||
print("\nインデックス 1 の要素にアクセスした結果 x =", x)
|
||||
|
||||
# [region]{update_element}
|
||||
# 要素を更新する
|
||||
nums[1] = 0
|
||||
nums[1] = 0 # インデックス 1 の要素を 0 に更新する
|
||||
# [endregion]{update_element}
|
||||
print("\nインデックス 1 の要素を 0 に更新した結果 nums =", nums)
|
||||
|
||||
# [region]{clear_list}
|
||||
# リストを空にする
|
||||
nums.clear()
|
||||
# [endregion]{clear_list}
|
||||
print("\nリストを空にした後 nums =", nums)
|
||||
|
||||
# [region]{append_element}
|
||||
# 末尾に要素を追加する
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
# [endregion]{append_element}
|
||||
print("\n要素を追加した後 nums =", nums)
|
||||
|
||||
# [region]{insert_element}
|
||||
# 途中に要素を挿入する
|
||||
nums.insert(3, 6)
|
||||
nums.insert(3, 6) # インデックス 3 に数値 6 を挿入する
|
||||
# [endregion]{insert_element}
|
||||
print("\nインデックス 3 に数値 6 を挿入した結果 nums =", nums)
|
||||
|
||||
# [region]{remove_element}
|
||||
# 要素を削除する
|
||||
nums.pop(3)
|
||||
nums.pop(3) # インデックス 3 の要素を削除する
|
||||
# [endregion]{remove_element}
|
||||
print("\nインデックス 3 の要素を削除した結果 nums =", nums)
|
||||
|
||||
# [region]{iterate_list}
|
||||
# インデックスを使ってリストを走査する
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
|
||||
# リストの要素を直接走査する
|
||||
for num in nums:
|
||||
count += num
|
||||
# [endregion]{iterate_list}
|
||||
|
||||
# [region]{concat_list}
|
||||
# 2 つのリストを連結する
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
nums += nums1 # リスト nums の後ろに nums1 を連結する
|
||||
# [endregion]{concat_list}
|
||||
print("\nリスト nums の後ろに nums1 を連結した結果 nums =", nums)
|
||||
|
||||
# [region]{sort_list}
|
||||
# リストをソートする
|
||||
nums.sort()
|
||||
nums.sort() # ソート後、リストの要素は昇順に並ぶ
|
||||
# [endregion]{sort_list}
|
||||
print("\nリストをソートした後 nums =", nums)
|
||||
|
||||
Reference in New Issue
Block a user