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's a simple Rust program that fulfills 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | use std::fs::File; use std::io::{Read, BufReader}; use std::path::Path; use std::thread; use std::collections::HashMap; fn main() -> std::io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let metadata = std::fs::metadata(file_path)?; let file_size = metadata.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let file = File::open(file_path)?; let start = i * chunk_size; let mut reader = BufReader::new(file); let mut buffer = vec![]; // Read the chunk reader.seek(std::io::SeekFrom::Start(start))?; let mut bytes_read = 0; while bytes_read < chunk_size { let bytes = reader.fill_buf()?; let bytes_to_read = std::cmp::min(bytes.len(), chunk_size - bytes_read); buffer.extend_from_slice(&bytes[..bytes_to_read]); bytes_read += bytes_to_read; reader.consume(bytes_to_read); if bytes_to_read < bytes.len() { break; } } // Spawn a new thread to count words in the chunk let handle = thread::spawn(move || { let count = count_words(buffer); print_count(count); }); handles.push(handle); } // Wait for all threads to finish for handle in handles { handle.join().unwrap(); } Ok(()) } fn count_words(buffer: Vec<u8>) -> HashMap<String, u32> { let string = String::from_utf8_lossy(&buffer); let words: Vec<&str> = string.split_whitespace().collect(); let mut count = HashMap::new(); for word in words { *count.entry(word.to_string()).or_insert(0) += 1; } count } fn print_count(count: HashMap<String, u32>) { print!("{"); let mut first = true; for (key, value) in count { if first { first = false; } else { print!(", "); } print!("\"{}\": {}", key, value); } println!("}"); } |
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 | use std::fs::File; use std::io::{Read, BufReader}; use std::path::Path; use std::thread; use std::collections::HashMap; fn main() -> std::io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let metadata = std::fs::metadata(file_path)?; let file_size = metadata.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let file = File::open(file_path)?; let start = i * chunk_size; let mut reader = BufReader::new(file); let mut buffer = vec![]; // Read the chunk reader.seek(std::io::SeekFrom::Start(start))?; let mut bytes_read = 0; while bytes_read < chunk_size { let bytes = reader.fill_buf()?; let bytes_to_read = std::cmp::min(bytes.len(), chunk_size - bytes_read); buffer.extend_from_slice(&bytes[..bytes_to_read]); bytes_read += bytes_to_read; reader.consume(bytes_to_read); if bytes_to_read < bytes.len() { break; } } // Spawn a new thread to count words in the chunk let handle = thread::spawn(move || { let count = count_words(buffer); print_count(count); }); handles.push(handle); } // Wait for all threads to finish for handle in handles { handle.join().unwrap(); } Ok(()) } fn count_words(buffer: Vec<u8>) -> HashMap<String, u32> { let string = String::from_utf8_lossy(&buffer); let words: Vec<&str> = string.split_whitespace().collect(); let mut count = HashMap::new(); for word in words { *count.entry(word.to_string()).or_insert(0) += 1; } count } fn print_count(count: HashMap<String, u32>) { print!("{"); let mut first = true; for (key, value) in count { if first { first = false; } else { print!(", "); } print!("\"{}\": {}", key, value); } println!("}"); } |
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::{Read, BufReader}; use std::path::Path; use std::thread; use std::collections::HashMap; fn main() -> std::io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let metadata = std::fs::metadata(file_path)?; let file_size = metadata.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let file = File::open(file_path)?; let start = i * chunk_size; let mut reader = BufReader::new(file); let mut buffer = vec![]; // Read the chunk reader.seek(std::io::SeekFrom::Start(start))?; let mut bytes_read = 0; while bytes_read < chunk_size { let bytes = reader.fill_buf()?; let bytes_to_read = std::cmp::min(bytes.len(), chunk_size - bytes_read); buffer.extend_from_slice(&bytes[..bytes_to_read]); bytes_read += bytes_to_read; reader.consume(bytes_to_read); if bytes_to_read < bytes.len() { break; } } // Spawn a new thread to count words in the chunk let handle = thread::spawn(move || { let count = count_words(buffer); print_count(count); }); handles.push(handle); } // Wait for all threads to finish for handle in handles { handle.join().unwrap(); } Ok(()) } fn count_words(buffer: Vec<u8>) -> HashMap<String, u32> { let string = String::from_utf8_lossy(&buffer); let words: Vec<&str> = string.split_whitespace().collect(); let mut count = HashMap::new(); for word in words { *count.entry(word.to_string()).or_insert(0) += 1; } count } fn print_count(count: HashMap<String, u32>) { print!("{"); let mut first = true; for (key, value) in count { if first { first = false; } else { print!(", "); } print!("\"{}\": {}", key, value); } println!("}"); } 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 | use std::fs::File; use std::io::{Read, BufReader}; use std::path::Path; use std::thread; use std::collections::HashMap; fn main() -> std::io::Result<()> { let file_path = "/tmp/file.txt"; let file = File::open(file_path)?; let metadata = std::fs::metadata(file_path)?; let file_size = metadata.len(); let chunk_size = file_size / 4; let mut handles = vec![]; for i in 0..4 { let file = File::open(file_path)?; let start = i * chunk_size; let mut reader = BufReader::new(file); let mut buffer = vec![]; // Read the chunk reader.seek(std::io::SeekFrom::Start(start))?; let mut bytes_read = 0; while bytes_read < chunk_size { let bytes = reader.fill_buf()?; let bytes_to_read = std::cmp::min(bytes.len(), chunk_size - bytes_read); buffer.extend_from_slice(&bytes[..bytes_to_read]); bytes_read += bytes_to_read; reader.consume(bytes_to_read); if bytes_to_read < bytes.len() { break; } } // Spawn a new thread to count words in the chunk let handle = thread::spawn(move || { let count = count_words(buffer); print_count(count); }); handles.push(handle); } // Wait for all threads to finish for handle in handles { handle.join().unwrap(); } Ok(()) } fn count_words(buffer: Vec<u8>) -> HashMap<String, u32> { let string = String::from_utf8_lossy(&buffer); let words: Vec<&str> = string.split_whitespace().collect(); let mut count = HashMap::new(); for word in words { *count.entry(word.to_string()).or_insert(0) += 1; } count } fn print_count(count: HashMap<String, u32>) { print!("{"); let mut first = true; for (key, value) in count { if first { first = false; } else { print!(", "); } print!("\"{}\": {}", key, value); } println!("}"); } |
error: invalid format string: expected `'}'` but string was terminated
--> main.rs:67:14
|
67 | print!("{");
| -^ expected `'}'` in format string
| |
| because of this opening brace
|
= note: if you intended to print `{`, you can escape it using `{{`
error: invalid format string: unmatched `}` found
--> main.rs:77:15
|
77 | println!("}");
| ^ unmatched `}` in format string
|
= note: if you intended to print `}`, you can escape it using `}}`
warning: unused import: `Read`
--> main.rs:3:15
|
3 | use std::io::{Read, BufReader};
| ^^^^
|
= note: `#[warn(unused_imports)]` on by default
warning: unused import: `std::path::Path`
--> main.rs:4:5
|
4 | use std::path::Path;
| ^^^^^^^^^^^^^^^
error[E0599]: no method named `seek` found for struct `BufReader` in the current scope
--> main.rs:24:16
|
24 | reader.seek(std::io::SeekFrom::Start(start))?;
| ^^^^ method not found in `BufReader<File>`
--> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/io/mod.rs:1914:8
|
= note: the method is available for `BufReader<File>` here
|
= help: items from traits can only be used if the trait is in scope
help: trait `Seek` which provides `seek` is implemented but not in scope; perhaps you want to import it
|
2 + use std::io::Seek;
|
error[E0599]: no method named `fill_buf` found for struct `BufReader` in the current scope
--> main.rs:27:32
|
27 | let bytes = reader.fill_buf()?;
| ^^^^^^^^ method not found in `BufReader<File>`
--> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/io/mod.rs:2222:8
|
= note: the method is available for `BufReader<File>` here
|
= help: items from traits can only be used if the trait is in scope
help: trait `BufRead` which provides `fill_buf` is implemented but not in scope; perhaps you want to import it
|
2 + use std::io::BufRead;
|
error[E0599]: no method named `consume` found for struct `BufReader` in the current scope
--> main.rs:31:20
|
31 | reader.consume(bytes_to_read);
| ^^^^^^^ method not found in `BufReader<File>`
--> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14/library/std/src/io/mod.rs:2244:8
|
= note: the method is available for `BufReader<File>` here
|
= help: items from traits can only be used if the trait is in scope
help: trait `BufRead` which provides `consume` is implemented but not in scope; perhaps you want to import it
|
2 + use std::io::BufRead;
|
error: aborting due to 5 previous errors; 2 warnings emitted
For more information about this error, try `rustc --explain E0599`.
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