Skip to content

Commit 8b5348c

Browse files
authored
feat: adds Jared's code samples (#185)
* feat: adds is_prime implementations in Python, JS, and TS * feat:adds reafactored .py, .js, .ts files to reduce the logic per function * feat: adds test_is_prime for all files. Renamed ts file to avoid errors with .js files Compiled test_is_primeTS.ts into .js file. * feat: adds more context to README.md file * feat: adds context into README.md
1 parent 16a45ec commit 8b5348c

File tree

9 files changed

+294
-0
lines changed

9 files changed

+294
-0
lines changed

lesson_04/jarededge/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## Explanation
2+
3+
I know we were instruected to only implement 2 languages but I decided to do 3 to practice writing in these languages as much as I could. Each version of the isPrime function follows the same logical structure, broken down into small helper functions for clarity and maintainability. The logic checks if a number is less than or equal to 1, whether it's 2 or 3, if it's divisible by 2 or 3, and finally uses the 6k ± 1 optimization to check for other factors efficiently.
4+
5+
## Differences for programs
6+
7+
Function Declarations & Syntax:
8+
9+
- Python uses def with colons and indentation.
10+
11+
- JavaScript and TypeScript use function and curly braces.
12+
13+
- TypeScript adds type annotations (e.g., n: number, : boolean).
14+
15+
## Type Safety:
16+
17+
- Python is dynamically typed but can include type hints.
18+
19+
- JavaScript is fully dynamic and prone to runtime type errors.
20+
21+
- TypeScript offers compile-time type checking for more robust code.
22+
23+
##Booleans & Print Statements:
24+
25+
- Python uses True/False and print()
26+
27+
- JS/TS use true/false and console.log() (uses capitol letters)
28+
29+
## Refactoring Practice:
30+
31+
- All three versions refactor logic into smaller single-responsibility functions
32+
33+
- This improves readability and testability
34+
35+
Tooling Differences:
36+
37+
- TypeScript requires a compiler (tsc) to convert code into JavaScript before execution
38+
39+
- Python and JavaScript can run directly via interpreters (e.g., python, node)
40+
41+
42+
## Differences for tests
43+
44+
45+
## Test Frameworks & Syntax
46+
47+
- Python (unittest): Uses the built-in unittest framework
48+
49+
- JavaScript (Node assert): No framework; uses Node’s built-in assert with a manual runTests() function.
50+
51+
- TypeScript (compiled JS): Compiled output JS that uses a custom assertEqual helper and a manual runTestsTS(); not using a TS-aware test runner in this artifact.
52+
53+
- All 3 tests used some sort of assert helper
54+
55+
56+
## Output
57+
58+
- Python prints a structured summary (dots/letters, failures)
59+
- JavaScript and TypeScript Prints a message of completion or failure

lesson_04/jarededge/is_prime.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function isLessThanOrEqualToOne(n) {
2+
return n <= 1;
3+
}
4+
5+
function isTwoOrThree(n) {
6+
return n === 2 || n === 3;
7+
}
8+
9+
function isDivisibleByTwoOrThree(n) {
10+
return n % 2 === 0 || n % 3 === 0;
11+
}
12+
13+
function hasOtherDivisors(n) {
14+
let i = 5;
15+
while (i * i <= n) {
16+
if (n % i === 0 || n % (i + 2) === 0) {
17+
return true;
18+
}
19+
i += 6;
20+
}
21+
return false;
22+
}
23+
24+
function isPrime(n) {
25+
if (isLessThanOrEqualToOne(n)) return false;
26+
if (isTwoOrThree(n)) return true;
27+
if (isDivisibleByTwoOrThree(n)) return false;
28+
return !hasOtherDivisors(n);
29+
}
30+
31+
module.exports = {isPrime};
32+
33+
// Testing before JUnit tests
34+
const testNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 19, 21, 23, 26];
35+
testNumbers.forEach((num) => {
36+
console.log(`${num} is prime? ${isPrime(num)}`);
37+
});

lesson_04/jarededge/is_prime.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def is_less_than_or_equal_to_one(n: int) -> bool:
2+
return n <= 1
3+
4+
5+
def is_two_or_three(n: int) -> bool:
6+
return n == 2 or n == 3
7+
8+
9+
def is_divisible_by_two_or_three(n: int) -> bool:
10+
return n % 2 == 0 or n % 3 == 0
11+
12+
13+
def has_other_divisors(n: int) -> bool:
14+
i = 5
15+
while i * i <= n:
16+
if n % i == 0 or n % (i + 2) == 0:
17+
return True
18+
i += 6
19+
return False
20+
21+
22+
def is_prime(n: int) -> bool:
23+
if is_less_than_or_equal_to_one(n):
24+
return False
25+
if is_two_or_three(n):
26+
return True
27+
if is_divisible_by_two_or_three(n):
28+
return False
29+
return not has_other_divisors(n)
30+
31+
32+
# Testing the program before JUnit tests
33+
if __name__ == "__main__":
34+
test_numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 19, 21, 23, 26]
35+
for num in test_numbers:
36+
print(f"{num} is prime? {is_prime(num)}")

