mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-08 13:36:06 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -14,41 +14,41 @@ import heapq
|
||||
|
||||
|
||||
def test_push(heap: list, val: int, flag: int = 1):
|
||||
heapq.heappush(heap, flag * val) # Push the element into heap
|
||||
print(f"\nElement {val} after pushed into heap")
|
||||
heapq.heappush(heap, flag * val) # Element enters heap
|
||||
print(f"\nAfter element {val} enters heap")
|
||||
print_heap([flag * val for val in heap])
|
||||
|
||||
|
||||
def test_pop(heap: list, flag: int = 1):
|
||||
val = flag * heapq.heappop(heap) # Pop the element at the heap top
|
||||
print(f"\nHeap top element {val} after exiting heap")
|
||||
val = flag * heapq.heappop(heap) # Top element exits heap
|
||||
print(f"\nAfter top element {val} exits heap")
|
||||
print_heap([flag * val for val in heap])
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize min-heap
|
||||
# Initialize min heap
|
||||
min_heap, flag = [], 1
|
||||
# Initialize max-heap
|
||||
# Initialize max heap
|
||||
max_heap, flag = [], -1
|
||||
|
||||
print("\nThe following test case is for max-heap")
|
||||
# Python's heapq module implements min-heap by default
|
||||
# Consider "negating the elements" before entering the heap, thus reversing the comparator to implement a max-heap
|
||||
# In this example, flag = 1 corresponds to min-heap, flag = -1 corresponds to max-heap
|
||||
print("\nThe following test cases are for max heap")
|
||||
# Python's heapq module implements min heap by default
|
||||
# Consider negating the elements before entering the heap, which can reverse the size relationship, thus implementing max heap
|
||||
# In this example, flag = 1 corresponds to min heap, flag = -1 corresponds to max heap
|
||||
|
||||
# Push the element into heap
|
||||
# Elements enter heap
|
||||
test_push(max_heap, 1, flag)
|
||||
test_push(max_heap, 3, flag)
|
||||
test_push(max_heap, 2, flag)
|
||||
test_push(max_heap, 5, flag)
|
||||
test_push(max_heap, 4, flag)
|
||||
|
||||
# Access heap top element
|
||||
# Get top element
|
||||
peek: int = flag * max_heap[0]
|
||||
print(f"\nHeap top element is {peek}")
|
||||
print(f"\nTop element is {peek}")
|
||||
|
||||
# Pop the element at the heap top
|
||||
# Top element exits heap
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
test_pop(max_heap, flag)
|
||||
@@ -59,13 +59,13 @@ if __name__ == "__main__":
|
||||
size: int = len(max_heap)
|
||||
print(f"\nNumber of heap elements is {size}")
|
||||
|
||||
# Determine if heap is empty
|
||||
# Check if heap is empty
|
||||
is_empty: bool = not max_heap
|
||||
print(f"\nIs the heap empty {is_empty}")
|
||||
print(f"\nIs heap empty {is_empty}")
|
||||
|
||||
# Enter list and build heap
|
||||
# Input list and build heap
|
||||
# Time complexity is O(n), not O(nlogn)
|
||||
min_heap = [1, 3, 2, 5, 4]
|
||||
heapq.heapify(min_heap)
|
||||
print("\nEnter list and build min-heap")
|
||||
print("\nAfter inputting list and building min heap")
|
||||
print_heap(min_heap)
|
||||
|
||||
@@ -12,13 +12,13 @@ from modules import print_heap
|
||||
|
||||
|
||||
class MaxHeap:
|
||||
"""Max-heap"""
|
||||
"""Max heap"""
|
||||
|
||||
def __init__(self, nums: list[int]):
|
||||
"""Constructor, build heap based on input list"""
|
||||
# Add all list elements into the heap
|
||||
# Add list elements to heap as is
|
||||
self.max_heap = nums
|
||||
# Heapify all nodes except leaves
|
||||
# Heapify all nodes except leaf nodes
|
||||
for i in range(self.parent(self.size() - 1), -1, -1):
|
||||
self.sift_down(i)
|
||||
|
||||
@@ -32,7 +32,7 @@ class MaxHeap:
|
||||
|
||||
def parent(self, i: int) -> int:
|
||||
"""Get index of parent node"""
|
||||
return (i - 1) // 2 # Integer division down
|
||||
return (i - 1) // 2 # Floor division
|
||||
|
||||
def swap(self, i: int, j: int):
|
||||
"""Swap elements"""
|
||||
@@ -43,62 +43,62 @@ class MaxHeap:
|
||||
return len(self.max_heap)
|
||||
|
||||
def is_empty(self) -> bool:
|
||||
"""Determine if heap is empty"""
|
||||
"""Check if heap is empty"""
|
||||
return self.size() == 0
|
||||
|
||||
def peek(self) -> int:
|
||||
"""Access heap top element"""
|
||||
"""Access top element"""
|
||||
return self.max_heap[0]
|
||||
|
||||
def push(self, val: int):
|
||||
"""Push the element into heap"""
|
||||
"""Element enters heap"""
|
||||
# Add node
|
||||
self.max_heap.append(val)
|
||||
# Heapify from bottom to top
|
||||
self.sift_up(self.size() - 1)
|
||||
|
||||
def sift_up(self, i: int):
|
||||
"""Start heapifying node i, from bottom to top"""
|
||||
"""Starting from node i, heapify from bottom to top"""
|
||||
while True:
|
||||
# Get parent node of node i
|
||||
p = self.parent(i)
|
||||
# When "crossing the root node" or "node does not need repair", end heapification
|
||||
# When "crossing root node" or "node needs no repair", end heapify
|
||||
if p < 0 or self.max_heap[i] <= self.max_heap[p]:
|
||||
break
|
||||
# Swap two nodes
|
||||
self.swap(i, p)
|
||||
# Loop upwards heapification
|
||||
# Loop upward heapify
|
||||
i = p
|
||||
|
||||
def pop(self) -> int:
|
||||
"""Element exits heap"""
|
||||
# Empty handling
|
||||
# Handle empty case
|
||||
if self.is_empty():
|
||||
raise IndexError("Heap is empty")
|
||||
# Swap the root node with the rightmost leaf node (swap the first element with the last element)
|
||||
# Swap root node with rightmost leaf node (swap first element with last element)
|
||||
self.swap(0, self.size() - 1)
|
||||
# Remove node
|
||||
# Delete node
|
||||
val = self.max_heap.pop()
|
||||
# Heapify from top to bottom
|
||||
self.sift_down(0)
|
||||
# Return heap top element
|
||||
# Return top element
|
||||
return val
|
||||
|
||||
def sift_down(self, i: int):
|
||||
"""Start heapifying node i, from top to bottom"""
|
||||
"""Starting from node i, heapify from top to bottom"""
|
||||
while True:
|
||||
# Determine the largest node among i, l, r, noted as ma
|
||||
# Find node with largest value among i, l, r, denoted as ma
|
||||
l, r, ma = self.left(i), self.right(i), i
|
||||
if l < self.size() and self.max_heap[l] > self.max_heap[ma]:
|
||||
ma = l
|
||||
if r < self.size() and self.max_heap[r] > self.max_heap[ma]:
|
||||
ma = r
|
||||
# If node i is the largest or indices l, r are out of bounds, no further heapification needed, break
|
||||
# If node i is largest or indices l, r are out of bounds, no need to continue heapify, break
|
||||
if ma == i:
|
||||
break
|
||||
# Swap two nodes
|
||||
self.swap(i, ma)
|
||||
# Loop downwards heapification
|
||||
# Loop downward heapify
|
||||
i = ma
|
||||
|
||||
def print(self):
|
||||
@@ -108,30 +108,30 @@ class MaxHeap:
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize max-heap
|
||||
# Initialize max heap
|
||||
max_heap = MaxHeap([9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2])
|
||||
print("\nEnter list and build heap")
|
||||
print("\nAfter inputting list and building heap")
|
||||
max_heap.print()
|
||||
|
||||
# Access heap top element
|
||||
# Get top element
|
||||
peek = max_heap.peek()
|
||||
print(f"\nHeap top element is {peek}")
|
||||
print(f"\nTop element is {peek}")
|
||||
|
||||
# Push the element into heap
|
||||
# Element enters heap
|
||||
val = 7
|
||||
max_heap.push(val)
|
||||
print(f"\nElement {val} after pushed into heap")
|
||||
print(f"\nAfter element {val} enters heap")
|
||||
max_heap.print()
|
||||
|
||||
# Pop the element at the heap top
|
||||
# Top element exits heap
|
||||
peek = max_heap.pop()
|
||||
print(f"\nHeap top element {peek} after exiting heap")
|
||||
print(f"\nAfter top element {peek} exits heap")
|
||||
max_heap.print()
|
||||
|
||||
# Get heap size
|
||||
size = max_heap.size()
|
||||
print(f"\nNumber of heap elements is {size}")
|
||||
|
||||
# Determine if heap is empty
|
||||
# Check if heap is empty
|
||||
is_empty = max_heap.is_empty()
|
||||
print(f"\nIs the heap empty {is_empty}")
|
||||
print(f"\nIs heap empty {is_empty}")
|
||||
|
||||
@@ -14,15 +14,15 @@ import heapq
|
||||
|
||||
|
||||
def top_k_heap(nums: list[int], k: int) -> list[int]:
|
||||
"""Using heap to find the largest k elements in an array"""
|
||||
# Initialize min-heap
|
||||
"""Find the largest k elements in array based on heap"""
|
||||
# Initialize min heap
|
||||
heap = []
|
||||
# Enter the first k elements of the array into the heap
|
||||
# Enter the first k elements of array into heap
|
||||
for i in range(k):
|
||||
heapq.heappush(heap, nums[i])
|
||||
# From the k+1th element, keep the heap length as k
|
||||
# Starting from the (k+1)th element, maintain heap length as k
|
||||
for i in range(k, len(nums)):
|
||||
# If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
|
||||
# If current element is greater than top element, top element exits heap, current element enters heap
|
||||
if nums[i] > heap[0]:
|
||||
heapq.heappop(heap)
|
||||
heapq.heappush(heap, nums[i])
|
||||
|
||||
Reference in New Issue
Block a user