Skip to content

Commit fee9b89

Browse files
txjackson252Trinitie Jackson
andauthored
feat: adds Trinity's lesson_04 homework and stretch (#172)
* modiified jest.config * add files * everything * chore: cleaning useless code * chore: changing directory and instructions * chore: redo instructions * chore: rearrange files and change dir and instructions wq git push git add . clear * chore: re-commit instructions --------- Co-authored-by: Trinitie Jackson <[email protected]>
1 parent 61a2845 commit fee9b89

File tree

8 files changed

+193
-0
lines changed

8 files changed

+193
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
## Python implementation
2+
3+
```python
4+
def is_prime(n):
5+
return n > 1 and all(n % i for i in range(2, int(n**0.5) + 1))
6+
7+
# Example usage:
8+
print(is_prime(-5)) # False
9+
print(is_prime(17)) # True
10+
```
11+
12+
## TypeScript implementation
13+
14+
```typescript
15+
function isPrime(n: number): boolean {
16+
return n > 1 && Array.from({ length: Math.floor(Math.sqrt(n)) - 1 }, (_, i) => i + 2)
17+
.every(i => n % i !== 0);
18+
}
19+
20+
// Example usage:
21+
console.log(isPrime(17)); // true
22+
console.log(isPrime(18)); // false
23+
```
24+
25+
## Similarities vs Differences
26+
27+
28+
29+
### Similarities
30+
- They both use return `n > 1`, which helps to ensure that numbers less than or equal to 1 are not prime.
31+
- Both languages use `i` as the variable for testing divisors of `n`.
32+
- While the methods are different, they both have a way of creating a loop or a range of numbers.
33+
34+
35+
### Differences
36+
- The comment style in both languages is different. Python uses hashtags while TypeScript uses slashes.
37+
- Python uses `n**0.5`, while TypeScript uses `Math.sqrt(n)` to call the sqaure root.
38+
- Python has range , which produces a sequence of integers. However, while TypeScript does not have `range` built in, it does have `Array`, which you can use to generate the sequence.
39+
40+
## Quiz Instructions
41+
42+
### Python Quiz
43+
44+
1. Open the project in **VS Code**
45+
2. Navigate to the [prime_python] directory.
46+
```bash
47+
cd lesson_04/trinitiejackson/prime_python
48+
```
49+
3. Check to make sure you have Python 3 installed
50+
```bash
51+
python3 --version
52+
```
53+
If not, do:
54+
```bash
55+
brew install python3
56+
```
57+
3. Open the [prime_test] file.
58+
4. Update the code to determine whether a number is prime.
59+
5. When ready to test, run the following command in the [prime_python] directory using the terminal:
60+
```bash
61+
python3 prime_test.py
62+
```
63+
### TypeScript Quiz
64+
65+
1. Make sure to sync your fork to pull in the latest changes.
66+
2. Open the project in **VS Code**.
67+
3. Navigate to the [prime_typescript] directory and install the required dependencies.
68+
```bash
69+
cd lesson_04/trinitiejackson/prime_typescript
70+
npm install
71+
```
72+
4. Open the [prime_test.ts] file located in the [prime_typescript/src/] directory.
73+
5. Update the code to determine whether a number is prime.
74+
6. When ready to test, run the following command in the terminal:
75+
```bash
76+
npm test
77+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
def is_prime(n):
3+
return n > 1 and all(n % i for i in range(2, int(n**0.5) + 1))
4+
5+
6+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from prime import is_prime
3+
4+
class TestPrime(unittest.TestCase):
5+
def test_prime_numbers(self):
6+
self.assertTrue(is_prime(5))
7+
self.assertFalse(is_prime(-5))
8+
9+
10+
if __name__ == '__main__':
11+
unittest.main()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { createDefaultPreset } = require("ts-jest");
2+
3+
const tsJestTransformCfg = createDefaultPreset().transform;
4+
5+
/** @type {import('ts-jest').JestConfigWithTsJest} **/
6+
module.exports = {
7+
testEnvironment: "node",
8+
transform: {
9+
...tsJestTransformCfg,
10+
},
11+
preset: 'ts-jest',
12+
testEnvironment: 'node',
13+
testMatch: ['**/src/**/*_test.ts'],
14+
transform: {
15+
'^.+\\.tsx?$': 'ts-jest'
16+
}
17+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "prime_typescript",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"test": "jest"
6+
},
7+
"devDependencies": {
8+
"@types/jest": "^30.0.0",
9+
"@types/mocha": "^10.0.10",
10+
"@types/node": "^24.2.1",
11+
"jest": "^30.0.5",
12+
"ts-jest": "^29.4.1",
13+
"typescript": "^5.0.0"
14+
},
15+
"compilerOptions": {
16+
"target": "esnext",
17+
"module": "commonjs",
18+
"strict": true,
19+
"esModuleInterop": true,
20+
"forceConsistentCasingInFileNames": true,
21+
"skipLibCheck": true,
22+
"types": [
23+
"jest",
24+
"node"
25+
]
26+
}
27+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function isPrime(n: number): boolean {
2+
return n > 1 && Array.from({ length: Math.floor(Math.sqrt(n)) - 1 }, (_, i) => i + 2)
3+
.every(i => n % i !== 0);
4+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { isPrime } from "./prime";
2+
3+
describe("isPrime (super-condensed)", () => {
4+
test.each([2])(
5+
"returns true for prime number %i",
6+
(n) => {
7+
expect(isPrime(n)).toBe(true);
8+
}
9+
);
10+
11+
test.each([6])(
12+
"returns false for non-prime number %i",
13+
(n) => {
14+
expect(isPrime(n)).toBe(false);
15+
}
16+
);
17+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"compilerOptions": {
3+
4+
"esModuleInterop": true,
5+
"forceConsistentCasingInFileNames": true,
6+
"types": ["jest", "node"],
7+
8+
9+
// Environment Settings
10+
11+
"module": "nodenext",
12+
"target": "esnext",
13+
14+
15+
// Other Outputs
16+
"sourceMap": true,
17+
"declaration": true,
18+
"declarationMap": true,
19+
20+
// Stricter Typechecking Options
21+
"noUncheckedIndexedAccess": true,
22+
"exactOptionalPropertyTypes": true,
23+
24+
// Style Options
25+
// Recommended Options
26+
"strict": true,
27+
"jsx": "react-jsx",
28+
"verbatimModuleSyntax": false,
29+
"isolatedModules": true,
30+
"noUncheckedSideEffectImports": true,
31+
"moduleDetection": "force",
32+
"skipLibCheck": true,
33+
}
34+
}

0 commit comments

Comments
 (0)