Skip to content

Commit 2cfefeb

Browse files
feat: adds Brooklyn's Python and Typescript isPrime implementation (#130)
* feat: created a branch for lesson_04 assignment * docs: added the python implementation of is_prime code * docs: added the TypeScript implementation of isPrime code * docs: added the explaination of both code implementation and what each function is doing * docs: added the differences of each code implementations * feat: I created the unit test * feat:created unit test for isPrime.ts file
1 parent 6f9d06e commit 2cfefeb

File tree

5 files changed

+391
-0
lines changed

5 files changed

+391
-0
lines changed

lesson_04/brooklynharden/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## Python Implementation
2+
3+
```python
4+
def is_prime(num):
5+
"If the Prime Number is greater than 1, print False"
6+
if num <= 1:
7+
print(False) "Any number less than or equal to one is not a prime number"
8+
9+
else:
10+
"if the number is greater than one, then we can continue with code"
11+
is_prime = True
12+
"""This loop will get us the range, 'i' already starts a 2"
13+
14+
Ex. num=9 , 9**.5=3 , 3+1=4, so the range is (2,4) not including 4, so meaning you are just checking 2 & 3 againsnt 9
15+
16+
9%2=0
17+
9%3=0
18+
"""
19+
for i in range(2, int(num ** .5) + 1):
20+
"if the num is divisible by i, its not a prime"
21+
"Ex. 9%3 == 0, its not prime"
22+
if num % i == 0:
23+
return False
24+
return True
25+
26+
## Example
27+
print(is_prime(9)) #OUTPUT: False
28+
print(is_prime(19)) #OUTPUT: True
29+
```
30+
31+
32+
## Typescript Implementation
33+
34+
```typescript
35+
function isPrime(num:number): boolean{
36+
if(num <= 1){
37+
return false;
38+
}
39+
for (let i=2; i <= Math.sqrt(num); i++){
40+
if(num % i === 0){
41+
return false;
42+
}
43+
}
44+
return true;
45+
}
46+
47+
//Example
48+
console.log(isPrime(9)) //OUTPUT: false
49+
console.log(isPrime(19)) //OUTPUT: true
50+
```
51+
52+
## Explanation
53+
54+
The Python implementation uses a function named `is_prime` that takes a single argument `num`. It returns `True` if the number is prime (i.e., when a number is greater than 1 and has no positive divisors other than 1 and itself.), otherwise, it returns `False`.
55+
56+
The TypeScript implementation uses a function named `isPrime` that also takes a single argument `num`. It returns `true` if the number is prime (using the same logic as the Python function) and `false` otherwise.
57+
58+
59+
### Differences
60+
61+
1. **Syntax**:
62+
- In Python, functions are defined using the `def` keyword, whereas in TypeScript, the `function` keyword is used.
63+
- In Python, identing matters for defining blocks, whereas Typescript doesn't require identation but it does require `{}` and semicolons `;`
64+
- Python uses `True` and `False` for boolean values, while TypeScript uses `true` and `false`.
65+
- In Python, there is no need to declare variables, whereas in TypeScript declaration need to happen either with `let` or `const`
66+
67+
2. **Equality Comparison**:
68+
- Typescript uses equalities that has 3 equal signs `===`, whereas Python equality is structured like `==`
69+
70+
3. **Function Calls**:
71+
- The syntax for calling functions and printing to the console/output is slightly different. Python uses `print()`, while TypeScript uses `console.log()`.
72+
73+
4. **NULL/NONE Values**
74+
- Typescript uses `null` or `undefined` values
75+
- Python uses the `None` value
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function isPrime(num:number): boolean{
2+
if(num <= 1){
3+
return false;
4+
}
5+
for (let i=2; i <= Math.sqrt(num); i++){
6+
if(num % i === 0){
7+
return false;
8+
}
9+
}
10+
return true;
11+
}
12+
13+
14+
// Basic tests to check the isPrime function
15+
console.assert(isPrime(2) === true, "Test Case 1 Failed: 2 should be prime");
16+
console.assert(isPrime(97) === true, "Test Case 6 Failed: 97 should be prime");
17+
18+
console.assert(isPrime(4) === false, "Test Case 7 Failed: 4 should not be prime");
19+
console.assert(isPrime(100) === false, "Test Case 11 Failed: 100 should not be prime");
20+
21+
console.assert(isPrime(0) === false, "Test Case 12 Failed: 0 should not be prime");
22+
console.assert(isPrime(-10) === false, "Test Case 15 Failed: -10 should not be prime");
23+
24+
console.log("All basic tests passed!");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import unittest
2+
3+
def is_prime(num):
4+
if num <= 1:
5+
return False
6+
7+
for i in range(2, int(num ** .5) + 1):
8+
if num % i == 0:
9+
return False
10+
return True
11+
12+
class TestIsPrime(unittest.TestCase):
13+
14+
def test_prime_numbers(self):
15+
"""Test for prime numbers."""
16+
self.assertTrue(is_prime(2))
17+
self.assertTrue(is_prime(3))
18+
self.assertTrue(is_prime(5))
19+
self.assertTrue(is_prime(7))
20+
self.assertTrue(is_prime(11))
21+
self.assertTrue(is_prime(97))
22+
23+
def test_non_prime_numbers(self):
24+
"""Test for non-prime numbers."""
25+
self.assertFalse(is_prime(4))
26+
self.assertFalse(is_prime(6))
27+
self.assertFalse(is_prime(8))
28+
self.assertFalse(is_prime(9))
29+
self.assertFalse(is_prime(100))
30+
31+
def test_edge_cases(self):
32+
"""Test for edge cases."""
33+
self.assertFalse(is_prime(0))
34+
self.assertFalse(is_prime(1))
35+
self.assertFalse(is_prime(-1))
36+
self.assertFalse(is_prime(-10))
37+
38+
if __name__ == '__main__':
39+
unittest.main()

lesson_04/brooklynharden/package-lock.json

Lines changed: 237 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "brooklynharden",
3+
"version": "1.0.0",
4+
"description": "```python def is_prime(num): \"If the Prime Number is greater than 1, print False\" if num <= 1: print(False) \"Any number less than or equal to one is not a prime number\"",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"devDependencies": {
13+
"ts-node": "^10.9.2",
14+
"typescript": "^5.9.2"
15+
}
16+
}

0 commit comments

Comments
 (0)