Skip to content

Commit 60127c1

Browse files
Merge branch 'code-differently:main' into feat/lesson06/expression_calculator
2 parents 9944e25 + 8b5348c commit 60127c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+13160
-3
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# https://help.github.com/articles/dealing-with-line-endings/
3+
#
4+
# Linux start script should use lf
5+
/gradlew text eol=lf
6+
7+
# These are Windows script files and should use crlf
8+
*.bat text eol=crlf
9+
10+
# Binary files should be left untouched
11+
*.jar binary
12+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore Gradle project-specific cache directory
2+
.gradle
3+
4+
# Ignore Gradle build output directory
5+
build
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
## Python implementation
2+
3+
```python
4+
def is_prime(num):
5+
if num <= 1:
6+
return False
7+
for val in range(2,int(num**(1/2))+1):
8+
if num % val == 0:
9+
return False
10+
return True
11+
12+
if __name__ == "__main__":
13+
print(4, is_prime(4))
14+
print(13, is_prime(13))
15+
for num in range(2, 101):
16+
print(str(num) +": "+ str(is_prime(num)))
17+
```
18+
19+
## Java implementation
20+
21+
```java
22+
public class Prime {
23+
public static void main(String[] args) {
24+
System.out.println(4 + " " + isPrime(4));
25+
System.out.println(13 + " " + isPrime(13));
26+
27+
// Checks each number between 2 and 100 to see if they're prime.
28+
for (int num = 2; num <= 100; num++) {
29+
System.out.println(num + ": " + isPrime(num));
30+
}
31+
}
32+
33+
/**
34+
* Checks if num is a prime number.
35+
*
36+
* @param num the number to check
37+
* @return true if num is prime, false otherwise
38+
*/
39+
public static boolean isPrime(int num) {
40+
if (num <= 1){
41+
return false;
42+
}
43+
for (int val = 2; val <= Math.sqrt(num); val++) {
44+
if (num % val == 0) {
45+
return false;
46+
}
47+
}
48+
return true;
49+
}
50+
}
51+
52+
```
53+
54+
## Explanation
55+
In the Python file `prime.py`, the function `is_prime` takes the parameter called `num` and returns `True` or `False` depending on whether or not num is a prime number (as in numbers that are only divisble by another number that is not 1 or itself). The function runs a loop through all numbers in the range from 2 to the square root of num inclusively, divides num by each of those numbers, and if the remainder is ever 0, then false is returned immediately. If all numbers are checked without getting 0, true is returned after the loop.
56+
57+
In the Java file `Prime.java`, the function `isPrime` also takes a parameter called `num` and has the same goal as the python iteration. The java version returns `true` when given a prime number and `false` when given a non-prime using the same logic as the python version.
58+
59+
### Differences
60+
61+
1. **Syntax**:
62+
- In Python, functions are defined using the `def` keyword. Meanwhile in Java the function is defined with `public static <return type>`
63+
- Python uses `True` and `False` for boolean values, while Java uses `true` and `false`.
64+
- For loop in python are written `for x in range(2, num**(1/2)+1).` Since the end point is exclusive, we need to add one to make sure that the square root of the num is included.
65+
- The java for loop is written like `for (int x = 2; x<=Math.sqrt(num); x++)`. This creates x with a value of 2, loops until it's greater than the root of num and adds one to x for the next iteration.
66+
67+
2. **Type Coercion**:
68+
- JavaScript has type coercion, which can sometimes lead to unexpected results if the input is not properly handled. In contrast, Python is more strict with types.
69+
70+
3. **Function Calls**:
71+
- Most function calls between Python and Java are done the same. However for printing to the console, Python uses `print()` and Java uses `System.out.println()`.
72+
73+
## Test Cases
74+
75+
Both the Python and Java implementations include automated tests to verify that the `is_prime` function works correctly.
76+
77+
- **Python (`prime_test.py`)**:
78+
The test file uses Python’s `unittest` framework. It checks if the function returns `True` for prime numbers and `False` for non-prime numbers.
79+
```python
80+
self.assertTrue(is_prime(13)) # Checks that 13 is correctly identified as a prime number.
81+
```
82+
83+
- **Java (`isPrimeTest.java`)**:
84+
The test file uses JUnit. It also checks that the function returns the correct answers.
85+
```java
86+
assertTrue(Prime.isPrime(13)); // Checks that 13 is correctly identified as a prime number.
87+
```
88+
89+
### How to run the tests
90+
91+
- **Python:**
92+
From the `danielsonadjocy` directory, run:
93+
```bash
94+
python -m unittest app.src.test.python.prime_test
95+
```
96+
97+
- **Java:**
98+
Make sure JUnit is available. From the `danielsonadjocy` directory, compile and run:
99+
```bash
100+
gradle test
101+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This generated file contains a sample Java application project to get you started.
5+
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/9.0.0/userguide/building_java_projects.html in the Gradle documentation.
6+
* This project uses @Incubating APIs which are subject to change.
7+
*/
8+
9+
plugins {
10+
// Apply the application plugin to add support for building a CLI application in Java.
11+
id 'application'
12+
}
13+
14+
repositories {
15+
// Use Maven Central for resolving dependencies.
16+
mavenCentral()
17+
}
18+
19+
dependencies {
20+
// This dependency is used by the application.
21+
implementation libs.guava
22+
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
23+
}
24+
25+
testing {
26+
suites {
27+
// Configure the built-in test suite
28+
test {
29+
// Use JUnit Jupiter test framework
30+
useJUnitJupiter('5.12.1')
31+
}
32+
}
33+
}
34+
35+
// Apply a specific Java toolchain to ease working on different environments.
36+
java {
37+
toolchain {
38+
languageVersion = JavaLanguageVersion.of(22)
39+
}
40+
}
41+
42+
application {
43+
// Define the main class for the application.
44+
mainClass = 'org.example.App'
45+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Prime {
2+
public static void main(String[] args) {
3+
System.out.println(4 + " " + isPrime(4));
4+
System.out.println(13 + " " + isPrime(13));
5+
6+
// Checks each number between 2 and 100 to see if they're prime.
7+
for (int num = 2; num <= 100; num++) {
8+
System.out.println(num + ": " + isPrime(num));
9+
}
10+
}
11+
12+
/**
13+
* Checks if num is a prime number.
14+
*
15+
* @param num the number to check
16+
* @return true if num is prime, false otherwise
17+
*/
18+
public static boolean isPrime(int num) {
19+
if (num <= 1){
20+
return false;
21+
}
22+
for (int val = 2; val <= Math.sqrt(num); val++) {
23+
if (num % val == 0) {
24+
return false;
25+
}
26+
}
27+
return true;
28+
}
29+
}

lesson_04/danielsonadjocy/app/src/main/python/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def is_prime(num):
2+
"""
3+
Checks if num is a prime number.
4+
5+
Args:
6+
num (int): The number to check.
7+
8+
Returns:
9+
bool: True if num is prime, False otherwise.
10+
"""
11+
if num <= 1:
12+
return False
13+
for val in range(2,int(num**(1/2))+1):
14+
if num % val == 0:
15+
return False
16+
return True
17+
18+
if __name__ == "__main__":
19+
print(4, is_prime(4))
20+
print(13, is_prime(13))
21+
# Checks each number between 2 and 100 to see if they're prime.
22+
for num in range(2, 101):
23+
print(str(num) +": "+ str(is_prime(num)))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import static org.junit.jupiter.api.Assertions.assertTrue;
2+
import static org.junit.jupiter.api.Assertions.assertFalse;
3+
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class isPrimeTest {
8+
9+
@Test
10+
void testPrime() {
11+
int[] nums = {2, 3, 7, 13, 71, 79, 613};
12+
for (int num : nums){
13+
assertTrue(Prime.isPrime(num), num + " is a prime number");
14+
}
15+
}
16+
17+
@Test
18+
void testnotPrime() {
19+
int[] nums = {1, 4, 6, 14, 44, 50, 82};
20+
for (int num : nums){
21+
assertFalse(Prime.isPrime(num), num + " is not a prime number");
22+
}
23+
}
24+
}

lesson_04/danielsonadjocy/app/src/test/python/__init__.py

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import unittest
2+
from app.src.main.python.prime import is_prime
3+
4+
5+
class TestPrime(unittest.TestCase):
6+
"""Unit tests for the prime number checker."""
7+
8+
def test_is_prime(self):
9+
for x in [2, 3, 7, 13, 71, 79, 613]:
10+
self.assertTrue(is_prime(x), str(x) + " should be prime")
11+
12+
13+
def test_is_not_prime(self):
14+
for x in [1, 4, 6, 14, 44, 50, 82]:
15+
self.assertFalse(is_prime(x), str(x) + " is not prime")
16+
17+
18+
if __name__ == "__main__":
19+
unittest.main()

0 commit comments

Comments
 (0)