mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 23:09:18 +00:00
翻譯
This commit is contained in:
parent
23ff14a68f
commit
10fc4cf388
@ -1,12 +1,10 @@
|
||||
# Hashmaps
|
||||
# 雜湊表
|
||||
|
||||
A *hash map* allows you to associate a value with a particular key.
|
||||
You may also know this by the names [*unordered map* in C++](https://en.cppreference.com/w/cpp/container/unordered_map),
|
||||
[*dictionary* in Python](https://docs.python.org/3/tutorial/datastructures.html#dictionaries) or an *associative array* in other languages.
|
||||
*雜湊表* 允許您將值與特定的鍵關聯。
|
||||
您可能也知道它在 C++ 中被稱為 [*unordered map*](https://en.cppreference.com/w/cpp/container/unordered_map),在 Python 中被稱為 [*dictionary*](https://docs.python.org/3/tutorial/datastructures.html#dictionaries),或在其他程式語言中稱為 *associative array*。
|
||||
|
||||
This is the other data structure that we've been talking about before, when
|
||||
talking about Vecs.
|
||||
這是我們之前談論 Vecs 時提到的另一個資料結構。
|
||||
|
||||
## Further information
|
||||
## 更多資訊
|
||||
|
||||
- [Storing Keys with Associated Values in Hash Maps](https://doc.rust-lang.org/book/ch08-03-hash-maps.html)
|
||||
- [在雜湊表中存儲鍵和值](https://doc.rust-lang.org/book/ch08-03-hash-maps.html)
|
||||
|
||||
@ -1,27 +1,22 @@
|
||||
// hashmaps1.rs
|
||||
//
|
||||
// A basket of fruits in the form of a hash map needs to be defined. The key
|
||||
// represents the name of the fruit and the value represents how many of that
|
||||
// particular fruit is in the basket. You have to put at least three different
|
||||
// types of fruits (e.g. apple, banana, mango) in the basket and the total count
|
||||
// of all the fruits should be at least five.
|
||||
// 需要定義一個包含水果的雜湊表。鍵代表水果的名稱,值代表籃子中那種特定水果的數量。您必須在籃子中放至少三種不同類型的水果(例如蘋果、香蕉、芒果),且所有水果的總數應至少為五個。
|
||||
//
|
||||
// Make me compile and pass the tests!
|
||||
// 使我能夠編譯並通過測試!
|
||||
//
|
||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint hashmaps1` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn fruit_basket() -> HashMap<String, u32> {
|
||||
let mut basket = // TODO: declare your hash map here.
|
||||
let mut basket = // TODO: 在這裡聲明您的雜湊表。
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
// 已經給了您兩根香蕉 :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
|
||||
// TODO: Put more fruits in your basket here.
|
||||
// TODO: 在這裡放更多的水果到您的籃子中。
|
||||
|
||||
basket
|
||||
}
|
||||
|
||||
@ -1,18 +1,10 @@
|
||||
// hashmaps2.rs
|
||||
//
|
||||
// We're collecting different fruits to bake a delicious fruit cake. For this,
|
||||
// we have a basket, which we'll represent in the form of a hash map. The key
|
||||
// represents the name of each fruit we collect and the value represents how
|
||||
// many of that particular fruit we have collected. Three types of fruits -
|
||||
// Apple (4), Mango (2) and Lychee (5) are already in the basket hash map. You
|
||||
// must add fruit to the basket so that there is at least one of each kind and
|
||||
// more than 11 in total - we have a lot of mouths to feed. You are not allowed
|
||||
// to insert any more of these fruits!
|
||||
// 我們正在收集不同的水果來烤一個美味的水果蛋糕。為此,我們有一個籃子,我們將用雜湊表來表示。鍵代表我們收集的每種水果的名稱,值代表我們收集了多少這種特定的水果。三種類型的水果 - 蘋果(4)、芒果(2)和荔枝(5)已經在籃子雜湊表中。您必須向籃子中添加水果,以便每種水果至少有一個,總數超過 11 - 我們有很多人要餵養。您不允許再插入這些水果中的任何一種!
|
||||
//
|
||||
// Make me pass the tests!
|
||||
// 讓我通過測試!
|
||||
//
|
||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint hashmaps2` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
@ -37,9 +29,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
];
|
||||
|
||||
for fruit in fruit_kinds {
|
||||
// TODO: Insert new fruits if they are not already present in the
|
||||
// basket. Note that you are not allowed to put any type of fruit that's
|
||||
// already present!
|
||||
// TODO: 如果籃子中還不存在這種水果,則插入新的水果。請注意,您不允許放入已經存在的任何類型的水果!
|
||||
}
|
||||
}
|
||||
|
||||
@ -47,7 +37,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Don't modify this function!
|
||||
// 不要修改此函數!
|
||||
fn get_fruit_basket() -> HashMap<Fruit, u32> {
|
||||
let mut basket = HashMap::<Fruit, u32>::new();
|
||||
basket.insert(Fruit::Apple, 4);
|
||||
|
||||
@ -1,32 +1,27 @@
|
||||
// hashmaps3.rs
|
||||
//
|
||||
// A list of scores (one per line) of a soccer match is given. Each line is of
|
||||
// the form : "<team_1_name>,<team_2_name>,<team_1_goals>,<team_2_goals>"
|
||||
// Example: England,France,4,2 (England scored 4 goals, France 2).
|
||||
// 給定了一場足球比賽的比分列表(每行一個)。每行的格式為 : "<team_1_name>,<team_2_name>,<team_1_goals>,<team_2_goals>"
|
||||
// 例如:England,France,4,2(英格蘭隊打進4球,法國隊打進2球)。
|
||||
//
|
||||
// You have to build a scores table containing the name of the team, the total
|
||||
// number of goals the team scored, and the total number of goals the team
|
||||
// conceded. One approach to build the scores table is to use a Hashmap.
|
||||
// The solution is partially written to use a Hashmap,
|
||||
// complete it to pass the test.
|
||||
// 您需要構建一個包含球隊名稱、球隊總進球數和球隊總失球數的比分表。一種構建比分表的方法是使用雜湊表。
|
||||
// 解決方案部分使用了雜湊表,完成它以通過測試。
|
||||
//
|
||||
// Make me pass the tests!
|
||||
// 讓我通過測試!
|
||||
//
|
||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
// 執行 `rustlings hint hashmaps3` 或使用 `hint` watch 子命令來獲取提示。
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
// A structure to store the goal details of a team.
|
||||
// 一個存儲球隊進球詳細信息的結構體。
|
||||
struct Team {
|
||||
goals_scored: u8,
|
||||
goals_conceded: u8,
|
||||
}
|
||||
|
||||
fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
// The name of the team is the key and its associated struct is the value.
|
||||
// 球隊的名稱是鍵,其關聯的結構體是值。
|
||||
let mut scores: HashMap<String, Team> = HashMap::new();
|
||||
|
||||
for r in results.lines() {
|
||||
@ -35,11 +30,7 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
let team_1_score: u8 = v[2].parse().unwrap();
|
||||
let team_2_name = v[1].to_string();
|
||||
let team_2_score: u8 = v[3].parse().unwrap();
|
||||
// TODO: Populate the scores table with details extracted from the
|
||||
// current line. Keep in mind that goals scored by team_1
|
||||
// will be the number of goals conceded by team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
// TODO: 使用從當前行提取的詳細信息填充比分表。請記住,team_1 進的球數將是 team_2 的失球數,反之亦然。
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user