mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-22 00:07:13 +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:
@@ -0,0 +1,31 @@
|
||||
// File: list_node.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
// ListNode linked list node
|
||||
type ListNode struct {
|
||||
Next *ListNode
|
||||
Val int
|
||||
}
|
||||
|
||||
// NewListNode linked list node constructor
|
||||
func NewListNode(v int) *ListNode {
|
||||
return &ListNode{
|
||||
Next: nil,
|
||||
Val: v,
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayToLinkedList deserialize array to linked list
|
||||
func ArrayToLinkedList(arr []int) *ListNode {
|
||||
// dummy header of linked list
|
||||
dummy := NewListNode(0)
|
||||
node := dummy
|
||||
for _, val := range arr {
|
||||
node.Next = NewListNode(val)
|
||||
node = node.Next
|
||||
}
|
||||
return dummy.Next
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// File: list_node_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListNode(t *testing.T) {
|
||||
arr := []int{2, 3, 5, 6, 7}
|
||||
head := ArrayToLinkedList(arr)
|
||||
|
||||
PrintLinkedList(head)
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// File: print_utils.go
|
||||
// Created Time: 2022-12-03
|
||||
// Author: Reanon (793584285@qq.com), krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PrintSlice print slice
|
||||
func PrintSlice[T any](nums []T) {
|
||||
fmt.Printf("%v", nums)
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// PrintList print list
|
||||
func PrintList(list *list.List) {
|
||||
if list.Len() == 0 {
|
||||
fmt.Print("[]\n")
|
||||
return
|
||||
}
|
||||
e := list.Front()
|
||||
// Force conversion to string will affect efficiency
|
||||
fmt.Print("[")
|
||||
for e.Next() != nil {
|
||||
fmt.Print(e.Value, " ")
|
||||
e = e.Next()
|
||||
}
|
||||
fmt.Print(e.Value, "]\n")
|
||||
}
|
||||
|
||||
// PrintMap print hash table
|
||||
func PrintMap[K comparable, V any](m map[K]V) {
|
||||
for key, value := range m {
|
||||
fmt.Println(key, "->", value)
|
||||
}
|
||||
}
|
||||
|
||||
// PrintHeap print heap
|
||||
func PrintHeap(h []any) {
|
||||
fmt.Printf("Heap array representation:")
|
||||
fmt.Printf("%v", h)
|
||||
fmt.Printf("\nTree representation of heap:\n")
|
||||
root := SliceToTree(h)
|
||||
PrintTree(root)
|
||||
}
|
||||
|
||||
// PrintLinkedList print linked list
|
||||
func PrintLinkedList(node *ListNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
var builder strings.Builder
|
||||
for node.Next != nil {
|
||||
builder.WriteString(strconv.Itoa(node.Val) + " -> ")
|
||||
node = node.Next
|
||||
}
|
||||
builder.WriteString(strconv.Itoa(node.Val))
|
||||
fmt.Println(builder.String())
|
||||
}
|
||||
|
||||
// PrintTree print binary tree
|
||||
func PrintTree(root *TreeNode) {
|
||||
printTreeHelper(root, nil, false)
|
||||
}
|
||||
|
||||
// printTreeHelper print binary tree
|
||||
// This tree printer is borrowed from TECHIE DELIGHT
|
||||
// https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
func printTreeHelper(root *TreeNode, prev *trunk, isRight bool) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
prevStr := " "
|
||||
trunk := newTrunk(prev, prevStr)
|
||||
printTreeHelper(root.Right, trunk, true)
|
||||
if prev == nil {
|
||||
trunk.str = "———"
|
||||
} else if isRight {
|
||||
trunk.str = "/———"
|
||||
prevStr = " |"
|
||||
} else {
|
||||
trunk.str = "\\———"
|
||||
prev.str = prevStr
|
||||
}
|
||||
showTrunk(trunk)
|
||||
fmt.Println(root.Val)
|
||||
if prev != nil {
|
||||
prev.str = prevStr
|
||||
}
|
||||
trunk.str = " |"
|
||||
printTreeHelper(root.Left, trunk, false)
|
||||
}
|
||||
|
||||
type trunk struct {
|
||||
prev *trunk
|
||||
str string
|
||||
}
|
||||
|
||||
func newTrunk(prev *trunk, str string) *trunk {
|
||||
return &trunk{
|
||||
prev: prev,
|
||||
str: str,
|
||||
}
|
||||
}
|
||||
|
||||
func showTrunk(t *trunk) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
|
||||
showTrunk(t.prev)
|
||||
fmt.Print(t.str)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// File: tree_node.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
// TreeNode binary tree node
|
||||
type TreeNode struct {
|
||||
Val any // Node value
|
||||
Height int // Node height
|
||||
Left *TreeNode // Reference to left child node
|
||||
Right *TreeNode // Reference to right child node
|
||||
}
|
||||
|
||||
// NewTreeNode binary tree node constructor
|
||||
func NewTreeNode(v any) *TreeNode {
|
||||
return &TreeNode{
|
||||
Val: v,
|
||||
Height: 0,
|
||||
Left: nil,
|
||||
Right: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// For the serialization encoding rules, please refer to:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// Array representation of binary tree:
|
||||
// [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
// Linked list representation of binary tree:
|
||||
//
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
// | \——— 6
|
||||
// | \——— 12
|
||||
//
|
||||
// ——— 1
|
||||
//
|
||||
// \——— 2
|
||||
// | /——— 9
|
||||
// \——— 4
|
||||
// \——— 8
|
||||
|
||||
// SliceToTreeDFS deserialize list to binary tree: recursion
|
||||
func SliceToTreeDFS(arr []any, i int) *TreeNode {
|
||||
if i < 0 || i >= len(arr) || arr[i] == nil {
|
||||
return nil
|
||||
}
|
||||
root := NewTreeNode(arr[i])
|
||||
root.Left = SliceToTreeDFS(arr, 2*i+1)
|
||||
root.Right = SliceToTreeDFS(arr, 2*i+2)
|
||||
return root
|
||||
}
|
||||
|
||||
// SliceToTree deserialize slice to binary tree
|
||||
func SliceToTree(arr []any) *TreeNode {
|
||||
return SliceToTreeDFS(arr, 0)
|
||||
}
|
||||
|
||||
// TreeToSliceDFS serialize binary tree to slice: recursion
|
||||
func TreeToSliceDFS(root *TreeNode, i int, res *[]any) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
for i >= len(*res) {
|
||||
*res = append(*res, nil)
|
||||
}
|
||||
(*res)[i] = root.Val
|
||||
TreeToSliceDFS(root.Left, 2*i+1, res)
|
||||
TreeToSliceDFS(root.Right, 2*i+2, res)
|
||||
}
|
||||
|
||||
// TreeToSlice serialize binary tree to slice
|
||||
func TreeToSlice(root *TreeNode) []any {
|
||||
var res []any
|
||||
TreeToSliceDFS(root, 0, &res)
|
||||
return res
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// File: tree_node_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTreeNode(t *testing.T) {
|
||||
arr := []any{1, 2, 3, nil, 5, 6, nil}
|
||||
node := SliceToTree(arr)
|
||||
|
||||
// print tree
|
||||
PrintTree(node)
|
||||
|
||||
// tree to arr
|
||||
fmt.Println(TreeToSlice(node))
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// File: vertex.go
|
||||
// Created Time: 2023-02-18
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
// Vertex vertex class
|
||||
type Vertex struct {
|
||||
Val int
|
||||
}
|
||||
|
||||
// NewVertex vertex constructor
|
||||
func NewVertex(val int) Vertex {
|
||||
return Vertex{
|
||||
Val: val,
|
||||
}
|
||||
}
|
||||
|
||||
// ValsToVets deserialize value list to vertex list
|
||||
func ValsToVets(vals []int) []Vertex {
|
||||
vets := make([]Vertex, len(vals))
|
||||
for i := 0; i < len(vals); i++ {
|
||||
vets[i] = NewVertex(vals[i])
|
||||
}
|
||||
return vets
|
||||
}
|
||||
|
||||
// VetsToVals serialize vertex list to value list
|
||||
func VetsToVals(vets []Vertex) []int {
|
||||
vals := make([]int, len(vets))
|
||||
for i := range vets {
|
||||
vals[i] = vets[i].Val
|
||||
}
|
||||
return vals
|
||||
}
|
||||
|
||||
// DeleteSliceElms delete specified elements from slice
|
||||
func DeleteSliceElms[T any](a []T, elms ...T) []T {
|
||||
if len(a) == 0 || len(elms) == 0 {
|
||||
return a
|
||||
}
|
||||
// First convert elements to set
|
||||
m := make(map[any]struct{})
|
||||
for _, v := range elms {
|
||||
m[v] = struct{}{}
|
||||
}
|
||||
// Filter out specified elements
|
||||
res := make([]T, 0, len(a))
|
||||
for _, v := range a {
|
||||
if _, ok := m[v]; !ok {
|
||||
res = append(res, v)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user