diff --git a/thinkcspy/Chapter-8-Exercises/Exercises.md b/thinkcspy/Chapter-8-Exercises/Exercises.md index 9be9b81..20dcc5d 100644 --- a/thinkcspy/Chapter-8-Exercises/Exercises.md +++ b/thinkcspy/Chapter-8-Exercises/Exercises.md @@ -25,10 +25,6 @@ So that's when `bound` was created. Problem was, I initialized it to the value o That was a pretty fun problem! Alright, now to the solution: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 2 - def print_triangular_numbers(n): # your code here i = 1 @@ -65,10 +61,6 @@ Now the real struggle was working with primes larger than 2. Turns out that solu But then we get back to the whole “taking too many steps thing”. I did more research and learned that taking the square root of `n` and making that the limit for the while loop would help the program execute a lot faster than testing if x was greater than. Then I reinitialized x to 3, incremented it by 2 instead of decrementing it by 2 and wallah! I had a program that was decently efficient and effective and it passed all the built in tests! ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 3 - import math def is_prime(n): @@ -93,6 +85,50 @@ print(is_prime(4813)) ![](Exercises/1_image.png) +### Correction (Oct 15, 2023): + +While learning about loops and arrays in JavaScript, I had a similar problem where I had to print all the prime numbers for a given number. I came back to this one as a reference and realized that there was actually an issue with the way I looked for prime numbers. For some reason, the number 25 returned `True`, when it should have returned `False`. + + This is how I solved the problem in JavaScript: + +```application-javascript-env-frontend +let n = prompt("Enter a positive integer."); + +outer: +for (let i = 2; i <= n; i++){ + + for (let j = 2; j < i; j++){ + if (i % j == 0) continue outer; + } + + console.log(i); +} +``` + +Result with an input of 25: + +![](Exercises/8_image.png) + +With the above script as a reference, this is how I revised my solution for problem 3: + +```text-x-python +def is_prime(n): + # your code + + for i in range (2, n): + if n % i == 0: + return False + return True + +print(is_prime(25)) +``` + +Result: + +![](Exercises/7_image.png) + +Once again, the built in tests are not the end all be all for these problems (see chapter 9's exercises for more context). I passed the tests with the last iteration of my problem, but there was still that _one_ number (25 and maybe more), that got missed by the tests.  + Turtle Problems --------------- @@ -113,10 +149,6 @@ I'm combining problems 4 through 6 because they build on each other. ### Initial code from an earlier lesson section: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Section 4 - import random import turtle @@ -155,13 +187,9 @@ wn.exitonclick() ### My Code -Oh boy.. this section was a lot. Problem 4 was pretty easy, though. All I had to do was have them go at a random angle. That initial code looked like this: +Oh boy.. this one was a lot. Problem 4 was pretty easy. All I had to do was have them go at a random angle. That initial code looked like this: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 4 - import random import turtle @@ -201,10 +229,6 @@ wn.exitonclick() The random starting position proved to be more challenging. The calculations were all fine from the getgo. The issue was I forgot bring the pen up before I moved the turtles to a random location. I ended up moving on to the image problems before coming back to this and realizing that lol. Here's the code for that: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 5 - import random import turtle @@ -293,10 +317,6 @@ I also changed the angle from `180` to `170` and that seemed to keep the turtles Here's the final code: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 6 - import random import turtle import math @@ -388,8 +408,7 @@ wn.exitonclick() Result: -I started recording after the turtles already got moving, but you get the idea.  - +I started after the turtles already got moving, but you get the idea.  ![](Exercises/turtlemoves-2023-09-29_11.47.3.gif) Image problems @@ -400,10 +419,6 @@ The following problems used the following image and code (from an earlier lesson ![](Exercises/LutherBellPic.jpg) ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Section 11 - import image img = image.Image("luther.jpg") @@ -434,10 +449,6 @@ win.exitonclick() This was my initial solution.  ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 8 - import image img = image.Image("luther.jpg") @@ -465,15 +476,11 @@ And the result: ![](Exercises/3_image.png) -But then I had to figure out turning that into a function. I was going to go the lazy route and leave it as it was since it gave me the solution I was looking for, but then I figured actually turning this into a function would help me with the next problem.  +But then I had to figure out turning That into a function. I was going to go the lazy route and leave it as it was since it gave me the solution I was looking for, but then I figured actually turning this into a function would help me with the next problem.  Here was my final solution with the function: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 8 - import image def greyScale (red, blue, green): @@ -511,10 +518,6 @@ And the result, of course, is the same: This was my initial solution: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 9 - import image img = image.Image("luther.jpg") @@ -551,10 +554,6 @@ The result was: Close, but not quite. I needed black and white, not high contrast. But my intuition was in the ballpark. I figured that this would work better if I actually took the solution I got from the grey scale problem and then turned _that_ black and white. So, I went back to Problem 8, turned my solution into a function and then fixed up the code for this problem like this: ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 9 - import image def greyScale (red, blue, green): @@ -604,10 +603,6 @@ win.exitonclick() This didn't take too much effort since they gave me the formula. Just sharing, because I think it's cool. ```text-x-python -# Runestone.Academy thinkcspy course -# Chapter 8 -# Problem 10 - import image diff --git a/thinkcspy/Chapter-8-Exercises/Exercises/7_image.png b/thinkcspy/Chapter-8-Exercises/Exercises/7_image.png new file mode 100644 index 0000000..1b28a35 Binary files /dev/null and b/thinkcspy/Chapter-8-Exercises/Exercises/7_image.png differ diff --git a/thinkcspy/Chapter-8-Exercises/Exercises/8_image.png b/thinkcspy/Chapter-8-Exercises/Exercises/8_image.png new file mode 100644 index 0000000..2563f35 Binary files /dev/null and b/thinkcspy/Chapter-8-Exercises/Exercises/8_image.png differ