From 65ba9208fcd311a0ac08f522f84fca0d55b6e6f5 Mon Sep 17 00:00:00 2001 From: OGordoo Date: Wed, 27 Jan 2021 14:46:34 +0000 Subject: [PATCH] new subjects for rust exams --- subjects/brain_fuck/README.md | 42 ++++++++++++++++++ subjects/nextprime/README.md | 37 ++++++++++++++++ subjects/previousprime/README.md | 33 +++++++++++++++ subjects/rpn/README.md | 73 ++++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 subjects/brain_fuck/README.md create mode 100644 subjects/nextprime/README.md create mode 100644 subjects/previousprime/README.md create mode 100644 subjects/rpn/README.md diff --git a/subjects/brain_fuck/README.md b/subjects/brain_fuck/README.md new file mode 100644 index 00000000..b6832d5c --- /dev/null +++ b/subjects/brain_fuck/README.md @@ -0,0 +1,42 @@ +## brain_fuck + +### Instructions + +Write a `Brainfuck` interpreter program. +The source code will be given as first parameter. +The code will always be valid, with less than 4096 operations. +`Brainfuck` is a minimalist language. It consists of an array of bytes (in this exercice 2048 bytes) all initialized with zero, and with a pointer to its first byte. + +Every operator consists of a single character : + +- '>' increment the pointer +- '<' decrement the pointer +- '+' increment the pointed byte +- '-' decrement the pointed byte +- '.' print the pointed byte on standard output +- '[' go to the matching ']' if the pointed byte is 0 (loop start) +- ']' go to the matching '[' if the pointed byte is not 0 (loop end) + +Any other character is a comment. + +For receiving arguments from the command line you should use something like: + +```rust + fn main() { + let args: Vec = std::env::args().collect(); + + rpn(&args[1]); + } + +``` + +### Usage + +```console +$ cargo run "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." +Hello World! +$ cargo run "+++++[>++++[>++++H>+++++i<<-]>>>++\n<<<<-]>>--------.>+++++.>." +Hi +$ cargo run "++++++++++[>++++++++++>++++++++++>++++++++++<<<-]>---.>--.>-.>++++++++++." +abc +``` diff --git a/subjects/nextprime/README.md b/subjects/nextprime/README.md new file mode 100644 index 00000000..5c1ef2fc --- /dev/null +++ b/subjects/nextprime/README.md @@ -0,0 +1,37 @@ +## nextprime + +### Instructions + +Write a function that returns the first prime number that is equal or superior to the `int` passed as parameter. + +The function must be optimized in order to avoid time-outs with the tester. + +(We consider that only positive numbers can be prime numbers) + +### Expected function + +```rust +pub fn next_prime(nbr: u64) -> u64 { + +} +``` + +### Usage + +Here is a possible program to test your function : + +```rust +fn main() { + println!("The next prime after 4 is: {}", next_prime(4)); + println!("The next prime after 11 is: {}", next_prime(11)); +} +``` + +And its output : + +```console +$ cargo run +The next prime after 4 is: 5 +The next prime after 11 is: 11 +$ +``` diff --git a/subjects/previousprime/README.md b/subjects/previousprime/README.md new file mode 100644 index 00000000..44ddbd0d --- /dev/null +++ b/subjects/previousprime/README.md @@ -0,0 +1,33 @@ +## prev_prime + +### Instructions + +Write a function that returns the first prime number that is equal or inferior to the `int` passed as parameter. + +If there are no primes inferior to the `int` passed as parameter the function should return 0. + +### Expected function + +```rust +pub fn prev_prime(nbr: u64) -> u64 { + +} +``` + +### Usage + +Here is a possible [program](TODO-LINK) to test your function : + +```rust +fn main() { + println!("The previous prime number before 34 is: {}", prev_prime(34)); +} +``` + +And its output : + +```console +$ cargo run +The previous prime number before 34 is: 31 +$ +``` diff --git a/subjects/rpn/README.md b/subjects/rpn/README.md new file mode 100644 index 00000000..122f90f5 --- /dev/null +++ b/subjects/rpn/README.md @@ -0,0 +1,73 @@ +## rpn + +### Instructions + +Write a program that takes a `string` which contains an equation written in +`Reverse Polish Notation` (RPN) as its first argument, that evaluates the equation, and that +prints the result on the standard output followed by a newline (`'\n'`). + +`Reverse Polish Notation` is a mathematical notation in which every operator +follows all of its operands. In RPN, every operator encountered evaluates the +previous 2 operands, and the result of this operation then becomes the first of +the two operands for the subsequent operator. Operands and operators must be +spaced by at least one space. + +The following operators must be implemented : `+`, `-`, `*`, `/`, and `%`. + +If the `string` is not valid or if there is not exactly one argument, `Error` must be printed +on the standard output followed by a newline. +If the `string` has extra spaces it is still considered valid. + +All the given operands must fit in a `int`. + +Examples of formulas converted in RPN: + +3 + 4 >> 3 4 + + +((1 \* 2) \* 3) - 4 >> 1 2 \* 3 \* 4 - or 3 1 2 \* \* 4 - + +50 \* (5 - (10 / 9)) >> 5 10 9 / - 50 \* + +Here is how to evaluate a formula in RPN: + +``` +1 2 * 3 * 4 - +2 3 * 4 - +6 4 - +2 +``` + +Or: + +``` +3 1 2 * * 4 - +3 2 * 4 - +6 4 - +2 +``` + +For receiving arguments from the command line you should use something like: + +```rust + fn main() { + let args: Vec = std::env::args().collect(); + + rpn(&args[1]); + } + +``` + +### Usage + +````console +$ cargo run "1 2 * 3 * 4 +" +10 +$ cargo run "1 2 3 4 +" +Error +$ cargo run +Error +$ cargo run " 1 3 * 2 -" +1 +$ cargo run " 1 3 * ksd 2 -" +Error``` +````