Skip to content

Commit

Permalink
Update code style for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Mar 2, 2023
1 parent 7e9e6b0 commit 7c50114
Show file tree
Hide file tree
Showing 45 changed files with 274 additions and 266 deletions.
6 changes: 3 additions & 3 deletions codes/javascript/chapter_sorting/quick_sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,19 @@ class QuickSortTailCall {

/* Driver Code */
/* 快速排序 */
const nums = [4, 1, 3, 1, 5, 2];
const nums = [2, 4, 1, 0, 3, 5];
const quickSort = new QuickSort();
quickSort.quickSort(nums, 0, nums.length - 1);
console.log('快速排序完成后 nums =', nums);

/* 快速排序(中位基准数优化) */
const nums1 = [4, 1, 3, 1, 5, 2];
const nums1 = [2, 4, 1, 0, 3, 5];
const quickSortMedian = new QuickSort();
quickSortMedian.quickSort(nums1, 0, nums1.length - 1);
console.log('快速排序(中位基准数优化)完成后 nums =', nums1);

/* 快速排序(尾递归优化) */
const nums2 = [4, 1, 3, 1, 5, 2];
const nums2 = [2, 4, 1, 0, 3, 5];
const quickSortTailCall = new QuickSort();
quickSortTailCall.quickSort(nums2, 0, nums2.length - 1);
console.log('快速排序(尾递归优化)完成后 nums =', nums2);
14 changes: 7 additions & 7 deletions codes/python/chapter_array_and_linkedlist/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *

""" 随机访问元素 """
def random_access(nums):
""" 随机访问元素 """
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素
random_num = nums[random_index]
return random_num

""" 扩展数组长度 """
# 请注意,Python 的 list 是动态数组,可以直接扩展
# 为了方便学习,本函数将 list 看作是长度不可变的数组
def extend(nums, enlarge):
""" 扩展数组长度 """
# 初始化一个扩展长度后的数组
res = [0] * (len(nums) + enlarge)
# 将原数组中的所有元素复制到新数组
Expand All @@ -28,22 +28,22 @@ def extend(nums, enlarge):
# 返回扩展后的新数组
return res

""" 在数组的索引 index 处插入元素 num """
def insert(nums, num, index):
""" 在数组的索引 index 处插入元素 num """
# 把索引 index 以及之后的所有元素向后移动一位
for i in range(len(nums) - 1, index, -1):
nums[i] = nums[i - 1]
# 将 num 赋给 index 处元素
nums[index] = num

""" 删除索引 index 处元素 """
def remove(nums, index):
""" 删除索引 index 处元素 """
# 把索引 index 之后的所有元素向前移动一位
for i in range(index, len(nums) - 1):
nums[i] = nums[i + 1]

""" 遍历数组 """
def traverse(nums):
""" 遍历数组 """
count = 0
# 通过索引遍历数组
for i in range(len(nums)):
Expand All @@ -52,8 +52,8 @@ def traverse(nums):
for num in nums:
count += 1

""" 在数组中查找指定元素 """
def find(nums, target):
""" 在数组中查找指定元素 """
for i in range(len(nums)):
if nums[i] == target:
return i
Expand Down
10 changes: 5 additions & 5 deletions codes/python/chapter_array_and_linkedlist/linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *

""" 在链表的结点 n0 之后插入结点 P """
def insert(n0, P):
""" 在链表的结点 n0 之后插入结点 P """
n1 = n0.next
P.next = n1
n0.next = P

""" 删除链表的结点 n0 之后的首个结点 """
def remove(n0):
""" 删除链表的结点 n0 之后的首个结点 """
if not n0.next:
return
# n0 -> P -> n1
P = n0.next
n1 = P.next
n0.next = n1

""" 访问链表中索引为 index 的结点 """
def access(head, index):
""" 访问链表中索引为 index 的结点 """
for _ in range(index):
if not head:
return None
head = head.next
return head

""" 在链表中查找值为 target 的首个结点 """
def find(head, target):
""" 在链表中查找值为 target 的首个结点 """
index = 0
while head:
if head.val == target:
Expand Down
2 changes: 1 addition & 1 deletion codes/python/chapter_array_and_linkedlist/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *


""" Driver Code """
Expand Down
39 changes: 22 additions & 17 deletions codes/python/chapter_array_and_linkedlist/my_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,47 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *

""" 列表类简易实现 """
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):
""" 尾部添加元素 """
# 元素数量超出容量时,触发扩容机制
if self.size() == self.capacity():
self.extend_capacity();
self.__nums[self.__size] = num
self.__size += 1

