Skip to content

Commit

Permalink
feat: add ex10
Browse files Browse the repository at this point in the history
  • Loading branch information
leogaudin committed Sep 1, 2024
1 parent e9347db commit 0d2a9e7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- [07 - SAT](#07---sat) 🧩
- [08 - Powerset](#08---powerset) 🔋
- [09 - Set Evaluation](#09---set-evaluation) 🧾
- [10 - Curve](#10---curve) 📈
- [11 - Inverse function](#11---inverse-function) 🔄
- [Resources](#resources) 📖

# Exercises
Expand Down Expand Up @@ -878,6 +880,38 @@ Now that we demystified those three operations, we know that our function will w
3. **Evaluate the formula** for each set of values.
4. **Apply the corresponding set operation** to the sets.

## 10 - Curve

```rust
fn map(x: u16, y: u16) -> f64;
```

> You must write a function that takes as input two integers `x` and `y`, and returns a floating-point number that represents the value of the curve at the point `(x, y)`.
>
> You must write a function (the inverse of a space-filling curve, used to encode spatial data into a line) that takes a pair of coordinates in two dimensions and assigns a unique value in the closed interval [0; 1] ∈ R.
>
> Let f be a function and let A be a set such as:
>
> $$
> f : (x, y) \in [[0; 2^{16} - 1]]^2 \subset \mathbb{N}^2 \rightarrow [0; 1] \subset \mathbb{R}
> $$
> $$
> A \subset [0; 1] \subset \mathbb{R}
> $$
---

It is easy to get frightened by the mathematical notation, but the instructions are actually quite simple:

- The function that takes two integers `x` and `y` comprised between $0$ and $2^{16} - 1$, in the set of natural numbers.
- It returns a floating-point number $A$, comprised between $0$ and $1$, in the set of real numbers.

To do this, we can use binary once again.

We know that the two inputs will not exceed $2^{16}$ each, so we can concatenate them to form a single number of $32$ bits.

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

- [📺 Add Two Numbers Without The "+" Sign (Bit Shifting Basics)](https://www.youtube.com/watch?v=qq64FrA2UXQ)
Expand All @@ -894,3 +928,5 @@ Now that we demystified those three operations, we know that our function will w
---
- [📺 Power Set](https://www.youtube.com/watch?v=FOQn8afAvLE)
- [📺 Generate Power set for a given set](https://www.youtube.com/watch?v=8xQXq5JLhEY) (bitwise way)
---
- [📚 jmcheon/ready_set_boole/ex10_11](https://github.com/jmcheon/ready_set_boole/tree/main?tab=readme-ov-file#ex10_11)
4 changes: 4 additions & 0 deletions src/ex10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub fn map(x: u16, y: u16) -> f64 {
let concatenated: u32 = (x as u32) << 16 | y as u32;
return concatenated as f64 / u32::MAX as f64;
}
27 changes: 27 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ use ex08::powerset;
mod ex09;
use ex09::eval_set;

mod ex10;
use ex10::map;

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

fn main() {
println!("\n{}", "EX00 - ADDER".bold());
for _ in 0..42 {
Expand Down Expand Up @@ -336,6 +342,27 @@ fn main() {
eval_set(formula.0, sets[i].clone()),
);
}

println!("\n{}", "EX10 - CURVE".bold());
let coordinates: Vec<((u16, u16), f64)> = vec![
((0, 0), 0.0),
((124, 5345), 0.0018933343239811561),
((42141, 5543), 0.6430219206127855),
((u16::MAX, u16::MAX), 1.0),
];

for coord in coordinates {
println!(
"{}\t{:?} → {}",
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)
);
}
}

fn check_conjunctions(formula: &str) -> bool {
Expand Down

0 comments on commit 0d2a9e7

Please sign in to comment.