I've created this repo to track the problems I've solved and make my testing setup available to others who are still learning rust and want to do the solutions locally to make use of rust-analyzer while learning.
A separate binary is created for each problem. They can be found in src/bin
. Evaluation of the solution against the supplied test cases can be done by setting tests/run_samples.rs
to point to the binary you want to test. See setup steps for details. It assumes the binary name matches the name of the folder with the samples (without the "-testcases" suffix). To run the problem manually use cargo run --bin <problem_name>
. For more info see https://doc.rust-lang.org/cargo/reference/cargo-targets.html?highlight=bin#binaries.
- From the problem page click
Exit Full Screen View
. - Download the sample test files from Hacker Rank.
- Optionally rename the downloaded file to remove any prefix.
- Extract the downloaded files into a folder.
- Move the folder to
tests/samples
. - Copy the problem name from the folder name.
- Update the problem name in
tests/run_samples.rs
. - Determine the expected output of the algorithm.
- Set type of evaluation used for problem in
tests/run_samples.rs
. - Create a new file in
src/bin
with the name of the problem followed by.rs
. - Copy the starter code into the new file.
- Read problem description on website and write your solution.
- Use
cargo test --test run_samples
to run the test cases
Assumes you have the repo cloned locally. Uses /
in instructions if you use Windows then use \
as appropriate.
- From the problem page click
Exit Full Screen View
- Download the sample test files from Hacker Rank
- Optionally rename downloaded file to remove any prefix such as
three-month-preparation-kit-
. - Extract the downloaded files into a folder so that inside of that folder has 2 folders (
input
andoutput
). - Move the folder containing
input
andoutput
totests/samples
. - Copy the problem name from the folder name. It's the whole folder name less the suffix
-testcases
. - Update the problem name in
tests/run_samples.rs
. (Look for comment saying,Change problem name here
near the top of the file). - Determine the expected output of the algorithm. Thus far I've only seen two methods, printing to stdout and writing to a file.
- Writing to a file: There are two things you can look for to determine if this is what is being used.
- A line similar to
let mut fptr = File::create(env::var("OUTPUT_PATH").unwrap()).unwrap();
near the top of main. - A line similar to
writeln!(&mut fptr, "{}", result).ok();
at the bottom of main.
- A line similar to
- Printing to stdout: This one is a bit more tricky but in general you can:
- Look for the absence of the things for writing to a file.
- Look for a line similar to
println!("{}", result.join(" "));
near the bottom of main. - Sometimes it's just in the question as it tells you to print your output
- Writing to a file: There are two things you can look for to determine if this is what is being used.
- Set type of evaluation used for problem in
tests/run_samples.rs
, on the line following the name of the problem from the step above. - Create a new file in
src/bin
with the name of the problem followed by.rs
eg.countingsort1.rs
(Must match name set intests/run_samples.rs
). - Copy the starter code into the new file.
- Read problem description on website and write your solution.
- Then just use
cargo test --test run_samples
to run the test cases on your solution (filtered to justrun_samples
to limit amount of junk printed to the screen).