""" 中间插入(尾部添加)元素 """
def add(self, num, index=-1):
def insert(self, num, index):
""" 中间插入元素 """
assert index >= 0 and index < self.__size, "索引越界"
# 若不指定索引 index ,则向数组尾部添加元素
if index == -1:
index = self.__size
# 元素数量超出容量时,触发扩容机制
if self.__size == self.capacity():
self.extend_capacity()
Expand All @@ -52,10 +57,10 @@ def add(self, num, index=-1):
# 更新元素数量
self.__size += 1

""" 删除元素 """
def remove(self, index):
""" 删除元素 """
assert index >= 0 and index < self.__size, "索引越界"
num = self.nums[index]
num = self.__nums[index]
# 索引 i 之后的元素都向前移动一位
for j in range(index, self.__size - 1):
self.__nums[j] = self.__nums[j + 1]
Expand All @@ -64,15 +69,15 @@ def remove(self, index):
# 返回被删除元素
return num

""" 列表扩容 """
def extend_capacity(self):
""" 列表扩容 """
# 新建一个长度为 self.__size 的数组,并将原数组拷贝到新数组
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
# 更新列表容量
self.__capacity = len(self.__nums)

""" 返回有效长度的列表 """
def to_array(self):
""" 返回有效长度的列表 """
return self.__nums[:self.__size]


Expand All @@ -90,7 +95,7 @@ def to_array(self):
.format(list.to_array(), list.capacity(), list.size()))

""" 中间插入元素 """
list.add(num=6, index=3)
list.insert(6, index=3)
print("在索引 3 处插入数字 6 ,得到 list =", list.to_array())

""" 删除元素 """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *

""" 方法一:暴力枚举 """
def two_sum_brute_force(nums: List[int], target: int) -> List[int]:
""" 方法一:暴力枚举 """
# 两层循环,时间复杂度 O(n^2)
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
if nums[i] + nums[j] == target:
return i, j
return []

""" 方法二:辅助哈希表 """
def two_sum_hash_table(nums: List[int], target: int) -> List[int]:
""" 方法二:辅助哈希表 """
# 辅助哈希表,空间复杂度 O(n)
dic = {}
# 单层循环,时间复杂度 O(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import sys, os.path as osp
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
from modules import *

""" 函数 """
def function():
""" 函数 """
# do something
return 0

""" 常数阶 """
def constant(n):
""" 常数阶 """
# 常量、变量、对象占用 O(1) 空间
a = 0
nums = [0] * 10000
Expand All @@ -26,35 +26,35 @@ def constant(n):
for _ in range(n):
function()

""" 线性阶 """
def linear(n):
""" 线性阶 """
# 长度为 n 的列表占用 O(n) 空间
nums = [0] * n
# 长度为 n 的哈希表占用 O(n) 空间
mapp = {}
for i in range(n):
mapp[i] = str(i)

""" 线性阶(递归实现) """
def linear_recur(n):
""" 线性阶(递归实现) """
print("递归 n =", n)
if n == 1: return
linear_recur(n - 1)

""" 平方阶 """
def quadratic(n):
""" 平方阶 """
# 二维列表占用 O(n^2) 空间
num_matrix = [[0] * n for _ in range(n)]

""" 平方阶(递归实现) """
def quadratic_recur(n):
""" 平方阶(递归实现) """
if n <= 0: return 0
# 数组 nums 长度为 n, n-1, ..., 2, 1
nums = [0] * n
return quadratic_recur(n - 1)

""" 指数阶(建立满二叉树) """
def build_tree(n):
""" 指数阶(建立满二叉树) """
if n == 0: return None
root = TreeNode(0)
root.left = build_tree(n - 1)
Expand Down
Loading

0 comments on commit 7c50114

Please sign in to comment.