mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Update code extractor of Java.
This commit is contained in:
parent
95bffcfb36
commit
8f512c2840
9 changed files with 99 additions and 87 deletions
|
@ -8,7 +8,7 @@
|
|||
|
||||
#define MAX_SIZE 5000
|
||||
|
||||
// 大顶堆
|
||||
/* 大顶堆 */
|
||||
typedef struct maxHeap {
|
||||
// size 代表的是实际元素的个数
|
||||
int size;
|
||||
|
@ -20,14 +20,6 @@ void siftDown(maxHeap *h, int i);
|
|||
|
||||
void siftUp(maxHeap *h, int i);
|
||||
|
||||
/* 构造空堆 */
|
||||
maxHeap *newEmptyMaxHeap() {
|
||||
// 所有元素入堆
|
||||
maxHeap *h = (maxHeap *) malloc(sizeof(maxHeap));
|
||||
h->size = 0;
|
||||
return h;
|
||||
}
|
||||
|
||||
/* 构造函数,根据切片建堆 */
|
||||
maxHeap *newMaxHeap(int nums[], int size) {
|
||||
// 所有元素入堆
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
#include "../include/include.hpp"
|
||||
|
||||
/* 最大堆类 */
|
||||
/* 大顶堆 */
|
||||
class MaxHeap {
|
||||
private:
|
||||
// 使用动态数组,这样无需考虑扩容问题
|
||||
|
@ -62,9 +62,6 @@ private:
|
|||
}
|
||||
|
||||
public:
|
||||
/* 构造函数,建立空堆 */
|
||||
MaxHeap() {}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
MaxHeap(vector<int> nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
|
|
|
@ -9,16 +9,11 @@ package chapter_heap;
|
|||
import include.*;
|
||||
import java.util.*;
|
||||
|
||||
/* 最大堆类 */
|
||||
/* 大顶堆 */
|
||||
class MaxHeap {
|
||||
// 使用列表而非数组,这样无需考虑扩容问题
|
||||
private List<Integer> maxHeap;
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
public MaxHeap() {
|
||||
maxHeap = new ArrayList<>();
|
||||
}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
public MaxHeap(List<Integer> nums) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
|
|
|
@ -14,12 +14,12 @@ function hierOrder(root) {
|
|||
// 初始化一个列表,用于保存遍历序列
|
||||
let list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存结点值
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存结点值
|
||||
if (node.left)
|
||||
queue.push(node.left); // 左子结点入队
|
||||
queue.push(node.left); // 左子结点入队
|
||||
if (node.right)
|
||||
queue.push(node.right); // 右子结点入队
|
||||
queue.push(node.right); // 右子结点入队
|
||||
|
||||
}
|
||||
return list;
|
||||
|
|
|
@ -6,14 +6,10 @@
|
|||
|
||||
import utils
|
||||
|
||||
/* 大顶堆 */
|
||||
class MaxHeap {
|
||||
private var maxHeap: [Int]
|
||||
|
||||
/* 构造函数,建立空堆 */
|
||||
init() {
|
||||
maxHeap = []
|
||||
}
|
||||
|
||||
/* 构造函数,根据输入列表建堆 */
|
||||
init(nums: [Int]) {
|
||||
// 将列表元素原封不动添加进堆
|
||||
|
|
|
@ -260,11 +260,6 @@ comments: true
|
|||
=== "Java"
|
||||
|
||||
```java title="my_heap.java"
|
||||
// 使用列表而非数组,这样无需考虑扩容问题
|
||||
List<Integer> maxHeap;
|
||||
|
||||
[class]{MaxHeap}-[func]{MaxHeap}
|
||||
|
||||
[class]{MaxHeap}-[func]{left}
|
||||
|
||||
[class]{MaxHeap}-[func]{right}
|
||||
|
|
|
@ -15,7 +15,7 @@ comments: true
|
|||
=== "Java"
|
||||
|
||||
```java title="linear_search.java"
|
||||
[class]{hashing_search}-[func]{linearSearchArray}
|
||||
[class]{linear_search}-[func]{linearSearchArray}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
@ -145,7 +145,7 @@ comments: true
|
|||
=== "Java"
|
||||
|
||||
```java title="linear_search.java"
|
||||
[class]{hashing_search}-[func]{linearSearchLinkedList}
|
||||
[class]{linear_search}-[func]{linearSearchLinkedList}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
|
|
@ -8,8 +8,9 @@ import re
|
|||
import os
|
||||
import os.path as osp
|
||||
|
||||
|
||||
class ExtractCodeBlocksJava:
|
||||
def __init__(self) -> None:
|
||||
def __init__(self) -> None:
|
||||
self.langs = ["java"]
|
||||
# Pattern to match function names and class names
|
||||
self.func_pattern = r'(\s+)(public|private|)\s*(static|)\s*(\S+)\s+(\w+)(\(.*\))\s+\{'
|
||||
|
@ -17,40 +18,38 @@ class ExtractCodeBlocksJava:
|
|||
# Pattern to match the start and end of a block
|
||||
self.block_end_pattern = '^\s{ind}\}'
|
||||
self.block_start_pattern = '^\s{ind}\/\*.+\*\/'
|
||||
|
||||
|
||||
def extract(self, file_path):
|
||||
"""
|
||||
Extract classes and functions from a markdown document
|
||||
"""
|
||||
self.file_path = file_path
|
||||
with open(file_path) as f:
|
||||
self.lines = f.readlines()
|
||||
self.content = "".join(self.lines)
|
||||
# Detect and extract all the classes along with its fucntions
|
||||
|
||||
# Detect and extract all the classes and fucntions
|
||||
classes = self.extract_class_blocks()
|
||||
# Remove 'static'
|
||||
self.post_process(classes)
|
||||
|
||||
funcs = self.extract_function_blocks()
|
||||
|
||||
self.post_process(classes, funcs)
|
||||
|
||||
return {
|
||||
"classes": classes
|
||||
"classes": classes,
|
||||
"funcs": funcs,
|
||||
}
|
||||
|
||||
def post_process(self, classes):
|
||||
for clas in classes.values():
|
||||
funcs = clas["funcs"]
|
||||
for func in funcs.values():
|
||||
for i, line in enumerate(func["block"]):
|
||||
if "static " in line:
|
||||
func["block"][i] = line.replace("static ", "")
|
||||
break
|
||||
|
||||
|
||||
def search_block(self, header_line, indentation):
|
||||
"""
|
||||
Search class/function block given the header_line and indentation
|
||||
"""
|
||||
start_line, end_line = 0, len(self.lines)
|
||||
|
||||
block_end_pattern = re.compile(self.block_end_pattern.replace("ind", str(indentation)))
|
||||
block_start_pattern = re.compile(self.block_start_pattern.replace("ind", str(indentation)))
|
||||
|
||||
|
||||
block_end_pattern = re.compile(
|
||||
self.block_end_pattern.replace("ind", str(indentation)))
|
||||
block_start_pattern = re.compile(
|
||||
self.block_start_pattern.replace("ind", str(indentation)))
|
||||
|
||||
# Search the code
|
||||
for i in range(header_line + 1, len(self.lines)):
|
||||
if re.match(block_end_pattern, self.lines[i]) is not None:
|
||||
|
@ -61,39 +60,35 @@ class ExtractCodeBlocksJava:
|
|||
if re.search(block_start_pattern, self.lines[i]) is not None:
|
||||
start_line = i
|
||||
break
|
||||
code_block = self.lines[start_line:end_line + 1]
|
||||
# Remove empty lines at bottom
|
||||
for i in range(len(code_block) - 1, -1, -1):
|
||||
if re.search("^\s*\n", code_block[i]) is None:
|
||||
break
|
||||
end_line -= 1
|
||||
|
||||
|
||||
return start_line, end_line, self.lines[start_line:end_line + 1]
|
||||
|
||||
|
||||
def extract_function_blocks(self, indentation=4, start_line=-1, end_line=-1):
|
||||
"""
|
||||
Extract all the functions with given indentation
|
||||
"""
|
||||
funcs = {}
|
||||
|
||||
|
||||
if start_line == -1:
|
||||
start_line = 0
|
||||
if end_line == -1:
|
||||
end_line = len(self.lines) - 1
|
||||
|
||||
|
||||
func_pattern = re.compile(self.func_pattern)
|
||||
|
||||
|
||||
for line_num in range(start_line, end_line + 1):
|
||||
# Search the function header
|
||||
func_match = func_pattern.match(self.lines[line_num])
|
||||
if func_match is None: continue
|
||||
if func_match is None:
|
||||
continue
|
||||
# The function should match the input indentation
|
||||
if len(func_match.group(1)) != indentation: continue
|
||||
if len(func_match.group(1)) != indentation:
|
||||
continue
|
||||
header_line = line_num
|
||||
|
||||
|
||||
# Search the block from the header line
|
||||
start_line, end_line, func_block = self.search_block(header_line, indentation)
|
||||
start_line, end_line, func_block = self.search_block(
|
||||
header_line, indentation)
|
||||
# Construct the funcs dict
|
||||
func_label = func_match.group(5)
|
||||
funcs[func_label] = {
|
||||
|
@ -105,7 +100,7 @@ class ExtractCodeBlocksJava:
|
|||
},
|
||||
"block": func_block,
|
||||
}
|
||||
|
||||
|
||||
return funcs
|
||||
|
||||
def extract_class_blocks(self):
|
||||
|
@ -113,17 +108,19 @@ class ExtractCodeBlocksJava:
|
|||
Extract all the classes with given indentation
|
||||
"""
|
||||
classes = {}
|
||||
|
||||
|
||||
class_pattern = re.compile(self.class_pattern)
|
||||
|
||||
|
||||
for line_num, line in enumerate(self.lines):
|
||||
# Search the class header
|
||||
class_match = class_pattern.match(line)
|
||||
if class_match is None: continue
|
||||
if class_match is None:
|
||||
continue
|
||||
header_line = line_num
|
||||
|
||||
|
||||
# Search the block from the header line
|
||||
start_line, end_line, class_block = self.search_block(header_line, 0)
|
||||
start_line, end_line, class_block = self.search_block(
|
||||
header_line, 0)
|
||||
# Construct the classes dict
|
||||
class_label = class_match.group(2)
|
||||
classes[class_label] = {
|
||||
|
@ -137,9 +134,24 @@ class ExtractCodeBlocksJava:
|
|||
"funcs": self.extract_function_blocks(
|
||||
indentation=4, start_line=start_line, end_line=end_line)
|
||||
}
|
||||
|
||||
|
||||
return classes
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def remove_keyword(func):
|
||||
block = func["block"]
|
||||
header_line = func["line_number"]["header"] - \
|
||||
func["line_number"]["start"]
|
||||
block[header_line] = block[header_line] \
|
||||
.replace("static ", "").replace("public ", "").replace("private ", "")
|
||||
for clas in classes.values():
|
||||
for func in clas["funcs"].values():
|
||||
remove_keyword(func)
|
||||
for func in funcs.values():
|
||||
remove_keyword(func)
|
||||
|
||||
# ext = ExtractCodeBlocksJava()
|
||||
# ext.extract("codes/java/chapter_array_and_linkedlist/array.java")
|
||||
# ext.extract("codes/java/chapter_array_and_linkedlist/my_list.java")
|
||||
|
|
|
@ -19,6 +19,9 @@ class ExtractCodeBlocksPython:
|
|||
self.block_start_pattern = '^\s{ind}""".+'
|
||||
|
||||
def extract(self, file_path):
|
||||
"""
|
||||
Extract classes and functions from a markdown document
|
||||
"""
|
||||
self.file_path = file_path
|
||||
with open(file_path) as f:
|
||||
self.lines = f.readlines()
|
||||
|
@ -28,6 +31,8 @@ class ExtractCodeBlocksPython:
|
|||
classes = self.extract_class_blocks()
|
||||
funcs = self.extract_function_blocks()
|
||||
|
||||
self.post_process(classes, funcs)
|
||||
|
||||
return {
|
||||
"classes": classes,
|
||||
"funcs": funcs,
|
||||
|
@ -45,21 +50,15 @@ class ExtractCodeBlocksPython:
|
|||
# Search the code
|
||||
for i in range(header_line + 1, len(self.lines)):
|
||||
if re.match(block_end_pattern, self.lines[i]) is not None:
|
||||
end_line = i
|
||||
end_line = i - 1
|
||||
break
|
||||
# Search the header comment
|
||||
for i in range(header_line - 1, -1, -1):
|
||||
if re.search(block_start_pattern, self.lines[i]) is not None:
|
||||
start_line = i
|
||||
break
|
||||
func_block = self.lines[start_line:end_line]
|
||||
# Remove empty lines at bottom
|
||||
for i in range(len(func_block) - 1, -1, -1):
|
||||
if re.search("^\s*\n", func_block[i]) is None:
|
||||
break
|
||||
end_line -= 1
|
||||
|
||||
return start_line, end_line, self.lines[start_line:end_line]
|
||||
return start_line, end_line, self.lines[start_line:end_line + 1]
|
||||
|
||||
|
||||
def extract_function_blocks(self, indentation=0, start_line=-1, end_line=-1):
|
||||
|
@ -130,3 +129,29 @@ class ExtractCodeBlocksPython:
|
|||
}
|
||||
|
||||
return classes
|
||||
|
||||
def post_process(self, classes, funcs):
|
||||
"""
|
||||
Process the classes and functions
|
||||
"""
|
||||
def remove_empty_lines(func):
|
||||
start_line, end_line = func["line_number"]["start"], func["line_number"]["end"]
|
||||
block = func["block"]
|
||||
# Remove empty lines at bottom
|
||||
for i in range(len(block) - 1, -1, -1):
|
||||
if re.search("^\s*\n", block[i]) is None:
|
||||
break
|
||||
end_line -= 1
|
||||
func["line_number"]["end"] = end_line
|
||||
func["block"] = block[:end_line - start_line + 1]
|
||||
|
||||
for clas in classes.values():
|
||||
remove_empty_lines(clas)
|
||||
for func in clas["funcs"].values():
|
||||
remove_empty_lines(func)
|
||||
for func in funcs.values():
|
||||
remove_empty_lines(func)
|
||||
|
||||
|
||||
# ext = ExtractCodeBlocksPython()
|
||||
# ext.extract("codes/python/chapter_array_and_linkedlist/my_list.py")
|
Loading…
Reference in a new issue