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 Rust program that meets your requirements:
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 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader, BufRead}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let mut file = File::open("/tmp/file.txt")?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(io::SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = String::new(); reader.by_ref() .take(end - start) .read_to_string(&mut chunk) .unwrap(); count_words(&chunk) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); println!("{}", serde_json::to_string(&word_counts).unwrap()); } Ok(()) } |
1 | rustc -C opt-level=3 -l serde_json word_count.rs |
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 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader, BufRead, Seek, SeekFrom}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let mut file = File::open("/tmp/file.txt")?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = String::new(); reader.by_ref() .take(end - start) .read_to_string(&mut chunk) .unwrap(); count_words(&chunk) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); println!("{:?}", word_counts); } Ok(()) } |
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::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader, BufRead, Seek, SeekFrom}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let mut file = File::open("/tmp/file.txt")?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = String::new(); reader.by_ref() .take(end - start) .read_to_string(&mut chunk) .unwrap(); count_words(&chunk) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); println!("{:?}", word_counts); } Ok(()) } 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 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader, BufRead, Seek, SeekFrom}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let mut file = File::open("/tmp/file.txt")?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = String::new(); reader.by_ref() .take(end - start) .read_to_string(&mut chunk) .unwrap(); count_words(&chunk) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); println!("{:?}", word_counts); } Ok(()) } |
{"the": 4, "it": 4, "of": 4, "worst": 1, "was": 4, "best": 1, "age": 2, "wisdom": 1, "foolish": 1, "times": 2}
{"epoch": 2, "ness": 1, "it": 4, "incredulity": 1, "Light": 1, "was": 4, "of": 3, "belief": 1, "th": 1, "the": 3, "season": 1}
{"Darkness": 1, "of": 3, "e": 1, "had": 1, "everything": 1, "before": 1, "spring": 1, "was": 2, "despair": 1, "hope": 1, "it": 2, "the": 2, "winter": 1, "season": 1, "we": 1}
{"all": 2, "way": 1, "were": 2, "nothing": 1, "us": 2, "before": 1, "we": 3, "had": 1, "going": 2, "direct": 2, "to": 1, "Heaven": 1, "the": 1, "other": 1}
warning: unused import: `BufRead`
--> main.rs:4:38
|
4 | use std::io::{self, Read, BufReader, BufRead, Seek, SeekFrom};
| ^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused imports: `Arc` and `Mutex`
--> main.rs:5:17
|
5 | use std::sync::{Arc, Mutex};
| ^^^ ^^^^^
warning: 2 warnings emitted
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
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 Rust program that meets your requirements:
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::{self, Read, BufReader, Seek, SeekFrom}; use std::collections::HashMap; use std::thread; use std::sync::{Arc, Mutex}; fn count_words(start: u64, length: u64) -> HashMap<String, usize> { let mut file = BufReader::new(File::open("/tmp/file.txt").unwrap()); file.seek(SeekFrom::Start(start)).unwrap(); let mut buffer = vec![0; length as usize]; file.read_exact(&mut buffer).unwrap(); let text = String::from_utf8_lossy(&buffer); let words: Vec<&str> = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())) .filter(|word| !word.is_empty()) .collect(); let mut word_counts = HashMap::new(); for word in words { *word_counts.entry(word.to_string()).or_insert(0) += 1; } word_counts } fn main() -> io::Result<()> { let file_size = std::fs::metadata("/tmp/file.txt")?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let start = i * chunk_size; let length = if i == 3 { file_size - start } else { chunk_size }; let handle = thread::spawn(move || { count_words(start, length) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); let json = serde_json::to_string(&word_counts).unwrap(); println!("{}", json); } Ok(()) } |
1 | rustc -L dependency=/path/to/serde_json/lib wordcount.rs |
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 | use std::fs::File; use std::io::{self, Read, BufReader, Seek, SeekFrom}; use std::collections::HashMap; use std::thread; use std::sync::{Arc, Mutex}; use std::path::Path; use serde_json; fn count_words(start: u64, length: u64) -> HashMap<String, usize> { let mut file = BufReader::new(File::open("/tmp/file.txt").unwrap()); file.seek(SeekFrom::Start(start)).unwrap(); let mut buffer = vec![0; length as usize]; file.read_exact(&mut buffer).unwrap(); let text = String::from_utf8_lossy(&buffer); let words: Vec<&str> = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())) .filter(|word| !word.is_empty()) .collect(); let mut word_counts = HashMap::new(); for word in words { *word_counts.entry(word.to_string()).or_insert(0) += 1; } word_counts } fn main() -> io::Result<()> { let file_size = std::fs::metadata("/tmp/file.txt")?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let start = i * chunk_size; let length = if i == 3 { file_size - start } else { chunk_size }; let handle = thread::spawn(move || { count_words(start, length) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); let json = serde_json::to_string(&word_counts).unwrap(); println!("{}", json); } Ok(()) } |
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, Read, BufReader, Seek, SeekFrom}; use std::collections::HashMap; use std::thread; use std::sync::{Arc, Mutex}; use std::path::Path; use serde_json; fn count_words(start: u64, length: u64) -> HashMap<String, usize> { let mut file = BufReader::new(File::open("/tmp/file.txt").unwrap()); file.seek(SeekFrom::Start(start)).unwrap(); let mut buffer = vec![0; length as usize]; file.read_exact(&mut buffer).unwrap(); let text = String::from_utf8_lossy(&buffer); let words: Vec<&str> = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())) .filter(|word| !word.is_empty()) .collect(); let mut word_counts = HashMap::new(); for word in words { *word_counts.entry(word.to_string()).or_insert(0) += 1; } word_counts } fn main() -> io::Result<()> { let file_size = std::fs::metadata("/tmp/file.txt")?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let start = i * chunk_size; let length = if i == 3 { file_size - start } else { chunk_size }; let handle = thread::spawn(move || { count_words(start, length) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); let json = serde_json::to_string(&word_counts).unwrap(); println!("{}", json); } Ok(()) } 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 | use std::fs::File; use std::io::{self, Read, BufReader, Seek, SeekFrom}; use std::collections::HashMap; use std::thread; use std::sync::{Arc, Mutex}; use std::path::Path; use serde_json; fn count_words(start: u64, length: u64) -> HashMap<String, usize> { let mut file = BufReader::new(File::open("/tmp/file.txt").unwrap()); file.seek(SeekFrom::Start(start)).unwrap(); let mut buffer = vec![0; length as usize]; file.read_exact(&mut buffer).unwrap(); let text = String::from_utf8_lossy(&buffer); let words: Vec<&str> = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())) .filter(|word| !word.is_empty()) .collect(); let mut word_counts = HashMap::new(); for word in words { *word_counts.entry(word.to_string()).or_insert(0) += 1; } word_counts } fn main() -> io::Result<()> { let file_size = std::fs::metadata("/tmp/file.txt")?.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let start = i * chunk_size; let length = if i == 3 { file_size - start } else { chunk_size }; let handle = thread::spawn(move || { count_words(start, length) }); handles.push(handle); } for handle in handles { let word_counts = handle.join().unwrap(); let json = serde_json::to_string(&word_counts).unwrap(); println!("{}", json); } Ok(()) } |
error[E0432]: unresolved import `serde_json`
--> main.rs:8:5
|
8 | use serde_json;
| ^^^^^^^^^^ no `serde_json` in the root
warning: unused imports: `Arc` and `Mutex`
--> main.rs:6:17
|
6 | use std::sync::{Arc, Mutex};
| ^^^ ^^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused import: `std::path::Path`
--> main.rs:7:5
|
7 | use std::path::Path;
| ^^^^^^^^^^^^^^^
error: aborting due to 1 previous error; 2 warnings emitted
For more information about this error, try `rustc --explain E0432`.
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
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 Rust program that meets your requirements:
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 57 58 59 60 61 62 63 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; use std::thread; use std::sync::{Arc, Mutex}; fn count_words(content: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = content.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> std::io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let results = Arc::new(Mutex::new(Vec::new())); let mut handles = vec![]; for i in 0..4 { let results = Arc::clone(&results); let mut file = file.try_clone()?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = vec![0; (end - start) as usize]; reader.read_exact(&mut chunk).unwrap(); let chunk_str = String::from_utf8_lossy(&chunk); let word_counts = count_words(&chunk_str); let mut results = results.lock().unwrap(); results.push(serde_json::to_string(&word_counts).unwrap()); }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } let results = results.lock().unwrap(); for result in results.iter() { println!("{}", result); } Ok(()) } |
1 | cargo install serde_json |
1 | rustc -L $(find ~/.cargo/registry -name "libserde_json*.rlib") word_count.rs |
1 2 3 4 | {"word1": 5, "word2": 3, ...} {"word3": 2, "word4": 1, ...} {"word5": 4, "word6": 2, ...} {"word7": 3, "word8": 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 57 58 59 60 61 62 63 64 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; use std::thread; use std::sync::{Arc, Mutex}; use serde_json; fn count_words(content: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = content.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> std::io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let results = Arc::new(Mutex::new(Vec::new())); let mut handles = vec![]; for i in 0..4 { let results = Arc::clone(&results); let mut file = file.try_clone()?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = vec![0; (end - start) as usize]; reader.read_exact(&mut chunk).unwrap(); let chunk_str = String::from_utf8_lossy(&chunk); let word_counts = count_words(&chunk_str); let mut results = results.lock().unwrap(); results.push(serde_json::to_string(&word_counts).unwrap()); }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } let results = results.lock().unwrap(); for result in results.iter() { println!("{}", result); } Ok(()) } |
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::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; use std::thread; use std::sync::{Arc, Mutex}; use serde_json; fn count_words(content: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = content.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> std::io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let results = Arc::new(Mutex::new(Vec::new())); let mut handles = vec![]; for i in 0..4 { let results = Arc::clone(&results); let mut file = file.try_clone()?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = vec![0; (end - start) as usize]; reader.read_exact(&mut chunk).unwrap(); let chunk_str = String::from_utf8_lossy(&chunk); let word_counts = count_words(&chunk_str); let mut results = results.lock().unwrap(); results.push(serde_json::to_string(&word_counts).unwrap()); }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } let results = results.lock().unwrap(); for result in results.iter() { println!("{}", result); } Ok(()) } 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 57 58 59 60 61 62 63 64 | use std::collections::HashMap; use std::fs::File; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom}; use std::thread; use std::sync::{Arc, Mutex}; use serde_json; fn count_words(content: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = content.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> std::io::Result<()> { let file = File::open("/tmp/file.txt")?; let file_size = file.metadata()?.len(); let chunk_size = file_size / 4; let results = Arc::new(Mutex::new(Vec::new())); let mut handles = vec![]; for i in 0..4 { let results = Arc::clone(&results); let mut file = file.try_clone()?; let start = i * chunk_size; let end = if i == 3 { file_size } else { (i + 1) * chunk_size }; let handle = thread::spawn(move || { file.seek(SeekFrom::Start(start)).unwrap(); let mut reader = BufReader::new(file); let mut chunk = vec![0; (end - start) as usize]; reader.read_exact(&mut chunk).unwrap(); let chunk_str = String::from_utf8_lossy(&chunk); let word_counts = count_words(&chunk_str); let mut results = results.lock().unwrap(); results.push(serde_json::to_string(&word_counts).unwrap()); }); handles.push(handle); } for handle in handles { handle.join().unwrap(); } let results = results.lock().unwrap(); for result in results.iter() { println!("{}", result); } Ok(()) } |
error[E0432]: unresolved import `serde_json`
--> main.rs:7:5
|
7 | use serde_json;
| ^^^^^^^^^^ no `serde_json` in the root
warning: unused import: `BufRead`
--> main.rs:4:15
|
4 | use std::io::{BufRead, BufReader, Read, Seek, SeekFrom};
| ^^^^^^^
|
= note: `#[warn(unused_imports)]` on by default
error: aborting due to 1 previous error; 1 warning emitted
For more information about this error, try `rustc --explain E0432`.
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
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 Rust program that accomplishes this task using only standard library features:
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 57 58 59 60 61 62 63 64 65 | use std::collections::HashMap; use std::fs::File; use std::io::{Read, Result}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> Result<()> { // Read the entire file let mut file = File::open("/tmp/file.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; // Split file into 4 approximately equal chunks let chunk_size = contents.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { contents.len() } else { (i + 1) * chunk_size }; chunks.push(contents[start..end].to_string()); } // Shared results container let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let mut handles = Vec::new(); for chunk in chunks { let results = Arc::clone(&results); let handle = thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }); handles.push(handle); } // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let final_results = results.lock().unwrap(); for result in final_results.iter() { let json = serde_json::to_string(result).unwrap(); println!("{}", json); } Ok(()) } |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | use std::collections::HashMap; use std::fs::File; use std::io::{Read, Result}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn json_serialize(map: &HashMap<String, usize>) -> String { let mut json = "{".to_string(); let mut first = true; for (key, value) in map { if !first { json.push_str(", "); } json.push_str(&format!("\"{}\":{}", key, value)); first = false; } json.push('}'); json } fn main() -> Result<()> { // Read the entire file let mut file = File::open("/tmp/file.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; // Split file into 4 approximately equal chunks let chunk_size = contents.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { contents.len() } else { (i + 1) * chunk_size }; chunks.push(contents[start..end].to_string()); } // Shared results container let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let mut handles = Vec::new(); for chunk in chunks { let results = Arc::clone(&results); let handle = thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }); handles.push(handle); } // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let final_results = results.lock().unwrap(); for result in final_results.iter() { let json = json_serialize(result); println!("{}", json); } Ok(()) } |
1 2 | rustc word_count.rs
./word_count
|
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | use std::collections::HashMap; use std::fs::File; use std::io::{Read, Result}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn json_serialize(map: &HashMap<String, usize>) -> String { let mut json = "{".to_string(); let mut first = true; for (key, value) in map { if !first { json.push_str(", "); } json.push_str(&format!("\"{}\":{}", key, value)); first = false; } json.push('}'); json } fn main() -> Result<()> { // Read the entire file let mut file = File::open("/tmp/file.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; // Split file into 4 approximately equal chunks let chunk_size = contents.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { contents.len() } else { (i + 1) * chunk_size }; chunks.push(contents[start..end].to_string()); } // Shared results container let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let mut handles = Vec::new(); for chunk in chunks { let results = Arc::clone(&results); let handle = thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }); handles.push(handle); } // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let final_results = results.lock().unwrap(); for result in final_results.iter() { let json = json_serialize(result); println!("{}", json); } Ok(()) } |
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::collections::HashMap; use std::fs::File; use std::io::{Read, Result}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn json_serialize(map: &HashMap<String, usize>) -> String { let mut json = "{".to_string(); let mut first = true; for (key, value) in map { if !first { json.push_str(", "); } json.push_str(&format!("\"{}\":{}", key, value)); first = false; } json.push('}'); json } fn main() -> Result<()> { // Read the entire file let mut file = File::open("/tmp/file.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; // Split file into 4 approximately equal chunks let chunk_size = contents.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { contents.len() } else { (i + 1) * chunk_size }; chunks.push(contents[start..end].to_string()); } // Shared results container let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let mut handles = Vec::new(); for chunk in chunks { let results = Arc::clone(&results); let handle = thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }); handles.push(handle); } // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let final_results = results.lock().unwrap(); for result in final_results.iter() { let json = json_serialize(result); println!("{}", json); } Ok(()) } 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | use std::collections::HashMap; use std::fs::File; use std::io::{Read, Result}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text .split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn json_serialize(map: &HashMap<String, usize>) -> String { let mut json = "{".to_string(); let mut first = true; for (key, value) in map { if !first { json.push_str(", "); } json.push_str(&format!("\"{}\":{}", key, value)); first = false; } json.push('}'); json } fn main() -> Result<()> { // Read the entire file let mut file = File::open("/tmp/file.txt")?; let mut contents = String::new(); file.read_to_string(&mut contents)?; // Split file into 4 approximately equal chunks let chunk_size = contents.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { contents.len() } else { (i + 1) * chunk_size }; chunks.push(contents[start..end].to_string()); } // Shared results container let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let mut handles = Vec::new(); for chunk in chunks { let results = Arc::clone(&results); let handle = thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }); handles.push(handle); } // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let final_results = results.lock().unwrap(); for result in final_results.iter() { let json = json_serialize(result); println!("{}", json); } Ok(()) } |
{"best":1, "was":4, "of":4, "worst":1, "times":2, "the":4, "age":2, "it":4, "wisdom":1, "foolish":1}
{"the":3, "ness":1, "it":4, "th":1, "was":4, "of":3, "epoch":2, "incredulity":1, "season":1, "belief":1, "Light":1}
{"Darkness":1, "winter":1, "season":1, "it":2, "spring":1, "we":1, "despair":1, "hope":1, "was":2, "e":1, "before":1, "of":3, "everything":1, "had":1, "the":2}
{"to":1, "the":1, "nothing":1, "were":2, "way":1, "before":1, "going":2, "Heaven":1, "other":1, "had":1, "we":3, "us":2, "direct":2, "all":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
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 self-contained Rust program that accomplishes this task:
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 57 58 59 60 61 62 63 64 65 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { // Open the file let file = File::open("/tmp/file.txt")?; let mut reader = BufReader::new(file); // Read entire file content let mut content = String::new(); reader.read_to_string(&mut content)?; // Split content into 4 roughly equal parts let chunk_size = content.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { content.len() } else { (i + 1) * chunk_size }; chunks.push(content[start..end].to_string()); } // Use Arc and Mutex for thread-safe shared state let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let handles: Vec<_> = chunks.into_iter().map(|chunk| { let results = Arc::clone(&results); thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }) }).collect(); // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let results = results.lock().unwrap(); for dict in results.iter() { println!("{}", serde_json::to_string(dict).unwrap()); } Ok(()) } |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { // Open the file let file = File::open("/tmp/file.txt")?; let mut reader = BufReader::new(file); // Read entire file content let mut content = String::new(); reader.read_to_string(&mut content)?; // Split content into 4 roughly equal parts let chunk_size = content.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { content.len() } else { (i + 1) * chunk_size }; chunks.push(content[start..end].to_string()); } // Use Arc and Mutex for thread-safe shared state let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let handles: Vec<_> = chunks.into_iter().map(|chunk| { let results = Arc::clone(&results); thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }) }).collect(); // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let results = results.lock().unwrap(); for dict in results.iter() { let json = dict.iter() .map(|(k, v)| format!("\"{}\":{}", k, v)) .collect::<Vec<_>>() .join(","); println!("{{{}}}", json); } Ok(()) } |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { // Open the file let file = File::open("/tmp/file.txt")?; let mut reader = BufReader::new(file); // Read entire file content let mut content = String::new(); reader.read_to_string(&mut content)?; // Split content into 4 roughly equal parts let chunk_size = content.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { content.len() } else { (i + 1) * chunk_size }; chunks.push(content[start..end].to_string()); } // Use Arc and Mutex for thread-safe shared state let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let handles: Vec<_> = chunks.into_iter().map(|chunk| { let results = Arc::clone(&results); thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }) }).collect(); // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let results = results.lock().unwrap(); for dict in results.iter() { let json = dict.iter() .map(|(k, v)| format!("\"{}\":{}", k, v)) .collect::<Vec<_>>() .join(","); println!("{{{}}}", json); } Ok(()) } |
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::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { // Open the file let file = File::open("/tmp/file.txt")?; let mut reader = BufReader::new(file); // Read entire file content let mut content = String::new(); reader.read_to_string(&mut content)?; // Split content into 4 roughly equal parts let chunk_size = content.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { content.len() } else { (i + 1) * chunk_size }; chunks.push(content[start..end].to_string()); } // Use Arc and Mutex for thread-safe shared state let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let handles: Vec<_> = chunks.into_iter().map(|chunk| { let results = Arc::clone(&results); thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }) }).collect(); // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let results = results.lock().unwrap(); for dict in results.iter() { let json = dict.iter() .map(|(k, v)| format!("\"{}\":{}", k, v)) .collect::<Vec<_>>() .join(","); println!("{{{}}}", json); } Ok(()) } 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 57 58 59 60 61 62 63 64 65 66 67 68 69 | use std::collections::HashMap; use std::fs::File; use std::io::{self, Read, BufReader}; use std::sync::{Arc, Mutex}; use std::thread; fn count_words(text: &str) -> HashMap<String, usize> { let mut word_counts = HashMap::new(); let words = text.split_whitespace() .map(|word| word.trim_matches(|c: char| !c.is_alphanumeric())); for word in words { if !word.is_empty() { *word_counts.entry(word.to_string()).or_insert(0) += 1; } } word_counts } fn main() -> io::Result<()> { // Open the file let file = File::open("/tmp/file.txt")?; let mut reader = BufReader::new(file); // Read entire file content let mut content = String::new(); reader.read_to_string(&mut content)?; // Split content into 4 roughly equal parts let chunk_size = content.len() / 4; let mut chunks = Vec::new(); for i in 0..4 { let start = i * chunk_size; let end = if i == 3 { content.len() } else { (i + 1) * chunk_size }; chunks.push(content[start..end].to_string()); } // Use Arc and Mutex for thread-safe shared state let results = Arc::new(Mutex::new(Vec::new())); // Spawn threads let handles: Vec<_> = chunks.into_iter().map(|chunk| { let results = Arc::clone(&results); thread::spawn(move || { let word_counts = count_words(&chunk); let mut results = results.lock().unwrap(); results.push(word_counts); }) }).collect(); // Wait for all threads to complete for handle in handles { handle.join().unwrap(); } // Print results let results = results.lock().unwrap(); for dict in results.iter() { let json = dict.iter() .map(|(k, v)| format!("\"{}\":{}", k, v)) .collect::<Vec<_>>() .join(","); println!("{{{}}}", json); } Ok(()) } |
{"was":4,"the":4,"wisdom":1,"foolish":1,"worst":1,"of":4,"age":2,"times":2,"best":1,"it":4}
{"of":3,"epoch":2,"Light":1,"season":1,"th":1,"ness":1,"the":3,"belief":1,"incredulity":1,"it":4,"was":4}
{"Heaven":1,"us":2,"way":1,"nothing":1,"to":1,"were":2,"the":1,"we":3,"going":2,"all":2,"direct":2,"before":1,"other":1,"had":1}
{"despair":1,"Darkness":1,"it":2,"e":1,"hope":1,"we":1,"the":2,"before":1,"season":1,"had":1,"was":2,"everything":1,"winter":1,"of":3,"spring":1}
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