forked from root/public
OGordoo
3 years ago
4 changed files with 185 additions and 0 deletions
@ -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<String> = std::env::args().collect(); |
||||||
|
|
||||||
|
rpn(&args[1]); |
||||||
|
} |
||||||
|
|
||||||
|
``` |
||||||
|
|
||||||
|
### Usage |
||||||
|
|
||||||
|
```console |
||||||
|
$ cargo run "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." |
||||||
|
Hello World! |
||||||
|
$ cargo run "+++++[>++++[>++++H>+++++i<<-]>>>++\n<<<<-]>>--------.>+++++.>." |
||||||
|
Hi |
||||||
|
$ cargo run "++++++++++[>++++++++++>++++++++++>++++++++++<<<-]>---.>--.>-.>++++++++++." |
||||||
|
abc |
||||||
|
``` |
@ -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 |
||||||
|
$ |
||||||
|
``` |
@ -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 |
||||||
|
$ |
||||||
|
``` |
@ -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<String> = 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``` |
||||||
|
```` |
Loading…
Reference in new issue