I have setup the docker container to run the model evaluation.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each job taking ~1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Write a self-contained Rust program that prints a JSON output of how often each word appears in a text file located in /tmp/file.txt. So if the file contains "This is is is, an example" then the program should print to stdout. {"This": 1, "is": 2, "is,": 1, "an": 1, "example": 1} Don't import any crates that would require a Cargo.toml file. I'll just compile it with `rustc yourfile.rs` and it should work. The program should be run by calling /path/to/rust_word_count with no arguments. Make it run in parallel on 4 cores at the same time, each job taking ~1/4 of the file. Print out 4 differnt json dicts, one per line. I'll do the merging.
Here's a simple Rust program that reads a file, splits it into lines, splits each line into words, counts the occurrences of each word, and prints the counts in JSON format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | use std::fs::File; use std::io::{BufRead, BufReader}; use std::collections::HashMap; use std::thread; use std::sync::mpsc; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).unwrap(); let reader = BufReader::new(file); let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect(); let (tx, rx) = mpsc::channel(); let num_threads = 4; let chunk_size = lines.len() / num_threads; for i in 0..num_threads { let tx_clone = tx.clone(); let start = i * chunk_size; let end = if i == num_threads - 1 { lines.len() } else { (i + 1) * chunk_size }; thread::spawn(move || { let mut word_count: HashMap<String, usize> = HashMap::new(); for line in lines[start..end].iter() { for word in line.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } } let mut json = String::new(); json.push_str("{"); for (word, count) in word_count { json.push_str(&format!("\"{}\": {}, ", word, count)); } json.pop(); // Remove the last space json.pop(); // Remove the last comma json.push_str("}"); tx_clone.send(json).unwrap(); }); } drop(tx); for _ in 0..num_threads { let json = rx.recv().unwrap(); println!("{}", json); } } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | use std::fs::File; use std::io::{BufRead, BufReader}; use std::collections::HashMap; use std::thread; use std::sync::mpsc; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).unwrap(); let reader = BufReader::new(file); let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect(); let (tx, rx) = mpsc::channel(); let num_threads = 4; let chunk_size = lines.len() / num_threads; for i in 0..num_threads { let tx_clone = tx.clone(); let start = i * chunk_size; let end = if i == num_threads - 1 { lines.len() } else { (i + 1) * chunk_size }; thread::spawn(move || { let mut word_count: HashMap<String, usize> = HashMap::new(); for line in lines[start..end].iter() { for word in line.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } } let mut json = String::new(); json.push_str("{"); for (word, count) in word_count { json.push_str(&format!("\"{}\": {}, ", word, count)); } json.pop(); // Remove the last space json.pop(); // Remove the last comma json.push_str("}"); tx_clone.send(json).unwrap(); }); } drop(tx); for _ in 0..num_threads { let json = rx.recv().unwrap(); println!("{}", json); } } |
Below is some student's code. I asked them to parallelize some code. Do not judge if it is correct, I have test cases for that. Logic errors are okay. So are synatx errors. Just tell me if they tried to parallelize it. use std::fs::File; use std::io::{BufRead, BufReader}; use std::collections::HashMap; use std::thread; use std::sync::mpsc; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).unwrap(); let reader = BufReader::new(file); let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect(); let (tx, rx) = mpsc::channel(); let num_threads = 4; let chunk_size = lines.len() / num_threads; for i in 0..num_threads { let tx_clone = tx.clone(); let start = i * chunk_size; let end = if i == num_threads - 1 { lines.len() } else { (i + 1) * chunk_size }; thread::spawn(move || { let mut word_count: HashMap<String, usize> = HashMap::new(); for line in lines[start..end].iter() { for word in line.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } } let mut json = String::new(); json.push_str("{"); for (word, count) in word_count { json.push_str(&format!("\"{}\": {}, ", word, count)); } json.pop(); // Remove the last space json.pop(); // Remove the last comma json.push_str("}"); tx_clone.send(json).unwrap(); }); } drop(tx); for _ in 0..num_threads { let json = rx.recv().unwrap(); println!("{}", json); } } Answer either 'The student passes' or 'The student fails'.
The student passes.
Testing if the previous output contains the string student passes
: True
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | use std::fs::File; use std::io::{BufRead, BufReader}; use std::collections::HashMap; use std::thread; use std::sync::mpsc; fn main() { let file_path = "/tmp/file.txt"; let file = File::open(file_path).unwrap(); let reader = BufReader::new(file); let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect(); let (tx, rx) = mpsc::channel(); let num_threads = 4; let chunk_size = lines.len() / num_threads; for i in 0..num_threads { let tx_clone = tx.clone(); let start = i * chunk_size; let end = if i == num_threads - 1 { lines.len() } else { (i + 1) * chunk_size }; thread::spawn(move || { let mut word_count: HashMap<String, usize> = HashMap::new(); for line in lines[start..end].iter() { for word in line.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } } let mut json = String::new(); json.push_str("{"); for (word, count) in word_count { json.push_str(&format!("\"{}\": {}, ", word, count)); } json.pop(); // Remove the last space json.pop(); // Remove the last comma json.push_str("}"); tx_clone.send(json).unwrap(); }); } drop(tx); for _ in 0..num_threads { let json = rx.recv().unwrap(); println!("{}", json); } } |
warning: variable does not need to be mutable
--> main.rs:12:9
|
12 | let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
| ----^^^^^
| |
| help: remove this `mut`
|
= note: `#[warn(unused_mut)]` on by default
error[E0382]: borrow of moved value: `lines`
--> main.rs:22:45
|
12 | let mut lines: Vec<String> = reader.lines().map(|line| line.unwrap()).collect();
| --------- move occurs because `lines` has type `Vec<String>`, which does not implement the `Copy` trait
...
19 | for i in 0..num_threads {
| ----------------------- inside of this loop
...
22 | let end = if i == num_threads - 1 { lines.len() } else { (i + 1) * chunk_size };
| ^^^^^ value borrowed here after move
23 |
24 | thread::spawn(move || {
| ------- value moved into closure here, in previous iteration of loop
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0382`.
main.sh: line 2: ./a.out: No such file or directory
Resulting in output: []
Testing if the previous output equals the string [('Darkness,', 1), ('Heaven,', 1), ('Light,', 1), ('age', 2), ('all', 2), ('before', 2), ('belief,', 1), ('best', 1), ('despair,', 1), ('direct', 2), ('epoch', 2), ('everything', 1), ('foolishness,', 1), ('going', 2), ('had', 2), ('hope,', 1), ('incredulity,', 1), ('it', 10), ('nothing', 1), ('of', 10), ('other', 1), ('season', 2), ('spring', 1), ('the', 11), ('times,', 2), ('to', 1), ('us,', 2), ('was', 10), ('way', 1), ('we', 4), ('were', 2), ('winter', 1), ('wisdom,', 1), ('worst', 1)]
: False