mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
141
en/codes/java/chapter_hashing/array_hash_map.java
Normal file
141
en/codes/java/chapter_hashing/array_hash_map.java
Normal file
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: array_hash_map.java
|
||||
* Created Time: 2022-12-04
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/* Key-value pair */
|
||||
class Pair {
|
||||
public int key;
|
||||
public String val;
|
||||
|
||||
public Pair(int key, String val) {
|
||||
this.key = key;
|
||||
this.val = val;
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash table based on array implementation */
|
||||
class ArrayHashMap {
|
||||
private List<Pair> buckets;
|
||||
|
||||
public ArrayHashMap() {
|
||||
// Initialize an array, containing 100 buckets
|
||||
buckets = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
buckets.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
private int hashFunc(int key) {
|
||||
int index = key % 100;
|
||||
return index;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
public String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Pair pair = buckets.get(index);
|
||||
if (pair == null)
|
||||
return null;
|
||||
return pair.val;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
public void put(int key, String val) {
|
||||
Pair pair = new Pair(key, val);
|
||||
int index = hashFunc(key);
|
||||
buckets.set(index, pair);
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
public void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// Set to null, indicating removal
|
||||
buckets.set(index, null);
|
||||
}
|
||||
|
||||
/* Get all key-value pairs */
|
||||
public List<Pair> pairSet() {
|
||||
List<Pair> pairSet = new ArrayList<>();
|
||||
for (Pair pair : buckets) {
|
||||
if (pair != null)
|
||||
pairSet.add(pair);
|
||||
}
|
||||
return pairSet;
|
||||
}
|
||||
|
||||
/* Get all keys */
|
||||
public List<Integer> keySet() {
|
||||
List<Integer> keySet = new ArrayList<>();
|
||||
for (Pair pair : buckets) {
|
||||
if (pair != null)
|
||||
keySet.add(pair.key);
|
||||
}
|
||||
return keySet;
|
||||
}
|
||||
|
||||
/* Get all values */
|
||||
public List<String> valueSet() {
|
||||
List<String> valueSet = new ArrayList<>();
|
||||
for (Pair pair : buckets) {
|
||||
if (pair != null)
|
||||
valueSet.add(pair.val);
|
||||
}
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
public void print() {
|
||||
for (Pair kv : pairSet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class array_hash_map {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize hash table */
|
||||
ArrayHashMap map = new ArrayHashMap();
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\nEnter student ID 15937, found name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
map.remove(10583);
|
||||
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Traverse hash table */
|
||||
System.out.println("\nTraverse key-value pairs Key->Value");
|
||||
for (Pair kv : map.pairSet()) {
|
||||
System.out.println(kv.key + " -> " + kv.val);
|
||||
}
|
||||
System.out.println("\nIndividually traverse keys Key");
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\nIndividually traverse values Value");
|
||||
for (String val : map.valueSet()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
38
en/codes/java/chapter_hashing/built_in_hash.java
Normal file
38
en/codes/java/chapter_hashing/built_in_hash.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: built_in_hash.java
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class built_in_hash {
|
||||
public static void main(String[] args) {
|
||||
int num = 3;
|
||||
int hashNum = Integer.hashCode(num);
|
||||
System.out.println("The hash value of integer " + num + " is " + hashNum);
|
||||
|
||||
boolean bol = true;
|
||||
int hashBol = Boolean.hashCode(bol);
|
||||
System.out.println("The hash value of boolean " + bol + " is " + hashBol);
|
||||
|
||||
double dec = 3.14159;
|
||||
int hashDec = Double.hashCode(dec);
|
||||
System.out.println("The hash value of decimal " + dec + " is " + hashDec);
|
||||
|
||||
String str = "Hello algorithm";
|
||||
int hashStr = str.hashCode();
|
||||
System.out.println("The hash value of string " + str + " is " + hashStr);
|
||||
|
||||
Object[] arr = { 12836, "Ha" };
|
||||
int hashTup = Arrays.hashCode(arr);
|
||||
System.out.println("The hash value of array " + Arrays.toString(arr) + " is " + hashTup);
|
||||
|
||||
ListNode obj = new ListNode(0);
|
||||
int hashObj = obj.hashCode();
|
||||
System.out.println("The hash value of node object " + obj + " is " + hashObj);
|
||||
}
|
||||
}
|
||||
52
en/codes/java/chapter_hashing/hash_map.java
Normal file
52
en/codes/java/chapter_hashing/hash_map.java
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* File: hash_map.java
|
||||
* Created Time: 2022-12-04
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
import java.util.*;
|
||||
import utils.*;
|
||||
|
||||
public class hash_map {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize hash table */
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
String name = map.get(15937);
|
||||
System.out.println("\nEnter student ID 15937, found name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
map.remove(10583);
|
||||
System.out.println("\nAfter removing 10583, the hash table is\nKey -> Value");
|
||||
PrintUtil.printHashMap(map);
|
||||
|
||||
/* Traverse hash table */
|
||||
System.out.println("\nTraverse key-value pairs Key->Value");
|
||||
for (Map.Entry<Integer, String> kv : map.entrySet()) {
|
||||
System.out.println(kv.getKey() + " -> " + kv.getValue());
|
||||
}
|
||||
System.out.println("\nIndividually traverse keys Key");
|
||||
for (int key : map.keySet()) {
|
||||
System.out.println(key);
|
||||
}
|
||||
System.out.println("\nIndividually traverse values Value");
|
||||
for (String val : map.values()) {
|
||||
System.out.println(val);
|
||||
}
|
||||
}
|
||||
}
|
||||
148
en/codes/java/chapter_hashing/hash_map_chaining.java
Normal file
148
en/codes/java/chapter_hashing/hash_map_chaining.java
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* File: hash_map_chaining.java
|
||||
* Created Time: 2023-06-13
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/* Chained address hash table */
|
||||
class HashMapChaining {
|
||||
int size; // Number of key-value pairs
|
||||
int capacity; // Hash table capacity
|
||||
double loadThres; // Load factor threshold for triggering expansion
|
||||
int extendRatio; // Expansion multiplier
|
||||
List<List<Pair>> buckets; // Bucket array
|
||||
|
||||
/* Constructor */
|
||||
public HashMapChaining() {
|
||||
size = 0;
|
||||
capacity = 4;
|
||||
loadThres = 2.0 / 3.0;
|
||||
extendRatio = 2;
|
||||
buckets = new ArrayList<>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.add(new ArrayList<>());
|
||||
}
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
int hashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
double loadFactor() {
|
||||
return (double) size / capacity;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
String get(int key) {
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, if the key is found, return the corresponding val
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
return pair.val;
|
||||
}
|
||||
}
|
||||
// If key is not found, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
void put(int key, String val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, if the specified key is encountered, update the corresponding val and return
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// If the key is not found, add the key-value pair to the end
|
||||
Pair pair = new Pair(key, val);
|
||||
bucket.add(pair);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
List<Pair> bucket = buckets.get(index);
|
||||
// Traverse the bucket, remove the key-value pair from it
|
||||
for (Pair pair : bucket) {
|
||||
if (pair.key == key) {
|
||||
bucket.remove(pair);
|
||||
size--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
void extend() {
|
||||
// Temporarily store the original hash table
|
||||
List<List<Pair>> bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = new ArrayList<>(capacity);
|
||||
for (int i = 0; i < capacity; i++) {
|
||||
buckets.add(new ArrayList<>());
|
||||
}
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
for (List<Pair> bucket : bucketsTmp) {
|
||||
for (Pair pair : bucket) {
|
||||
put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
void print() {
|
||||
for (List<Pair> bucket : buckets) {
|
||||
List<String> res = new ArrayList<>();
|
||||
for (Pair pair : bucket) {
|
||||
res.add(pair.key + " -> " + pair.val);
|
||||
}
|
||||
System.out.println(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_chaining {
|
||||
public static void main(String[] args) {
|
||||
/* Initialize hash table */
|
||||
HashMapChaining map = new HashMapChaining();
|
||||
|
||||
/* Add operation */
|
||||
// Add key-value pair (key, value) to the hash table
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
map.print();
|
||||
|
||||
/* Query operation */
|
||||
// Enter key to the hash table, get value
|
||||
String name = map.get(13276);
|
||||
System.out.println("\nEnter student ID 13276, found name " + name);
|
||||
|
||||
/* Remove operation */
|
||||
// Remove key-value pair (key, value) from the hash table
|
||||
map.remove(12836);
|
||||
System.out.println("\nAfter removing 12836, the hash table is\nKey -> Value");
|
||||
map.print();
|
||||
}
|
||||
}
|
||||
158
en/codes/java/chapter_hashing/hash_map_open_addressing.java
Normal file
158
en/codes/java/chapter_hashing/hash_map_open_addressing.java
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.java
|
||||
* Created Time: 2023-06-13
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
/* Open addressing hash table */
|
||||
class HashMapOpenAddressing {
|
||||
private int size; // Number of key-value pairs
|
||||
private int capacity = 4; // Hash table capacity
|
||||
private final double loadThres = 2.0 / 3.0; // Load factor threshold for triggering expansion
|
||||
private final int extendRatio = 2; // Expansion multiplier
|
||||
private Pair[] buckets; // Bucket array
|
||||
private final Pair TOMBSTONE = new Pair(-1, "-1"); // Removal mark
|
||||
|
||||
/* Constructor */
|
||||
public HashMapOpenAddressing() {
|
||||
size = 0;
|
||||
buckets = new Pair[capacity];
|
||||
}
|
||||
|
||||
/* Hash function */
|
||||
private int hashFunc(int key) {
|
||||
return key % capacity;
|
||||
}
|
||||
|
||||
/* Load factor */
|
||||
private double loadFactor() {
|
||||
return (double) size / capacity;
|
||||
}
|
||||
|
||||
/* Search for the bucket index corresponding to key */
|
||||
private int findBucket(int key) {
|
||||
int index = hashFunc(key);
|
||||
int firstTombstone = -1;
|
||||
// Linear probing, break when encountering an empty bucket
|
||||
while (buckets[index] != null) {
|
||||
// If the key is encountered, return the corresponding bucket index
|
||||
if (buckets[index].key == key) {
|
||||
// If a removal mark was encountered earlier, 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 encountered removal mark
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// Calculate the bucket index, return to the head if exceeding the tail
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// If the key does not exist, return the index of the insertion point
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* Query operation */
|
||||
public String get(int key) {
|
||||
// Search for the bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, return the corresponding val
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index].val;
|
||||
}
|
||||
// If the key-value pair does not exist, return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/* Add operation */
|
||||
public void put(int key, String val) {
|
||||
// When the load factor exceeds the threshold, perform expansion
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
// Search for the bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, overwrite val and return
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index].val = val;
|
||||
return;
|
||||
}
|
||||
// If the key-value pair does not exist, add the key-value pair
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* Remove operation */
|
||||
public void remove(int key) {
|
||||
// Search for the bucket index corresponding to key
|
||||
int index = findBucket(key);
|
||||
// If the key-value pair is found, cover it with a removal mark
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index] = TOMBSTONE;
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* Extend hash table */
|
||||
private void extend() {
|
||||
// Temporarily store the original hash table
|
||||
Pair[] bucketsTmp = buckets;
|
||||
// Initialize the extended new hash table
|
||||
capacity *= extendRatio;
|
||||
buckets = new Pair[capacity];
|
||||
size = 0;
|
||||
// Move key-value pairs from the original hash table to the new hash table
|
||||
for (Pair pair : bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
put(pair.key, pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
public void print() {
|
||||
for (Pair pair : buckets) {
|
||||
if (pair == null) {
|
||||
System.out.println("null");
|
||||
} else if (pair == TOMBSTONE) {
|
||||
System.out.println("TOMBSTONE");
|
||||
} else {
|
||||
System.out.println(pair.key + " -> " + pair.val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class hash_map_open_addressing {
|
||||
public static void main(String[] args) {
|
||||
// Initialize hash table
|
||||
HashMapOpenAddressing hashmap = new HashMapOpenAddressing();
|
||||
|
||||
// Add operation
|
||||
// Add key-value pair (key, val) to the hash table
|
||||
hashmap.put(12836, "Ha");
|
||||
hashmap.put(15937, "Luo");
|
||||
hashmap.put(16750, "Suan");
|
||||
hashmap.put(13276, "Fa");
|
||||
hashmap.put(10583, "Ya");
|
||||
System.out.println("\nAfter adding, the hash table is\nKey -> Value");
|
||||
hashmap.print();
|
||||
|
||||
// Query operation
|
||||
// Enter key to the hash table, get value val
|
||||
String name = hashmap.get(13276);
|
||||
System.out.println("\nEnter student ID 13276, found name " + name);
|
||||
|
||||
// Remove operation
|
||||
// Remove key-value pair (key, val) from the hash table
|
||||
hashmap.remove(16750);
|
||||
System.out.println("\nAfter removing 16750, the hash table is\nKey -> Value");
|
||||
hashmap.print();
|
||||
}
|
||||
}
|
||||
65
en/codes/java/chapter_hashing/simple_hash.java
Normal file
65
en/codes/java/chapter_hashing/simple_hash.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* File: simple_hash.java
|
||||
* Created Time: 2023-06-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_hashing;
|
||||
|
||||
public class simple_hash {
|
||||
/* Additive hash */
|
||||
static int addHash(String key) {
|
||||
long hash = 0;
|
||||
final int MODULUS = 1000000007;
|
||||
for (char c : key.toCharArray()) {
|
||||
hash = (hash + (int) c) % MODULUS;
|
||||
}
|
||||
return (int) hash;
|
||||
}
|
||||
|
||||
/* Multiplicative hash */
|
||||
static int mulHash(String key) {
|
||||
long hash = 0;
|
||||
final int MODULUS = 1000000007;
|
||||
for (char c : key.toCharArray()) {
|
||||
hash = (31 * hash + (int) c) % MODULUS;
|
||||
}
|
||||
return (int) hash;
|
||||
}
|
||||
|
||||
/* XOR hash */
|
||||
static int xorHash(String key) {
|
||||
int hash = 0;
|
||||
final int MODULUS = 1000000007;
|
||||
for (char c : key.toCharArray()) {
|
||||
hash ^= (int) c;
|
||||
}
|
||||
return hash & MODULUS;
|
||||
}
|
||||
|
||||
/* Rotational hash */
|
||||
static int rotHash(String key) {
|
||||
long hash = 0;
|
||||
final int MODULUS = 1000000007;
|
||||
for (char c : key.toCharArray()) {
|
||||
hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;
|
||||
}
|
||||
return (int) hash;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String key = "Hello algorithm";
|
||||
|
||||
int hash = addHash(key);
|
||||
System.out.println("Additive hash value is " + hash);
|
||||
|
||||
hash = mulHash(key);
|
||||
System.out.println("Multiplicative hash value is " + hash);
|
||||
|
||||
hash = xorHash(key);
|
||||
System.out.println("XOR hash value is " + hash);
|
||||
|
||||
hash = rotHash(key);
|
||||
System.out.println("Rotational hash value is " + hash);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user