chore(i8n,learn): processed translations
This commit is contained in:
committed by
Mrugesh Mohapatra
parent
15047f2d90
commit
e5c44a3ae5
@ -1,18 +1,28 @@
|
||||
---
|
||||
id: 587d825b367417b2b2512c8e
|
||||
title: 创建一个哈希表
|
||||
title: Create a Hash Table
|
||||
challengeType: 1
|
||||
videoUrl: ''
|
||||
forumTopicId: 301627
|
||||
dashedName: create-a-hash-table
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
在这个挑战中,我们将学习哈希表。哈希表用于实现关联数组或键值对的映射,就像我们刚刚研究的对象和地图一样。例如,JavaScript对象可以实现为哈希表(其实际实现将取决于它运行的环境)。哈希表的工作方式是它接受一个键输入并以确定的方式将此键散列到某个数值。然后将该数值用作存储相关值的实际键。然后,如果您尝试再次访问相同的密钥,则散列函数将处理密钥,返回相同的数字结果,然后将用于查找关联的值。这平均提供非常有效的O(1)查找时间。散列表可以实现为具有散列函数的数组,从而生成指定范围内的数组索引。在这种方法中,数组大小的选择很重要,散列函数也是如此。例如,如果散列函数为两个不同的键生成相同的值,该怎么办?这称为碰撞。处理冲突的一种方法是仅将两个键值对存储在该索引处。然后,在查找其中任何一个时,您将不得不遍历项目桶以找到您要查找的密钥。良好的散列函数可最大限度地减少冲突,从而保持有效的搜索时间。在这里,我们不会关注散列或散列表实现的细节,我们将尝试大致了解它们的工作原理。说明:让我们创建哈希表的基本功能。我们已经创建了一个天真的散列函数供您使用。您可以将字符串值传递给函数哈希,它将返回一个哈希值,您可以将其用作存储键。在this.collection对象中根据此散列值存储项目。创建这三种方法:添加,删除和查找。第一个应该接受一个键值对来添加到哈希表。第二个应该在传递密钥时删除键值对。第三个应该接受一个键并返回相关的值,如果该键不存在则返回null。请务必编写代码以解决冲突!
|
||||
In this challenge we will learn about hash tables. A Hash table is used to implement associative arrays, or mappings of key-value pairs, like the objects and Maps we have just been studying. A JavaScript object could be implemented as a hash table, for instance (its actual implementation will depend on the environment it's running in). The way a hash table works is that it takes a key input and hashes this key in a deterministic way to some numerical value. This numerical value is then used as the actual key the associated value is stored by. Then, if you try to access the same key again, the hashing function will process the key, return the same numerical result, which will then be used to look up the associated value. This provides very efficient O(1) lookup time on average.
|
||||
|
||||
Hash tables can be implemented as arrays with hash functions producing array indices within a specified range. In this method, the choice of the array size is important, as is the hashing function. For instance, what if the hashing function produces the same value for two different keys? This is called a collision. One way to handle collisions is to just store both key-value pairs at that index. Then, upon lookup of either, you would have to iterate through the bucket of items to find the key you are looking for. A good hashing function will minimize collisions to maintain efficient search time.
|
||||
|
||||
Here, we won't be concerned with the details of hashing or hash table implementation, we will just try to get a general sense of how they work.
|
||||
|
||||
# --instructions--
|
||||
|
||||
Let's create the basic functionality of a hash table. We've created a naive hashing function for you to use. You can pass a string value to the function `hash` and it will return a hashed value you can use as a key for storage. Store items based on this hashed value in the `this.collection` object. Create these three methods: `add`, `remove`, and `lookup`. The first should accept a key value pair to add to the hash table. The second should remove a key-value pair when passed a key. The third should accept a key and return the associated value or `null` if the key is not present.
|
||||
|
||||
Be sure to write your code to account for collisions!
|
||||
|
||||
# --hints--
|
||||
|
||||
存在HashTable数据结构。
|
||||
The HashTable data structure should exist.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -26,7 +36,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
HashTable有一个add方法。
|
||||
The HashTable should have an add method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -40,7 +50,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
HashTable有一个删除方法。
|
||||
The HashTable should have a remove method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -54,7 +64,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
HashTable有一个查找方法。
|
||||
The HashTable should have a lookup method.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -68,7 +78,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
add方法添加键值对,lookup方法返回与给定键关联的值。
|
||||
The add method should add key value pairs and the lookup method should return the values associated with a given key.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -83,32 +93,25 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
remove方法接受一个键作为输入,并删除关联的键值对。
|
||||
The remove method should accept a key as input and should remove the associated key value pair.
|
||||
|
||||
```js
|
||||
assert(
|
||||
(function () {
|
||||
var test = false;
|
||||
var hashValue = hash('key');
|
||||
if (typeof HashTable !== 'undefined') {
|
||||
test = new HashTable();
|
||||
}
|
||||
test.add = addMethodSolution;
|
||||
test.add('key', 'value');
|
||||
test.remove('key');
|
||||
test.lookup = function (key) {
|
||||
var theHash = hash(key);
|
||||
if (this.collection.hasOwnProperty(theHash)[key]) {
|
||||
return this.collection[theHash][key];
|
||||
}
|
||||
return null;
|
||||
};
|
||||
var lookup = test.lookup('key');
|
||||
test.lookup = null;
|
||||
return lookup === null;
|
||||
return !test.collection.hasOwnProperty(hashValue);
|
||||
})()
|
||||
);
|
||||
```
|
||||
|
||||
使用哈希函数添加项目。
|
||||
Items should be added using the hash function.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -126,7 +129,7 @@ assert(
|
||||
);
|
||||
```
|
||||
|
||||
哈希表处理冲突。
|
||||
The hash table should handle collisions.
|
||||
|
||||
```js
|
||||
assert(
|
||||
@ -187,7 +190,7 @@ var hash = string => {
|
||||
var HashTable = function() {
|
||||
this.collection = {};
|
||||
// Only change code below this line
|
||||
|
||||
|
||||
// Only change code above this line
|
||||
};
|
||||
```
|
||||
|
Reference in New Issue
Block a user