mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-26 04:56: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:
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* File: array.dart
|
||||
* Created Time: 2023-01-20
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// ignore_for_file: unused_local_variable
|
||||
|
||||
import 'dart:math';
|
||||
|
||||
/* Random access to element */
|
||||
int randomAccess(List<int> nums) {
|
||||
// Randomly select a number in the interval [0, nums.length)
|
||||
int randomIndex = Random().nextInt(nums.length);
|
||||
// Retrieve and return the random element
|
||||
int randomNum = nums[randomIndex];
|
||||
return randomNum;
|
||||
}
|
||||
|
||||
/* Extend array length */
|
||||
List<int> extend(List<int> nums, int enlarge) {
|
||||
// Initialize an array with extended length
|
||||
List<int> res = List.filled(nums.length + enlarge, 0);
|
||||
// Copy all elements from the original array to the new array
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
// Return the extended new array
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Insert element _num at array index index */
|
||||
void insert(List<int> nums, int _num, int index) {
|
||||
// Move all elements at and after index index backward by one position
|
||||
for (var i = nums.length - 1; i > index; i--) {
|
||||
nums[i] = nums[i - 1];
|
||||
}
|
||||
// Assign _num to element at index
|
||||
nums[index] = _num;
|
||||
}
|
||||
|
||||
/* Remove the element at index index */
|
||||
void remove(List<int> nums, int index) {
|
||||
// Move all elements after index index forward by one position
|
||||
for (var i = index; i < nums.length - 1; i++) {
|
||||
nums[i] = nums[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* Traverse array elements */
|
||||
void traverse(List<int> nums) {
|
||||
int count = 0;
|
||||
// Traverse array by index
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
// Direct traversal of array elements
|
||||
for (int _num in nums) {
|
||||
count += _num;
|
||||
}
|
||||
// Traverse array using forEach method
|
||||
nums.forEach((_num) {
|
||||
count += _num;
|
||||
});
|
||||
}
|
||||
|
||||
/* Find the specified element in the array */
|
||||
int find(List<int> nums, int target) {
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == target) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Initialize array */
|
||||
var arr = List.filled(5, 0);
|
||||
print('Array arr = $arr');
|
||||
List<int> nums = [1, 3, 2, 5, 4];
|
||||
print('Array nums = $nums');
|
||||
|
||||
/* Insert element */
|
||||
int randomNum = randomAccess(nums);
|
||||
print('Get random element $randomNum from nums');
|
||||
|
||||
/* Traverse array */
|
||||
nums = extend(nums, 3);
|
||||
print('Extend array length to 8, get nums = $nums');
|
||||
|
||||
/* Insert element */
|
||||
insert(nums, 6, 3);
|
||||
print("Insert number 6 at index 3, get nums = $nums");
|
||||
|
||||
/* Remove element */
|
||||
remove(nums, 2);
|
||||
print("Delete element at index 2, get nums = $nums");
|
||||
|
||||
/* Traverse array */
|
||||
traverse(nums);
|
||||
|
||||
/* Find element */
|
||||
int index = find(nums, 3);
|
||||
print("Find element 3 in nums, index = $index");
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* File: linked_list.dart
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import '../utils/list_node.dart';
|
||||
import '../utils/print_util.dart';
|
||||
|
||||
/* Insert node P after node n0 in the linked list */
|
||||
void insert(ListNode n0, ListNode P) {
|
||||
ListNode? n1 = n0.next;
|
||||
P.next = n1;
|
||||
n0.next = P;
|
||||
}
|
||||
|
||||
/* Remove the first node after node n0 in the linked list */
|
||||
void remove(ListNode n0) {
|
||||
if (n0.next == null) return;
|
||||
// n0 -> P -> n1
|
||||
ListNode P = n0.next!;
|
||||
ListNode? n1 = P.next;
|
||||
n0.next = n1;
|
||||
}
|
||||
|
||||
/* Access the node at index index in the linked list */
|
||||
ListNode? access(ListNode? head, int index) {
|
||||
for (var i = 0; i < index; i++) {
|
||||
if (head == null) return null;
|
||||
head = head.next;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/* Find the first node with value target in the linked list */
|
||||
int find(ListNode? head, int target) {
|
||||
int index = 0;
|
||||
while (head != null) {
|
||||
if (head.val == target) {
|
||||
return index;
|
||||
}
|
||||
head = head.next;
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
// Initialize linked list
|
||||
// Initialize each node
|
||||
ListNode n0 = ListNode(1);
|
||||
ListNode n1 = ListNode(3);
|
||||
ListNode n2 = ListNode(2);
|
||||
ListNode n3 = ListNode(5);
|
||||
ListNode n4 = ListNode(4);
|
||||
// Build references between nodes
|
||||
n0.next = n1;
|
||||
n1.next = n2;
|
||||
n2.next = n3;
|
||||
n3.next = n4;
|
||||
|
||||
print('Initialized linked list is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Insert node */
|
||||
insert(n0, ListNode(0));
|
||||
print('Linked list after inserting node is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Remove node */
|
||||
remove(n0);
|
||||
print('Linked list after removing node is');
|
||||
printLinkedList(n0);
|
||||
|
||||
/* Access node */
|
||||
ListNode? node = access(n0, 3);
|
||||
print('Value of node at index 3 in linked list = ${node!.val}');
|
||||
|
||||
/* Search node */
|
||||
int index = find(n0, 2);
|
||||
print('Index of node with value 2 in linked list = $index');
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* File: list.dart
|
||||
* Created Time: 2023-01-24
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// ignore_for_file: unused_local_variable
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Initialize list */
|
||||
List<int> nums = [1, 3, 2, 5, 4];
|
||||
print('List nums = $nums');
|
||||
|
||||
/* Update element */
|
||||
int _num = nums[1];
|
||||
print('Access element at index 1, get _num = $_num');
|
||||
|
||||
/* Add elements at the end */
|
||||
nums[1] = 0;
|
||||
print('Update element at index 1 to 0, get nums = $nums');
|
||||
|
||||
/* Remove element */
|
||||
nums.clear();
|
||||
print('After clearing list, nums = $nums');
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
print('After adding elements, nums = $nums');
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(3, 6);
|
||||
print('Insert number 6 at index 3, get nums = $nums');
|
||||
|
||||
/* Remove element */
|
||||
nums.removeAt(3);
|
||||
print('Delete element at index 3, get nums = $nums');
|
||||
|
||||
/* Traverse list by index */
|
||||
int count = 0;
|
||||
for (var i = 0; i < nums.length; i++) {
|
||||
count += nums[i];
|
||||
}
|
||||
/* Directly traverse list elements */
|
||||
count = 0;
|
||||
for (var x in nums) {
|
||||
count += x;
|
||||
}
|
||||
|
||||
/* Concatenate two lists */
|
||||
List<int> nums1 = [6, 8, 7, 10, 9];
|
||||
nums.addAll(nums1);
|
||||
print('After concatenating list nums1 to nums, get nums = $nums');
|
||||
|
||||
/* Sort list */
|
||||
nums.sort();
|
||||
print('After sorting list, nums = $nums');
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* File: my_list.dart
|
||||
* Created Time: 2023-02-05
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
/* List class */
|
||||
class MyList {
|
||||
late List<int> _arr; // Array (stores list elements)
|
||||
int _capacity = 10; // List capacity
|
||||
int _size = 0; // List length (current number of elements)
|
||||
int _extendRatio = 2; // Multiple by which the list capacity is extended each time
|
||||
|
||||
/* Constructor */
|
||||
MyList() {
|
||||
_arr = List.filled(_capacity, 0);
|
||||
}
|
||||
|
||||
/* Get list length (current number of elements) */
|
||||
int size() => _size;
|
||||
|
||||
/* Get list capacity */
|
||||
int capacity() => _capacity;
|
||||
|
||||
/* Update element */
|
||||
int get(int index) {
|
||||
if (index >= _size) throw RangeError('Index out of bounds');
|
||||
return _arr[index];
|
||||
}
|
||||
|
||||
/* Add elements at the end */
|
||||
void set(int index, int _num) {
|
||||
if (index >= _size) throw RangeError('Index out of bounds');
|
||||
_arr[index] = _num;
|
||||
}
|
||||
|
||||
/* Direct traversal of list elements */
|
||||
void add(int _num) {
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (_size == _capacity) extendCapacity();
|
||||
_arr[_size] = _num;
|
||||
// Update the number of elements
|
||||
_size++;
|
||||
}
|
||||
|
||||
/* Sort list */
|
||||
void insert(int index, int _num) {
|
||||
if (index >= _size) throw RangeError('Index out of bounds');
|
||||
// When the number of elements exceeds capacity, trigger the extension mechanism
|
||||
if (_size == _capacity) extendCapacity();
|
||||
// Move all elements after index index forward by one position
|
||||
for (var j = _size - 1; j >= index; j--) {
|
||||
_arr[j + 1] = _arr[j];
|
||||
}
|
||||
_arr[index] = _num;
|
||||
// Update the number of elements
|
||||
_size++;
|
||||
}
|
||||
|
||||
/* Remove element */
|
||||
int remove(int index) {
|
||||
if (index >= _size) throw RangeError('Index out of bounds');
|
||||
int _num = _arr[index];
|
||||
// Move all elements after index forward by one position
|
||||
for (var j = index; j < _size - 1; j++) {
|
||||
_arr[j] = _arr[j + 1];
|
||||
}
|
||||
// Update the number of elements
|
||||
_size--;
|
||||
// Return the removed element
|
||||
return _num;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void extendCapacity() {
|
||||
// Create new array with length _extendRatio times original array
|
||||
final _newNums = List.filled(_capacity * _extendRatio, 0);
|
||||
// Copy original array to new array
|
||||
List.copyRange(_newNums, 0, _arr);
|
||||
// Update _arr reference
|
||||
_arr = _newNums;
|
||||
// Add elements at the end
|
||||
_capacity = _arr.length;
|
||||
}
|
||||
|
||||
/* Convert list to array */
|
||||
List<int> toArray() {
|
||||
List<int> arr = [];
|
||||
for (var i = 0; i < _size; i++) {
|
||||
arr.add(get(i));
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
void main() {
|
||||
/* Initialize list */
|
||||
MyList nums = MyList();
|
||||
/* Direct traversal of list elements */
|
||||
nums.add(1);
|
||||
nums.add(3);
|
||||
nums.add(2);
|
||||
nums.add(5);
|
||||
nums.add(4);
|
||||
print(
|
||||
'List nums = ${nums.toArray()}, capacity = ${nums.capacity()}, length = ${nums.size()}');
|
||||
|
||||
/* Sort list */
|
||||
nums.insert(3, 6);
|
||||
print('Insert number 6 at index 3, get nums = ${nums.toArray()}');
|
||||
|
||||
/* Remove element */
|
||||
nums.remove(3);
|
||||
print('Delete element at index 3, get nums = ${nums.toArray()}');
|
||||
|
||||
/* Update element */
|
||||
int _num = nums.get(1);
|
||||
print('Access element at index 1, get _num = $_num');
|
||||
|
||||
/* Add elements at the end */
|
||||
nums.set(1, 0);
|
||||
print('Update element at index 1 to 0, get nums = ${nums.toArray()}');
|
||||
|
||||
/* Test capacity expansion mechanism */
|
||||
for (var i = 0; i < 10; i++) {
|
||||
// At i = 5, the list length will exceed the list capacity, triggering the expansion mechanism
|
||||
nums.add(i);
|
||||
}
|
||||
print(
|
||||
'After expansion, list nums = ${nums.toArray()}, capacity = ${nums.capacity()}, length = ${nums.size()}');
|
||||
}
|
||||
Reference in New Issue
Block a user