mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-25 11:13:38 +08:00
1. Add the building util of Python
for the markdown docs. 2. Update the deploy.sh
This commit is contained in:
51
docs/chapter_array_and_linkedlist/array.md
Normal file → Executable file
51
docs/chapter_array_and_linkedlist/array.md
Normal file → Executable file
@ -143,13 +143,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 随机访问元素 """
|
||||
def random_access(nums):
|
||||
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
|
||||
random_index = random.randint(0, len(nums) - 1)
|
||||
# 获取并返回随机元素
|
||||
random_num = nums[random_index]
|
||||
return random_num
|
||||
[class]{}-[func]{random_access}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -279,17 +273,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 扩展数组长度 """
|
||||
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
||||
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
||||
def extend(nums, enlarge):
|
||||
# 初始化一个扩展长度后的数组
|
||||
res = [0] * (len(nums) + enlarge)
|
||||
# 将原数组中的所有元素复制到新数组
|
||||
for i in range(len(nums)):
|
||||
res[i] = nums[i]
|
||||
# 返回扩展后的新数组
|
||||
return res
|
||||
[class]{}-[func]{extend}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -452,19 +436,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 在数组的索引 index 处插入元素 num """
|
||||
def insert(nums, num, index):
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
# 将 num 赋给 index 处元素
|
||||
nums[index] = num
|
||||
[class]{}-[func]{insert}
|
||||
|
||||
""" 删除索引 index 处元素 """
|
||||
def remove(nums, index):
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
[class]{}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -648,15 +622,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 遍历数组 """
|
||||
def traverse(nums):
|
||||
count = 0
|
||||
# 通过索引遍历数组
|
||||
for i in range(len(nums)):
|
||||
count += 1
|
||||
# 直接遍历数组
|
||||
for num in nums:
|
||||
count += 1
|
||||
[class]{}-[func]{traverse}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -803,12 +769,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
|
||||
=== "Python"
|
||||
|
||||
```python title="array.py"
|
||||
""" 在数组中查找指定元素 """
|
||||
def find(nums, target):
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target:
|
||||
return i
|
||||
return -1
|
||||
[class]{}-[func]{find}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
33
docs/chapter_array_and_linkedlist/linked_list.md
Normal file → Executable file
33
docs/chapter_array_and_linkedlist/linked_list.md
Normal file → Executable file
@ -369,20 +369,9 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 在链表的结点 n0 之后插入结点 P """
|
||||
def insert(n0, P):
|
||||
n1 = n0.next
|
||||
n0.next = P
|
||||
P.next = n1
|
||||
[class]{}-[func]{insert}
|
||||
|
||||
""" 删除链表的结点 n0 之后的首个结点 """
|
||||
def remove(n0):
|
||||
if not n0.next:
|
||||
return
|
||||
# n0 -> P -> n1
|
||||
P = n0.next
|
||||
n1 = P.next
|
||||
n0.next = n1
|
||||
[class]{}-[func]{remove}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -557,13 +546,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 访问链表中索引为 index 的结点 """
|
||||
def access(head, index):
|
||||
for _ in range(index):
|
||||
if not head:
|
||||
return None
|
||||
head = head.next
|
||||
return head
|
||||
[class]{}-[func]{access}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -704,15 +687,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="linked_list.py"
|
||||
""" 在链表中查找值为 target 的首个结点 """
|
||||
def find(head, target):
|
||||
index = 0
|
||||
while head:
|
||||
if head.val == target:
|
||||
return index
|
||||
head = head.next
|
||||
index += 1
|
||||
return -1
|
||||
[class]{}-[func]{find}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
63
docs/chapter_array_and_linkedlist/list.md
Normal file → Executable file
63
docs/chapter_array_and_linkedlist/list.md
Normal file → Executable file
@ -912,68 +912,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="my_list.py"
|
||||
""" 列表类简易实现 """
|
||||
class MyList:
|
||||
""" 构造函数 """
|
||||
def __init__(self):
|
||||
self.__capacity = 10 # 列表容量
|
||||
self.__nums = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__size = 0 # 列表长度(即当前元素数量)
|
||||
self.__extend_ratio = 2 # 每次列表扩容的倍数
|
||||
|
||||
""" 获取列表长度(即当前元素数量) """
|
||||
def size(self):
|
||||
return self.__size
|
||||
|
||||
""" 获取列表容量 """
|
||||
def capacity(self):
|
||||
return self.__capacity
|
||||
|
||||
""" 访问元素 """
|
||||
def get(self, index):
|
||||
# 索引如果越界则抛出异常,下同
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
return self.__nums[index]
|
||||
|
||||
""" 更新元素 """
|
||||
def set(self, num, index):
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
self.__nums[index] = num
|
||||
|
||||
""" 中间插入(尾部添加)元素 """
|
||||
def add(self, num, index=-1):
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
# 若不指定索引 index ,则向数组尾部添加元素
|
||||
if index == -1:
|
||||
index = self.__size
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.__size == self.capacity():
|
||||
self.extend_capacity()
|
||||
# 索引 i 以及之后的元素都向后移动一位
|
||||
for j in range(self.__size - 1, index - 1, -1):
|
||||
self.__nums[j + 1] = self.__nums[j]
|
||||
self.__nums[index] = num
|
||||
# 更新元素数量
|
||||
self.__size += 1
|
||||
|
||||
""" 删除元素 """
|
||||
def remove(self, index):
|
||||
assert index >= 0 and index < self.__size, "索引越界"
|
||||
num = self.nums[index]
|
||||
# 索引 i 之后的元素都向前移动一位
|
||||
for j in range(index, self.__size - 1):
|
||||
self.__nums[j] = self.__nums[j + 1]
|
||||
# 更新元素数量
|
||||
self.__size -= 1
|
||||
# 返回被删除元素
|
||||
return num
|
||||
|
||||
""" 列表扩容 """
|
||||
def extend_capacity(self):
|
||||
# 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
|
||||
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
|
||||
# 更新列表容量
|
||||
self.__capacity = len(self.__nums)
|
||||
[class]{MyList}-[func]{}
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
Reference in New Issue
Block a user