-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'lesson_07' of https://github.com/A1-4U2T1NN/code-differ…
…ently-24-q4 into lesson_07
- Loading branch information
Showing
16 changed files
with
635 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Check Lesson 05 Quiz Pull Request | ||
|
||
on: | ||
pull_request: | ||
branches: [ "main" ] | ||
paths: | ||
- "lesson_05/quiz/**" | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Use Node.js | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: '20.x' | ||
|
||
- name: Build Shared Lib with Node.js | ||
working-directory: ./lib/typescript/codedifferently-instructional | ||
run: npm ci | ||
|
||
- name: Build Lesson 06 with Node.js | ||
working-directory: ./lesson_05/quiz | ||
run: | | ||
npm ci | ||
npm run check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Prime Number Finder (Python and JavaScript) # | ||
|
||
This project demonstrates how to find prime numbers between 1 and 100 using two programming languages: Python and JavaScript. Each function uses a for loop to check whether a number is prime, and both include inline comments to explain the code step by step. Additionally, there are detailed explanations after each function. | ||
|
||
## Prime Number Finder in Python ## | ||
``` | ||
# Function to find prime numbers between 1 and 100 in Python | ||
def find_prime_num_py(): | ||
# Loop through numbers from 2 to 100 | ||
for num in range(2, 101): | ||
is_prime = True # Assume the number is prime | ||
# Check if num is divisible by any number between 2 and its square root | ||
for i in range(2, int(num**0.5) + 1): | ||
if num % i == 0: | ||
is_prime = False # If divisible, not a prime number | ||
break # Exit the loop early | ||
# If is_prime is still true, num is a prime number | ||
if is_prime: | ||
print(num) # Output the prime number | ||
# Call the Python function | ||
find_prime_num_py() | ||
``` | ||
### Python Explanation: ### | ||
The Python function, find_prime_num_py, uses a for loop to iterate through the numbers between 2 and 100. The is_prime variable is set to True for each number. Inside the inner loop, we check if the number is divisible by any integer between 2 and the square root of the number. If the number is divisible, it is marked as not prime, and we break out of the inner loop to stop further unnecessary checks. The square root limit ensures better performance. When the number is confirmed to be prime, it is printed to the console. | ||
|
||
|
||
## Prime Number Finder in JavaScript ## | ||
``` | ||
// Function to find prime numbers between 1 and 100 in JavaScript | ||
function findPrimeNumberJS() { | ||
// Loop through numbers from 2 to 100 | ||
for (let num = 2; num <= 100; num++) { | ||
let isPrime = true; // Assume the number is prime | ||
// Check if num is divisible by any number between 2 and its square root | ||
for (let i = 2; i <= Math.sqrt(num); i++) { | ||
if (num % i === 0) { | ||
isPrime = false; // If divisible, not a prime number | ||
break; // Exit the loop early | ||
} | ||
} | ||
// If isPrime is still true, num is a prime number | ||
if (isPrime) { | ||
console.log(num); // Output the prime number | ||
} | ||
} | ||
} | ||
// Call the JavaScript function | ||
findPrimeNumberJS(); | ||
``` | ||
|
||
### JavaScript Explanation: ### | ||
In this JavaScript function, findPrimeNumberJS, we use a for loop to iterate over the numbers from 2 to 100. The isPrime variable initially assumes that each number is prime. For each number num, we check if it is divisible by any number from 2 to the square root of num. If the number is divisible, it is marked as not prime by setting isPrime to false, and we break out of the inner loop to save time. If the number remains prime after the check, it is printed to the console. The square root check ensures we minimize the number of iterations for efficiency. | ||
|
||
## Similarities and Differences Between Python and JavaScript Prime Number Functions ## | ||
|
||
### Similarities | ||
|
||
* Logic (Using Square Roots to Check Primes): Both functions in Python and JavaScript use the same fundamental logic to determine if a number is prime. By checking divisors only up to the square root of the number, we can efficiently reduce the number of checks. This optimization works in both languages because if a number has a divisor greater than its square root, it must also have a corresponding smaller divisor, which would have been checked already. | ||
|
||
* Looping Structure: Both functions use a for loop to iterate over potential divisors, starting from 2. While the exact syntax differs, the purpose is the same—to test whether the number is divisible by any number within this range. If no divisor is found, the number is prime. | ||
|
||
|
||
### Differences: | ||
|
||
* Syntax: Python uses range() to generate numbers in a loop, while JavaScript directly declares the loop variable and sets its conditions. Python also uses indentation to define code blocks, while JavaScript uses curly braces {}. | ||
|
||
* Case Sensitivity: Python commonly uses snake_case for function and variable names (e.g., is_prime), while JavaScript typically uses camelCase (e.g., isPrime). | ||
|
||
* Variable Declarations: In Python, variables are declared by simply assigning a value to them (e.g., num = 5). In JavaScript, variables must be explicitly declared using let, const, or var (e.g., let num = 5). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
```Javascript | ||
#Javascript | ||
|
||
function findPrimes(numberToCheck) { // A machine that helps find prime numbers | ||
|
||
if (numberToCheck <=1){ | ||
return `${numberToCheck} is not a prime number.`; //any number that is less or equal to 1 it is NOT a prime number | ||
} | ||
let isPrime = true; //I am start with assuming the number is prime | ||
|
||
|
||
for (let factor = 2; factor <= Math.floor(numberToCheck / 2); factor++) { //this is another loop but it checks to see if the number is divisible by other numbers. | ||
if (numberToCheck % factor === 0) { // this is checking to see if the number can divide evenly and if so then it is not a prime number | ||
isPrime = false; // states that the number is not prime if it comes out as 0 (should have a remainder) | ||
break; //states that it can STOP the loop once it finds a 0, no need to keep going through | ||
} //this is closing the loop | ||
} | ||
|
||
if (isPrime) { //if said number is still true that means that we did not find any number that is divided evenly so it is prime | ||
return `${numberToCheck} is a prime number.`; //if the numbe is prime it will say^^ | ||
} else{ | ||
return `${numberToCheck} is not a prime number.`; // if it is NOT prime it will say so | ||
} | ||
} //closing the loop of if it is a prime number or not | ||
|
||
const input = process.argv[2]; // telling the computer to save the 3rd thing you type | ||
|
||
let number = parseInt(input); // if it types a word the computer does the math and makes it a number | ||
|
||
if (isNaN(number)) { // make sure you type a number | ||
console.log("Please enter a valid number."); // letting the user know you have to type a number | ||
} else { | ||
console.log(findPrimes(number)); //now the computer can check if it is prime or not | ||
} | ||
|
||
// credit from Coding with John youtube video https://www.youtube.com/watch?v=I9-3drnfyp0 and Chatgpt for a explanation of things I still might have been confused about | ||
``` | ||
|
||
```python | ||
# Python | ||
# this is a function that will help us find all the prime numbers | ||
def find_primes(number_to_check): | ||
if number_to_check <= 1: # this is an empty list for now until we run the test for all the prime numbers we find | ||
return f"{number_to_check} is not a prime number." | ||
|
||
is_prime = True # I am saying that it is a prime until I find out it is not | ||
|
||
# checks to see if the number can be divided by a smaller number evenly | ||
for factor in range(2, number_to_check // 2 + 1): | ||
if number_to_check % factor == 0: # trying to see if there is a remainder after the divison and if it is equal to zero | ||
is_prime = False # if it is equal to zero it is flase meaning it is not prime | ||
break # again it means STOP | ||
if is_prime: # after checking all | ||
return f"{number_to_check} is a prime number." | ||
else: | ||
return f"{number_to_check} is not a prime number." | ||
|
||
number = int(input("Enter a number to check to see if its prime: ")) | ||
print(find_primes(number)) | ||
|
||
``` | ||
|
||
## Explanation | ||
My first thought was to use Javascript and html because those were the 2 languages that I was familiar with. I did some research and quickly came to the realization that html would not be the most effective. That's when I found out that I should use Python and Javascript. | ||
Python is known for how easy it is to read and how simple it is. But is super space indentation sensitive. Whereas Javascript is a little more complex because it uses different characters, which makes it a little harder to understand. | ||
|
||
Similarities: Both Javascript and Python are finding prime numbers between 2 and 100 even though in pyton it says 101. That is becuase we did not plug in 101 we stopped right before it. | ||
|
||
Diffreneces: A diffrence that Javascript uses let for declaring variables while python uses simplier words becuase you do not need a keyword | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
## Javascript | ||
```Javascript | ||
|
||
function isPrime (num) { | ||
/* first condtion is checking if 7 is less than or equal to 1. */ | ||
if (num <= 1) return false; | ||
/*checking if the number has a remainder or 0 if divided 2 */ | ||
for (let i = 2; i < num; i++) { | ||
if (num% i === 0) | ||
return false; | ||
} | ||
return true;} | ||
|
||
|
||
console.log(isPrime (7)) | ||
``` | ||
|
||
## Java | ||
```Java | ||
|
||
public class PrimeChecker { | ||
|
||
public static boolean isPrime(int num) { | ||
// First condition: check if num is less than or equal to 1 | ||
if (num <= 1) return false; | ||
|
||
// Check for factors from 2 to num - 1 | ||
for (int i = 2; i < num; i++) { | ||
if (num % i == 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(isPrime(11)); // Test the function | ||
} | ||
} | ||
``` | ||
|
||
## Explination | ||
|
||
In javascript, the function "isPrime" is checking if the number is less than or equal to 1. if so the fuction will fail. If it passes it will run the next fuction to see if the input has a remainder of 0 if divided by 2. | ||
|
||
In Java, the function "isPrime(int num)" is checking if the number is less than or equal to 1. if the number is indeed greater than or less than it will return false. if true the function will run through a for loop to determine if divided by 2 will the remainder be 0. | ||
|
||
|
||
### Differences | ||
|
||
Java and Javascript have numerous similarites but arent related. Java runs a more strict ruleset with certain elements such capital vs lower case letters where as javascript is more lenient in its functionality. In javascript the print out to test the fuction is console.log where as in Java the print out is system.out.println | ||
|
||
|
||
#### Citing | ||
|
||
I got my Javascript code from a youtube video https://www.youtube.com/watch?v=ZdoiS_qUOSE I took that code and pasted it into https://playcode.io/ to test if the code worked. I had to make a few changes through trial and error. Once i got the correct code, i asked chatGPT to convert the javascript into java. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
|
||
```Java | ||
static boolean checkPrime(int num) { | ||
boolean prime = false; | ||
if (num == 0 || num == 1) { | ||
prime = true; | ||
} | ||
for (int i = 2; i <= num / 2; i++) { | ||
if (num % i == 0) { | ||
prime = true; | ||
break; | ||
}git | ||
} | ||
return !prime; | ||
} | ||
|
||
# Example usage: | ||
print(checkPrime(4)) # Output: false | ||
print(checkPrime(7)) # Output: true | ||
|
||
## JavaScript implementation | ||
|
||
```javascript | ||
function checkPrime(num) { | ||
let prime = false; | ||
if (num === 0 || num === 1) { | ||
prime = true; | ||
} | ||
for (let i = 2; i <= num / 2; i++) { | ||
if (num % i === 0) { | ||
prime = true; | ||
break; | ||
} | ||
} | ||
return !prime; | ||
} | ||
// Example usage: | ||
console.log(checkPrime(4)); // Output: false | ||
console.log(checkPrime(7)); // Output: true | ||
|
||
|
||
## Explanation | ||
|
||
The Javascript implementation uses a function named `checkPrime` that takes a single argument `number`. It returns `True` if the number is prime, otherwise, it returns `False`. | ||
|
||
The Java implementation uses a function named `checkPrime` that also takes a single argument `int number`. It returns `true` if the number is Prime and `false` otherwise. | ||
|
||
Java uses `true` and `talse` for boolean values and JavaScript uses `true` and `false`. | ||
|
||
### Differences | ||
|
||
**Function Calls**: | ||
- The syntax for calling functions and printing to the console/output is slightly different. Java uses `print()`, while JavaScript uses `console.log()`. | ||
|
||
**Citation | ||
https://www.programiz.com/java-programming/online-compiler/?ref=8039b165git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
## Java Implementation | ||
```Java | ||
public class Main { | ||
public static void main(String[]args){ | ||
int num = 19; | ||
boolean isPrime = true; | ||
for (int i = 2; i < num; i++){ | ||
if (num % i == 0){ | ||
isPrime = false; | ||
break; | ||
} | ||
} | ||
if (isPrime){ | ||
System.out.println(num + " is a Prime number."); | ||
} else { | ||
System.out.println(num + " is not a Prime number."); | ||
} | ||
} | ||
} | ||
//Had help from Chat GPT to help me understand how to make one and break it down step by step// | ||
``` | ||
## Python Implementation | ||
``` Python | ||
def is_prime(num): | ||
if num <= 1: | ||
return False | ||
|
||
for i in range(2, num): | ||
if num % i == 0: | ||
return False | ||
|
||
return True | ||
num = 19 | ||
if is_prime(num): | ||
print(f"{num} is a Prime number.") | ||
else: | ||
print(f"{num} is Not a Prime number.") | ||
|
||
## I Had Chat GPT convert this from the Java equation ## | ||
``` | ||
## Explanation | ||
Here i have the equations to find prime numbers in both Java and Python, however it is hard coded which means that if you wanted to figure out that another number was a prime number you would need to change the value in the variable num. | ||
|
||
## Differences | ||
- Java uses curly braces with `else`, while python uses a colon with `else` to define the blocks of code. | ||
- Java has to use `public static boolean` to define the method, as in this Python equation does not use any class to define its method since it does not have one in this case, but instead uses a function with the keyword `def` to declare it. | ||
- Java and Python both use a `for` loop however Python uses `range` to create the sequence of numbers from 2 till num and in the Java code `for (int i = 2; i < num; i++)` you have to initialize, then have the condition, and lastly increment. | ||
## Similarities | ||
- Both Codes print out whether or not the number is prime, if you would like to find another number you just need to change the value from the variable num. | ||
- They both use some type of print to output what the solution of your value is to know whether or not your number that is inputted is prime. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Lesson 05: User Stories | ||
|
||
## Amazon User Stories | ||
|
||
### Persona - As a Amazon Seller: | ||
|
||
- **As an** Amazon seller, **I want** to have a large platform that's well known and popular **so that** I can sell my product. | ||
|
||
### Persona - As a Business Owner: | ||
|
||
- **As a** business owner **I want** to have a way to easily purchase items in bulk **so that** I can create my product. | ||
|
||
### Persona - As a Common Consumer: | ||
|
||
- **As a** common consumer **I want** to have a fast and secure package delivery **so that** I have a reliable way to order online. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Lesson 05 User Stories | ||
## User Story 1 | ||
I am a fellow consumer of **musi** which is a music app and here are a few things I would like to see | ||
- I want to be able to see my songs have a *history/record* to see a listening time and how many times I have played that song, so the app can recommend me songs similar to that due to constant listening or the opposite if I did not listen to that song. | ||
- I want to distinguish the genres and categories my songs are in, so a different section in the app to show you the genres of music you have so if you did not know what genre your favorite song was then you would have no trouble in finding it and delete a whole genre if you wanted. | ||
## User Story 2 | ||
I am the owner of an app called **Stars Of The Night**, and i have a few ideas for implemenations on my app that would make it better | ||
- I want to be able to have my consumers interact with the constellations, so if they are looking at the sign Leo it would be traced and then you can move it around how you like and hit another constellation. | ||
- I want to have it that even if the clouds are in the way the app will clear it up for you as long as it has your location while you use the app so that it can track where you are and give you a real time view with no obstruction. | ||
## User Story 3 | ||
I am a frequent user of **Youtube** however the algorithm is always changing and showing me things i do not usually watch so I tried to come up with a few ideas | ||
- I want to be able to have the algorithm change only if I like a video or save it for later. | ||
- I want to be able to watch two things at once such as one is music and the other is a tutorial I am watching so that I can possibly focus more. |
Oops, something went wrong.