mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-27 02:27:12 +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,110 @@
|
||||
/**
|
||||
* File: array_hash_map.swift
|
||||
* Created Time: 2023-01-16
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Hash table based on array implementation */
|
||||
class ArrayHashMap {
|
||||
private var buckets: [Pair?]
|
||||
|
||||
init() {
|
||||
// Initialize array with 100 buckets
|
||||
buckets = Array(repeating: nil, count: 100)
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
private func hashFunc(key: Int) -> Int {
|
||||
let index = key % 100
|
||||
return index
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
func get(key: Int) -> String? {
|
||||
let index = hashFunc(key: key)
|
||||
let pair = buckets[index]
|
||||
return pair?.val
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
func put(key: Int, val: String) {
|
||||
let pair = Pair(key: key, val: val)
|
||||
let index = hashFunc(key: key)
|
||||
buckets[index] = pair
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
func remove(key: Int) {
|
||||
let index = hashFunc(key: key)
|
||||
// Set to nil to delete
|
||||
buckets[index] = nil
|
||||
}
|
||||
|
||||
/* Get all key-value pairs */
|
||||
func pairSet() -> [Pair] {
|
||||
buckets.compactMap { $0 }
|
||||
}
|
||||
|
||||
/* Get all keys */
|
||||
func keySet() -> [Int] {
|
||||
buckets.compactMap { $0?.key }
|
||||
}
|
||||
|
||||
/* Get all values */
|
||||
func valueSet() -> [String] {
|
||||
buckets.compactMap { $0?.val }
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
func print() {
|
||||
for pair in pairSet() {
|
||||
Swift.print("\(pair.key) -> \(pair.val)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _ArrayHashMap {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize hash table */
|
||||
let map = ArrayHashMap()
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(key: 12836, val: "Xiao Ha")
|
||||
map.put(key: 15937, val: "Xiao Luo")
|
||||
map.put(key: 16750, val: "Xiao Suan")
|
||||
map.put(key: 13276, val: "Xiao Fa")
|
||||
map.put(key: 10583, val: "Xiao Ya")
|
||||
print("\nAfter adding is complete, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
let name = map.get(key: 15937)!
|
||||
print("\nInput student ID 15937, found name \(name)")
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(key: 10583)
|
||||
print("\nAfter removing 10583, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
|
||||
/* Traverse hash table */
|
||||
print("\nTraverse key-value pairs Key->Value")
|
||||
for pair in map.pairSet() {
|
||||
print("\(pair.key) -> \(pair.val)")
|
||||
}
|
||||
print("\nTraverse keys only Key")
|
||||
for key in map.keySet() {
|
||||
print(key)
|
||||
}
|
||||
print("\nTraverse values only Value")
|
||||
for val in map.valueSet() {
|
||||
print(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* File: built_in_hash.swift
|
||||
* Created Time: 2023-07-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
@main
|
||||
enum BuiltInHash {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let num = 3
|
||||
let hashNum = num.hashValue
|
||||
print("Hash value of integer \(num) is \(hashNum)")
|
||||
|
||||
let bol = true
|
||||
let hashBol = bol.hashValue
|
||||
print("Hash value of boolean \(bol) is \(hashBol)")
|
||||
|
||||
let dec = 3.14159
|
||||
let hashDec = dec.hashValue
|
||||
print("Hash value of decimal \(dec) is \(hashDec)")
|
||||
|
||||
let str = "Hello Algo"
|
||||
let hashStr = str.hashValue
|
||||
print("Hash value of string \(str) is \(hashStr)")
|
||||
|
||||
let arr = [AnyHashable(12836), AnyHashable("Xiao Ha")]
|
||||
let hashTup = arr.hashValue
|
||||
print("Hash value of array \(arr) is \(hashTup)")
|
||||
|
||||
let obj = ListNode(x: 0)
|
||||
let hashObj = obj.hashValue
|
||||
print("Hash value of node object \(obj) is \(hashObj)")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* File: hash_map.swift
|
||||
* Created Time: 2023-01-16
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
@main
|
||||
enum HashMap {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize hash table */
|
||||
var map: [Int: String] = [:]
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map[12836] = "Xiao Ha"
|
||||
map[15937] = "Xiao Luo"
|
||||
map[16750] = "Xiao Suan"
|
||||
map[13276] = "Xiao Fa"
|
||||
map[10583] = "Xiao Ya"
|
||||
print("\nAfter adding is complete, hash table is\nKey -> Value")
|
||||
PrintUtil.printHashMap(map: map)
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
let name = map[15937]!
|
||||
print("\nInput student ID 15937, found name \(name)")
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.removeValue(forKey: 10583)
|
||||
print("\nAfter removing 10583, hash table is\nKey -> Value")
|
||||
PrintUtil.printHashMap(map: map)
|
||||
|
||||
/* Traverse hash table */
|
||||
print("\nTraverse key-value pairs Key->Value")
|
||||
for (key, value) in map {
|
||||
print("\(key) -> \(value)")
|
||||
}
|
||||
print("\nTraverse keys only Key")
|
||||
for key in map.keys {
|
||||
print(key)
|
||||
}
|
||||
print("\nTraverse values only Value")
|
||||
for value in map.values {
|
||||
print(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* File: hash_map_chaining.swift
|
||||
* Created Time: 2023-06-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Hash table with separate chaining */
|
||||
class HashMapChaining {
|
||||
var size: Int // Number of key-value pairs
|
||||
var capacity: Int // Hash table capacity
|
||||
var loadThres: Double // Load factor threshold for triggering expansion
|
||||
var extendRatio: Int // Expansion multiplier
|
||||
var buckets: [[Pair]] // Bucket array
|
||||
|
||||
/* Constructor */
|
||||
init() {
|
||||
size = 0
|
||||
capacity = 4
|
||||
loadThres = 2.0 / 3.0
|
||||
extendRatio = 2
|
||||
buckets = Array(repeating: [], count: capacity)
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
func hashFunc(key: Int) -> Int {
|
||||
key % capacity
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
func loadFactor() -> Double {
|
||||
Double(size) / Double(capacity)
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
func get(key: Int) -> String? {
|
||||
let index = hashFunc(key: key)
|
||||
let bucket = buckets[index]
|
||||
// Traverse bucket, if key is found, return corresponding val
|
||||
for pair in bucket {
|
||||
if pair.key == key {
|
||||
return pair.val
|
||||
}
|
||||
}
|
||||
// Return nil if key not found
|
||||
return nil
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
func put(key: Int, val: String) {
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if loadFactor() > loadThres {
|
||||
extend()
|
||||
}
|
||||
let index = hashFunc(key: key)
|
||||
let bucket = buckets[index]
|
||||
// Traverse bucket, if specified key is encountered, update corresponding val and return
|
||||
for pair in bucket {
|
||||
if pair.key == key {
|
||||
pair.val = val
|
||||
return
|
||||
}
|
||||
}
|
||||
// If key does not exist, append key-value pair to the end
|
||||
let pair = Pair(key: key, val: val)
|
||||
buckets[index].append(pair)
|
||||
size += 1
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
func remove(key: Int) {
|
||||
let index = hashFunc(key: key)
|
||||
let bucket = buckets[index]
|
||||
// Traverse bucket and remove key-value pair from it
|
||||
for (pairIndex, pair) in bucket.enumerated() {
|
||||
if pair.key == key {
|
||||
buckets[index].remove(at: pairIndex)
|
||||
size -= 1
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Expand hash table */
|
||||
func extend() {
|
||||
// Temporarily store the original hash table
|
||||
let bucketsTmp = buckets
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio
|
||||
buckets = Array(repeating: [], count: capacity)
|
||||
size = 0
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for bucket in bucketsTmp {
|
||||
for pair in bucket {
|
||||
put(key: pair.key, val: pair.val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
func print() {
|
||||
for bucket in buckets {
|
||||
let res = bucket.map { "\($0.key) -> \($0.val)" }
|
||||
Swift.print(res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _HashMapChaining {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize hash table */
|
||||
let map = HashMapChaining()
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(key: 12836, val: "Xiao Ha")
|
||||
map.put(key: 15937, val: "Xiao Luo")
|
||||
map.put(key: 16750, val: "Xiao Suan")
|
||||
map.put(key: 13276, val: "Xiao Fa")
|
||||
map.put(key: 10583, val: "Xiao Ya")
|
||||
print("\nAfter adding is complete, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
let name = map.get(key: 13276)
|
||||
print("\nInput student ID 13276, found name \(name!)")
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(key: 12836)
|
||||
print("\nAfter removing 12836, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.swift
|
||||
* Created Time: 2023-06-28
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
import utils
|
||||
|
||||
/* Hash table with open addressing */
|
||||
class HashMapOpenAddressing {
|
||||
var size: Int // Number of key-value pairs
|
||||
var capacity: Int // Hash table capacity
|
||||
var loadThres: Double // Load factor threshold for triggering expansion
|
||||
var extendRatio: Int // Expansion multiplier
|
||||
var buckets: [Pair?] // Bucket array
|
||||
var TOMBSTONE: Pair // Removal marker
|
||||
|
||||
/* Constructor */
|
||||
init() {
|
||||
size = 0
|
||||
capacity = 4
|
||||
loadThres = 2.0 / 3.0
|
||||
extendRatio = 2
|
||||
buckets = Array(repeating: nil, count: capacity)
|
||||
TOMBSTONE = Pair(key: -1, val: "-1")
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
func hashFunc(key: Int) -> Int {
|
||||
key % capacity
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
func loadFactor() -> Double {
|
||||
Double(size) / Double(capacity)
|
||||
}
|
||||
|
||||
/* Search for bucket index corresponding to key */
|
||||
func findBucket(key: Int) -> Int {
|
||||
var index = hashFunc(key: key)
|
||||
var firstTombstone = -1
|
||||
// Linear probing, break when encountering an empty bucket
|
||||
while buckets[index] != nil {
|
||||
// If key is encountered, return the corresponding bucket index
|
||||
if buckets[index]!.key == key {
|
||||
// If a removal marker was encountered before, move the key-value pair to that index
|
||||
if firstTombstone != -1 {
|
||||
buckets[firstTombstone] = buckets[index]
|
||||
buckets[index] = TOMBSTONE
|
||||
return firstTombstone // Return the moved bucket index
|
||||
}
|
||||
return index // Return bucket index
|
||||
}
|
||||
// Record the first removal marker encountered
|
||||
if firstTombstone == -1 && buckets[index] == TOMBSTONE {
|
||||
firstTombstone = index
|
||||
}
|
||||
// Calculate bucket index, wrap around to the head if past the tail
|
||||
index = (index + 1) % capacity
|
||||
}
|
||||
// If key does not exist, return the index for insertion
|
||||
return firstTombstone == -1 ? index : firstTombstone
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
func get(key: Int) -> String? {
|
||||
// Search for bucket index corresponding to key
|
||||
let index = findBucket(key: key)
|
||||
// If key-value pair is found, return corresponding val
|
||||
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||
return buckets[index]!.val
|
||||
}
|
||||
// If key-value pair does not exist, return null
|
||||
return nil
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
func put(key: Int, val: String) {
|
||||
// When load factor exceeds threshold, perform expansion
|
||||
if loadFactor() > loadThres {
|
||||
extend()
|
||||
}
|
||||
// Search for bucket index corresponding to key
|
||||
let index = findBucket(key: key)
|
||||
// If key-value pair is found, overwrite val and return
|
||||
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||
buckets[index]!.val = val
|
||||
return
|
||||
}
|
||||
// If key-value pair does not exist, add the key-value pair
|
||||
buckets[index] = Pair(key: key, val: val)
|
||||
size += 1
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
func remove(key: Int) {
|
||||
// Search for bucket index corresponding to key
|
||||
let index = findBucket(key: key)
|
||||
// If key-value pair is found, overwrite it with removal marker
|
||||
if buckets[index] != nil, buckets[index] != TOMBSTONE {
|
||||
buckets[index] = TOMBSTONE
|
||||
size -= 1
|
||||
}
|
||||
}
|
||||
|
||||
/* Expand hash table */
|
||||
func extend() {
|
||||
// Temporarily store the original hash table
|
||||
let bucketsTmp = buckets
|
||||
// Initialize expanded new hash table
|
||||
capacity *= extendRatio
|
||||
buckets = Array(repeating: nil, count: capacity)
|
||||
size = 0
|
||||
// Move key-value pairs from original hash table to new hash table
|
||||
for pair in bucketsTmp {
|
||||
if let pair, pair != TOMBSTONE {
|
||||
put(key: pair.key, val: pair.val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
func print() {
|
||||
for pair in buckets {
|
||||
if pair == nil {
|
||||
Swift.print("null")
|
||||
} else if pair == TOMBSTONE {
|
||||
Swift.print("TOMBSTONE")
|
||||
} else {
|
||||
Swift.print("\(pair!.key) -> \(pair!.val)")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
enum _HashMapOpenAddressing {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
/* Initialize hash table */
|
||||
let map = HashMapOpenAddressing()
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(key: 12836, val: "Xiao Ha")
|
||||
map.put(key: 15937, val: "Xiao Luo")
|
||||
map.put(key: 16750, val: "Xiao Suan")
|
||||
map.put(key: 13276, val: "Xiao Fa")
|
||||
map.put(key: 10583, val: "Xiao Ya")
|
||||
print("\nAfter adding is complete, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
|
||||
/* Query operation */
|
||||
// Input key into hash table to get value
|
||||
let name = map.get(key: 13276)
|
||||
print("\nInput student ID 13276, found name \(name!)")
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from hash table
|
||||
map.remove(key: 16750)
|
||||
print("\nAfter removing 16750, hash table is\nKey -> Value")
|
||||
map.print()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* File: simple_hash.swift
|
||||
* Created Time: 2023-07-01
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* Additive hash */
|
||||
func addHash(key: String) -> Int {
|
||||
var hash = 0
|
||||
let MODULUS = 1_000_000_007
|
||||
for c in key {
|
||||
for scalar in c.unicodeScalars {
|
||||
hash = (hash + Int(scalar.value)) % MODULUS
|
||||
}
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
/* Multiplicative hash */
|
||||
func mulHash(key: String) -> Int {
|
||||
var hash = 0
|
||||
let MODULUS = 1_000_000_007
|
||||
for c in key {
|
||||
for scalar in c.unicodeScalars {
|
||||
hash = (31 * hash + Int(scalar.value)) % MODULUS
|
||||
}
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
/* XOR hash */
|
||||
func xorHash(key: String) -> Int {
|
||||
var hash = 0
|
||||
let MODULUS = 1_000_000_007
|
||||
for c in key {
|
||||
for scalar in c.unicodeScalars {
|
||||
hash ^= Int(scalar.value)
|
||||
}
|
||||
}
|
||||
return hash & MODULUS
|
||||
}
|
||||
|
||||
/* Rotational hash */
|
||||
func rotHash(key: String) -> Int {
|
||||
var hash = 0
|
||||
let MODULUS = 1_000_000_007
|
||||
for c in key {
|
||||
for scalar in c.unicodeScalars {
|
||||
hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS
|
||||
}
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
@main
|
||||
enum SimpleHash {
|
||||
/* Driver Code */
|
||||
static func main() {
|
||||
let key = "Hello Algo"
|
||||
|
||||
var hash = addHash(key: key)
|
||||
print("Additive hash value is \(hash)")
|
||||
|
||||
hash = mulHash(key: key)
|
||||
print("Multiplicative hash value is \(hash)")
|
||||
|
||||
hash = xorHash(key: key)
|
||||
print("XOR hash value is \(hash)")
|
||||
|
||||
hash = rotHash(key: key)
|
||||
print("Rotational hash value is \(hash)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user