lesson_04/jarededge/is_primeTS.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.isPrimeTS = isPrimeTS;
4+
function isLessThanOrEqualToOneTS(n) {
5+
return n <= 1;
6+
}
7+
function isTwoOrThreeTS(n) {
8+
return n === 2 || n === 3;
9+
}
10+
function isDivisibleByTwoOrThreeTS(n) {
11+
return n % 2 === 0 || n % 3 === 0;
12+
}
13+
function hasOtherDivisorsTS(n) {
14+
var i = 5;
15+
while (i * i <= n) {
16+
if (n % i === 0 || n % (i + 2) === 0) {
17+
return true;
18+
}
19+
i += 6;
20+
}
21+
return false;
22+
}
23+
function isPrimeTS(n) {
24+
if (isLessThanOrEqualToOneTS(n))
25+
return false;
26+
if (isTwoOrThreeTS(n))
27+
return true;
28+
if (isDivisibleByTwoOrThreeTS(n))
29+
return false;
30+
return !hasOtherDivisorsTS(n);
31+
}
32+
// Testing before JUnit tests
33+
var testNumbersTS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 19, 21, 23, 26];
34+
testNumbersTS.forEach(function (num) {
35+
console.log("".concat(num, " is prime? ").concat(isPrimeTS(num)));
36+
});

lesson_04/jarededge/is_primeTS.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
4+
5+
6+
7+
8+
function isLessThanOrEqualToOneTS(n: number): boolean {
9+
return n <= 1;
10+
}
11+
12+
function isTwoOrThreeTS(n: number): boolean {
13+
return n === 2 || n === 3;
14+
}
15+
16+
function isDivisibleByTwoOrThreeTS(n: number): boolean {
17+
return n % 2 === 0 || n % 3 === 0;
18+
}
19+
20+
function hasOtherDivisorsTS(n: number): boolean {
21+
let i = 5;
22+
while (i * i <= n) {
23+
if (n % i === 0 || n % (i + 2) === 0) {
24+
return true;
25+
}
26+
i += 6;
27+
}
28+
return false;
29+
}
30+
31+
export function isPrimeTS(n: number): boolean {
32+
if (isLessThanOrEqualToOneTS(n)) return false;
33+
if (isTwoOrThreeTS(n)) return true;
34+
if (isDivisibleByTwoOrThreeTS(n)) return false;
35+
return !hasOtherDivisorsTS(n);
36+
}
37+
38+
// Testing before JUnit tests
39+
const testNumbersTS: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 19, 21, 23, 26];
40+
41+
testNumbersTS.forEach((num) => {
42+
console.log(`${num} is prime? ${isPrimeTS(num)}`);
43+
});
44+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const assert = require('assert');
2+
const { isPrime } = require('./is_prime');
3+
4+
function runTests(){
5+
assert.strictEqual(isPrime(2), true);
6+
assert.strictEqual(isPrime(3), true);
7+
assert.strictEqual(isPrime(17), true);
8+
assert.strictEqual(isPrime(23), true);
9+
assert.strictEqual(isPrime(1), false);
10+
assert.strictEqual(isPrime(4), false);
11+
assert.strictEqual(isPrime(9), false);
12+
assert.strictEqual(isPrime(16), false);
13+
14+
console.log("All the test cases passed");
15+
16+
17+
}
18+
19+
runTests();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from is_prime import is_prime
3+
4+
5+
class TestisPrime(unittest.TestCase):
6+
def test_primes(self):
7+
self.assertTrue(is_prime(2))
8+
self.assertTrue(is_prime(3))
9+
self.assertTrue(is_prime(17))
10+
self.assertTrue(is_prime(23))
11+
12+
def test_non_primes(self):
13+
self.assertFalse(is_prime(1))
14+
self.assertFalse(is_prime(4))
15+
self.assertFalse(is_prime(9))
16+
self.assertFalse(is_prime(16))
17+
18+
19+
if __name__ == "__main__":
20+
unittest.main()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
var is_primeTS_1 = require("./is_primeTS");
4+
function assertEqual(actual, expected, label) {
5+
if (actual !== expected) {
6+
throw new Error("".concat(label, ") Test failed. Expected ").concat(expected, ", got ").concat(actual));
7+
}
8+
}
9+
function runTestsTS() {
10+
assertEqual((0, is_primeTS_1.isPrimeTS)(2), true, "prime");
11+
assertEqual((0, is_primeTS_1.isPrimeTS)(3), true, "prime");
12+
assertEqual((0, is_primeTS_1.isPrimeTS)(17), true, "prime");
13+
assertEqual((0, is_primeTS_1.isPrimeTS)(23), true, "prime");
14+
assertEqual((0, is_primeTS_1.isPrimeTS)(1), false, "prime");
15+
assertEqual((0, is_primeTS_1.isPrimeTS)(4), false, "prime");
16+
assertEqual((0, is_primeTS_1.isPrimeTS)(9), false, "prime");
17+
assertEqual((0, is_primeTS_1.isPrimeTS)(16), false, "prime");
18+
console.log("All the TS test passed");
19+
}
20+
runTestsTS();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { isPrimeTS } from "./is_primeTS";
2+
3+
function assertEqual(actual: boolean, expected: boolean, label: string){
4+
if (actual !== expected){
5+
throw new Error (`${label}) Test failed. Expected ${expected}, got ${actual}`);
6+
7+
}
8+
}
9+
10+
function runTestsTS(){
11+
assertEqual(isPrimeTS(2), true, "prime");
12+
assertEqual(isPrimeTS(3), true, "prime");
13+
assertEqual(isPrimeTS(17), true, "prime");
14+
assertEqual(isPrimeTS(23), true, "prime");
15+
assertEqual(isPrimeTS(1), false, "prime");
16+
assertEqual(isPrimeTS(4), false, "prime");
17+
assertEqual(isPrimeTS(9), false, "prime");
18+
assertEqual(isPrimeTS(16), false, "prime");
19+
console.log("All the TS test passed");
20+
21+
}
22+
23+
runTestsTS();

0 commit comments

Comments
 (0)