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. Do not import serde_json, write json output from scratch.
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. Do not import serde_json, write json output from scratch.
Here is a self-contained Rust program that reads a text file located at `/tmp/file.txt`, splits the file into four parts, processes each part in parallel to count word frequencies, and prints the results as JSON dictionaries, one per line.
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 52 53 54 55 56 | use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::sync::mpsc; use std::thread; use std::collections::HashMap; fn main() -> io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let reader = BufReader::new(file); let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?; let text = lines.join("\n"); let chunk_size = (text.len() + 3) / 4; // Split into 4 chunks let (tx, rx) = mpsc::channel(); for i in 0..4 { let tx = tx.clone(); let start = i * chunk_size; let end = if i == 3 { text.len() } else { (i + 1) * chunk_size }; let chunk = text[start..end].to_string(); thread::spawn(move || { let mut word_count = HashMap::new(); for word in chunk.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } let json = format_word_count_as_json(&word_count); tx.send(json).unwrap(); }); } drop(tx); // Close the sender to allow the receiver to exit for received in rx { println!("{}", received); } Ok(()) } fn format_word_count_as_json(word_count: &HashMap<String, u32>) -> String { let mut json = String::from("{"); for (i, (word, count)) in word_count.iter().enumerate() { if i != 0 { json.push_str(", "); } json.push_str(&format!("\"{}\": {}", word, count)); } json.push('}'); json } |
1 2 3 4 | {"This": 1} {"is": 2, "is,": 1} {"an": 1} {"example": 1} |
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 52 53 54 55 56 | use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::sync::mpsc; use std::thread; use std::collections::HashMap; fn main() -> io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let reader = BufReader::new(file); let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?; let text = lines.join("\n"); let chunk_size = (text.len() + 3) / 4; // Split into 4 chunks let (tx, rx) = mpsc::channel(); for i in 0..4 { let tx = tx.clone(); let start = i * chunk_size; let end = if i == 3 { text.len() } else { (i + 1) * chunk_size }; let chunk = text[start..end].to_string(); thread::spawn(move || { let mut word_count = HashMap::new(); for word in chunk.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } let json = format_word_count_as_json(&word_count); tx.send(json).unwrap(); }); } drop(tx); // Close the sender to allow the receiver to exit for received in rx { println!("{}", received); } Ok(()) } fn format_word_count_as_json(word_count: &HashMap<String, u32>) -> String { let mut json = String::from("{"); for (i, (word, count)) in word_count.iter().enumerate() { if i != 0 { json.push_str(", "); } json.push_str(&format!("\"{}\": {}", word, count)); } json.push('}'); 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::{self, BufRead, BufReader}; use std::sync::mpsc; use std::thread; use std::collections::HashMap; fn main() -> io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let reader = BufReader::new(file); let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?; let text = lines.join("\n"); let chunk_size = (text.len() + 3) / 4; // Split into 4 chunks let (tx, rx) = mpsc::channel(); for i in 0..4 { let tx = tx.clone(); let start = i * chunk_size; let end = if i == 3 { text.len() } else { (i + 1) * chunk_size }; let chunk = text[start..end].to_string(); thread::spawn(move || { let mut word_count = HashMap::new(); for word in chunk.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } let json = format_word_count_as_json(&word_count); tx.send(json).unwrap(); }); } drop(tx); // Close the sender to allow the receiver to exit for received in rx { println!("{}", received); } Ok(()) } fn format_word_count_as_json(word_count: &HashMap<String, u32>) -> String { let mut json = String::from("{"); for (i, (word, count)) in word_count.iter().enumerate() { if i != 0 { json.push_str(", "); } json.push_str(&format!("\"{}\": {}", word, count)); } json.push('}'); 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 52 53 54 55 56 | use std::fs::File; use std::io::{self, BufRead, BufReader}; use std::sync::mpsc; use std::thread; use std::collections::HashMap; fn main() -> io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let reader = BufReader::new(file); let lines: Vec<String> = reader.lines().collect::<Result<_, _>>()?; let text = lines.join("\n"); let chunk_size = (text.len() + 3) / 4; // Split into 4 chunks let (tx, rx) = mpsc::channel(); for i in 0..4 { let tx = tx.clone(); let start = i * chunk_size; let end = if i == 3 { text.len() } else { (i + 1) * chunk_size }; let chunk = text[start..end].to_string(); thread::spawn(move || { let mut word_count = HashMap::new(); for word in chunk.split_whitespace() { *word_count.entry(word.to_string()).or_insert(0) += 1; } let json = format_word_count_as_json(&word_count); tx.send(json).unwrap(); }); } drop(tx); // Close the sender to allow the receiver to exit for received in rx { println!("{}", received); } Ok(()) } fn format_word_count_as_json(word_count: &HashMap<String, u32>) -> String { let mut json = String::from("{"); for (i, (word, count)) in word_count.iter().enumerate() { if i != 0 { json.push_str(", "); } json.push_str(&format!("\"{}\": {}", word, count)); } json.push('}'); json } |
{"it": 4, "worst": 1, "was": 4, "foolish": 1, "of": 4, "best": 1, "times,": 2, "age": 2, "wisdom,": 1, "the": 4}
{"th": 1, "season": 1, "of": 3, "it": 4, "epoch": 2, "ness,": 1, "Light,": 1, "the": 3, "belief,": 1, "incredulity,": 1, "was": 4}
{"the": 2, "despair,": 1, "season": 1, "it": 2, "we": 1, "hope,": 1, "Darkness,": 1, "before": 1, "spring": 1, "e": 1, "was": 2, "of": 3, "everything": 1, "had": 1, "winter": 1}
{"before": 1, "other": 1, "Heaven,": 1, "nothing": 1, "we": 3, "direct": 2, "had": 1, "way": 1, "us,": 2, "the": 1, "all": 2, "going": 2, "to": 1, "were": 2}
Resulting in output: [('Darkness,', 1), ('Heaven,', 1), ('Light,', 1), ('age', 2), ('all', 2), ('before', 2), ('belief,', 1), ('best', 1), ('despair,', 1), ('direct', 2), ('e', 1), ('epoch', 2), ('everything', 1), ('foolish', 1), ('going', 2), ('had', 2), ('hope,', 1), ('incredulity,', 1), ('it', 10), ('ness,', 1), ('nothing', 1), ('of', 10), ('other', 1), ('season', 2), ('spring', 1), ('th', 1), ('the', 10), ('times,', 2), ('to', 1), ('us,', 2), ('was', 10), ('way', 1), ('we', 4), ('were', 2), ('winter', 1), ('wisdom,', 1), ('worst', 1)]
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