Skip to content

Commit

Permalink
feat: add ex11
Browse files Browse the repository at this point in the history
  • Loading branch information
leogaudin committed Sep 1, 2024
1 parent 0d2a9e7 commit a092209
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 33 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,49 @@ We know that the two inputs will not exceed $2^{16}$ each, so we can concatenate

> 💡 We can use the `u32` type to store this number.
### Steps

1. **Shift `x` to the left by $16$ bits**.
2. **Perform a bitwise OR** between `x` and `y`.
3. **Divide the result by $2^{32} - 1$** to get a number between $0$ and $1$.

And that's it! You have your solution.

## 11 - Curve Inverse

```rust
fn reverse_map(n: f64) -> (u16, u16);
```

> You must write the inverse function $f^{−1}$ of the function $f$ from the previous exercise (so this time, this is a space-filling curve, used to decode data from a line into a space).
>
> Let $f$ and $A$ be the function and set from the previous exercise, then:
>
> $$f^{-1} : A \rightarrow [[0; 2^{16} - 1]]^2 \subset \mathbb{N}^2$$
>
> The above function must be implemented such that the following expressions are true for values that are in range:
>
> $$(f^{-1} \circ f)(x, y) = (x, y)$$
> $$(f \circ f^{-1})(x) = x$$
---

Let's decrypt the mathematical notation.

- The function $f^{-1}$ takes a floating-point number $n$ in the set $A$, and returns a pair of integers $(x, y)$ comprised between $0$ and $2^{16} - 1$.

- If you apply $f$ to a pair of integers $(x, y)$, and then apply $f^{-1}$ to the result, you should get back $(x, y)$.

- If you apply $f^{-1}$ to a floating-point number $n$, and then apply $f$ to the result, you should get back $n$.

So basically, you need to reverse the operation you did in the previous exercise.

### Steps

1. **Multiply `n` by $2^{32} - 1$** to get a number between $0$ and $2^{32} - 1$.
2. **Shift `n` to the right by $16$ bits** to get the original `x`.
3. **Shift `n` to the left by $16$ bits, then again to the right by $16$ bits** to get the original `y`.

# Resources

- [📺 Add Two Numbers Without The "+" Sign (Bit Shifting Basics)](https://www.youtube.com/watch?v=qq64FrA2UXQ)
Expand Down
7 changes: 7 additions & 0 deletions src/ex11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub fn reverse_map(n: f64) -> (u16, u16) {
let n: u32 = (n * u32::MAX as f64) as u32;
let x: u16 = (n >> 16) as u16;
let y: u16 = ((n << 16) >> 16) as u16;

return (x, y);
}
59 changes: 26 additions & 33 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use ex09::eval_set;
mod ex10;
use ex10::map;

// mod ex11;
// use ex11::reverse_map;
mod ex11;
use ex11::reverse_map;

fn main() {
println!("\n{}", "EX00 - ADDER".bold());
Expand Down Expand Up @@ -236,10 +236,7 @@ fn main() {
);

println!("Formula: {}", formula.bold());
println!(
"RPN output: {}",
conjunctive_normal_form(formula).bold()
);
println!("RPN output: {}", conjunctive_normal_form(formula).bold());
println!();
}

Expand Down Expand Up @@ -295,30 +292,12 @@ fn main() {

println!("\n{}", "EX09 - EVAL SET".bold());
let sets: Vec<Vec<Vec<i32>>> = vec![
vec![
vec![0, 1, 2],
vec![0, 3, 4]
],
vec![
vec![0, 1, 2],
vec![3, 4, 5]
],
vec![
vec![0, 1, 2]
],
vec![
vec![0, 1, 2],
vec![3, 4, 5],
vec![6, 7, 3]
],
vec![
vec![42, 43],
vec![44, 45, 46],
],
vec![
vec![42, 43],
vec![44, 45, 46],
],
vec![vec![0, 1, 2], vec![0, 3, 4]],
vec![vec![0, 1, 2], vec![3, 4, 5]],
vec![vec![0, 1, 2]],
vec![vec![0, 1, 2], vec![3, 4, 5], vec![6, 7, 3]],
vec![vec![42, 43], vec![44, 45, 46]],
vec![vec![42, 43], vec![44, 45, 46]],
];
let formulas: Vec<(&str, Vec<i32>)> = vec![
("AB&", vec![0]),
Expand Down Expand Up @@ -351,16 +330,30 @@ fn main() {
((u16::MAX, u16::MAX), 1.0),
];

for coord in coordinates {
for coord in &coordinates {
println!(
"{}\t{:?} → {}",
if map(coord.0.0, coord.0.1) == coord.1 {
if map(coord.0 .0, coord.0 .1) == coord.1 {
"OK".green().bold()
} else {
"KO".red().bold()
},
coord.0,
map(coord.0.0, coord.0.1)
map(coord.0 .0, coord.0 .1)
);
}

println!("\n{}", "EX11 - INVERSE FUNCTION".bold());
for coord in &coordinates {
println!(
"{}\t{} → {:?}",
if reverse_map(coord.1) == (coord.0 .0, coord.0 .1) {
"OK".green().bold()
} else {
"KO".red().bold()
},
coord.1,
reverse_map(coord.1)
);
}
}
Expand Down

0 comments on commit a092209

Please sign in to comment.