mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-03 12:21:09 +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:
|
||||
|
||||
"""Driver code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize the linked list
|
||||
# [region]{init_linked_list}
|
||||
# Initialize the linked list 1 -> 3 -> 2 -> 5 -> 4
|
||||
# Initialize the individual nodes
|
||||
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("The initialized linked list is")
|
||||
print_linked_list(n0)
|
||||
|
||||
|
||||
@@ -10,47 +10,66 @@ if __name__ == "__main__":
|
||||
nums: list[int] = [1, 3, 2, 5, 4]
|
||||
print("\nList nums =", nums)
|
||||
|
||||
# [region]{access_element}
|
||||
# Access an element
|
||||
x: int = nums[1]
|
||||
x: int = nums[1] # Access the element at index 1
|
||||
# [endregion]{access_element}
|
||||
print("\nAccess the element at index 1, giving x =", x)
|
||||
|
||||
# [region]{update_element}
|
||||
# Update an element
|
||||
nums[1] = 0
|
||||
nums[1] = 0 # Update the element at index 1 to 0
|
||||
# [endregion]{update_element}
|
||||
print("\nUpdate the element at index 1 to 0, giving nums =", nums)
|
||||
|
||||
# [region]{clear_list}
|
||||
# Clear the list
|
||||
nums.clear()
|
||||
# [endregion]{clear_list}
|
||||
print("\nAfter clearing the list, nums =", nums)
|
||||
|
||||
# [region]{append_element}
|
||||
# Append an element at the end
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
# [endregion]{append_element}
|
||||
print("\nAfter adding elements, nums =", nums)
|
||||
|
||||
# [region]{insert_element}
|
||||
# Insert an element in the middle
|
||||
nums.insert(3, 6)
|
||||
nums.insert(3, 6) # Insert the number 6 at index 3
|
||||
# [endregion]{insert_element}
|
||||
print("\nInsert the number 6 at index 3, giving nums =", nums)
|
||||
|
||||
# [region]{remove_element}
|
||||
# Remove an element
|
||||
nums.pop(3)
|
||||
nums.pop(3) # Remove the element at index 3
|
||||
# [endregion]{remove_element}
|
||||
print("\nRemove the element at index 3, giving nums =", nums)
|
||||
|
||||
# [region]{iterate_list}
|
||||
# Traverse the list by index
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
|
||||
# Iterate directly over the list elements
|
||||
for num in nums:
|
||||
count += num
|
||||
# [endregion]{iterate_list}
|
||||
|
||||
# [region]{concat_list}
|
||||
# Concatenate two lists
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
nums += nums1 # Append list nums1 to nums
|
||||
# [endregion]{concat_list}
|
||||
print("\nAppend list nums1 to nums, giving nums =", nums)
|
||||
|
||||
# [region]{sort_list}
|
||||
# Sort the list
|
||||
nums.sort()
|
||||
nums.sort() # After sorting, the list elements are in ascending order
|
||||
# [endregion]{sort_list}
|
||||
print("\nAfter sorting the list, nums =", nums)
|
||||
|
||||
Reference in New Issue
Block a user