nature-of-code / noc-book-2

The 2024 edition of The Nature of Code with p5.js. Includes Notion workflow and build system.
https://natureofcode.com
1.19k stars 82 forks source link

Some solutions to exercises for chapter 0 #1038

Open sidwellr opened 1 month ago

sidwellr commented 1 month ago

Here are my solutions for the chapter 0 exercises. Feel free to use some, all, or none of them on the website. Snapshots are included in the sketches on the editor.p5js.org site.

Exercise 0.1, random walker with down-right tendency: https://editor.p5js.org/rsidwell/sketches/JgeveyQkr

The exercise text states that "the" solution follows in the next section (example 0.3), but my solution is quite different. Rather than use information after the exercise in the book, I used the step function described earlier in the chapter that set xstep and ystep to random(-1, 1), and just made the parameters assymetrical to create the down-right tendency. I personally prefer it to the example 0.3 method, but it may confuse readers expecting the latter.

Exercise 0.2, two probability questions with text answers. Here are mine:

  1. What is the probability of drawing two aces in a row from a deck of 52 cards, if you reshuffle your first draw back into the deck before making your second draw?

4/52 * 4/52 = 0.0059 or 0.6%.

  1. What would that probability be if you didn’t reshuffle after your first draw?

4/52 * 3/51 = 0.0045 or 0.4%.

Exercise 0.3, random walker with dynamic probabilities: https://editor.p5js.org/rsidwell/sketches/loLe4_6j5

The exercise suggests a 50% chance of moving towards the mouse, but I liked 25% better.

Exercise 0.4, paint splatter simulation: https://editor.p5js.org/rsidwell/sketches/dW7vKdBTc

This was fun, and I got a bit carried away. It may be too much for a "suggested solution". (It's probably more appropriate for the Coding Train Passenger Showcase, except there are no videos on the normal distribution to attach it to.)

Exercise 0.5, Gaussian random walker: https://editor.p5js.org/rsidwell/sketches/SUIj1U6Wv

This one is straightforward.

Exercise 0.6, custom probability distribution walker: https://editor.p5js.org/rsidwell/sketches/Uiqkl4nUd

I actually have two solutions for this one. The first was my first thought; it uses the accept-reject algorithm to produce a random number between 0 and 1 with a quadratic distribution, then scales the value by step and randomly changes the sign. But I wondered if the accept-reject algorithm could be made to produce the target distribution directly so I didn't have to modify it. After some research, I got it working: https://editor.p5js.org/rsidwell/sketches/cxAGKTY16. The result is the same, but the second one needs information beyond what is in the book, so I would use the first as the suggested solution. But you have a choice!

(A more efficient way to implement this would be to use Math.cbrt(random(-step, step)) (see #932), but that kind of defeats the purpose of the exercise to try the accept-reject algorithm.)

Exercise 0.7, Perlin noise random walker: https://editor.p5js.org/rsidwell/sketches/S41FvvQzK

This one is straightforward.

Exercise 0.8, play with 2D Perlin noise: https://editor.p5js.org/rsidwell/sketches/nZFfZLE0w

I may have gotten carried away here too. Once I add a control panel, it's just too easy to add more controls! I also use the x and y values directly, multiplied by the "frequency", instead of using xoff and yoff as done in the book. To me, this is more appropriate since the 2D noise is actually based on the points, but it may be confusing since it is so different from the book.

Exercise 0.9, animated 2D noise: https://editor.p5js.org/rsidwell/sketches/R6CTkPo_a

I just added animation to my solution for exercise 0.8. If you think that solution is too confusing to include, this one will be too.

shiffman commented 1 month ago

Wow, thank you for these! Fantastic work! Apologies it may take me some time to review and incorporate, but I hope to get to it soon!

shiffman commented 1 month ago

@tuantinghuang is going to work on incorporating these!

shiffman commented 11 hours ago

@tuantinghuang worked on incorporating these solutions with some adjustments and @jasongao97 implemented the feature where they show up as a "suggested answer" and on the exercise page!

https://natureofcode.com/exercises/

Take a peek @sidwellr and if it all looks good to you, this issue can be closed! We outlined a bit more about the process for submitting exercise solutions in the README:

https://github.com/nature-of-code/noc-book-2/blob/main/README.md#exercise-solutions

sidwellr commented 6 hours ago

All looks good to me! Thanks.

You may want to add a statement to the process in README that controls, if present, should be placed below the 640x240 canvas. (Tuan did this for my solutions.)

sidwellr commented 6 hours ago

All looks good to me! Thanks.

You may want to add a statement to the process in README that controls, if present, should be placed below the 640x240 canvas. (Tuan did this for my solutions.)