Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit d674e2f

Browse files
committed
new algorithms & add author
1 parent 6501de8 commit d674e2f

File tree

10 files changed

+236
-5
lines changed

10 files changed

+236
-5
lines changed

src/main/java/DavisStaircase.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* Davis has s staircases in his house and he likes to climb each staircase 1,
8+
* 2, or 3 steps at a time. Being a very precocious child, he wonders how many
9+
* ways there are to reach the top of the staircase.
10+
*
11+
* Given the respective heights for each of the s staircases in his house, find
12+
* and print the number of ways he can climb each staircase on a new line.
13+
*/
14+
15+
public class DavisStaircase {
16+
17+
public int solve(int num) {
18+
switch (num) {
19+
case 0:
20+
case 1:
21+
return 1;
22+
default:
23+
if (num < 0)
24+
return 0;
25+
26+
int[] waysArr = new int[num];
27+
28+
waysArr[0] = 1;
29+
waysArr[1] = 2;
30+
waysArr[2] = 4;
31+
32+
for (int i = 3; i < num; i++) {
33+
waysArr[i] = waysArr[i - 1] + waysArr[i - 2] + waysArr[i - 3];
34+
}
35+
int ways = waysArr[waysArr.length - 1];
36+
waysArr = null;
37+
return ways;
38+
}
39+
}
40+
}

src/main/java/FibonacciNumbers.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
16
import java.math.BigInteger;
27

38
/**

src/main/java/LinkedListsDetectCycle.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
/**
3+
* @author medany
4+
*/
25

36
import java.util.ArrayList;
47
import java.util.List;

src/main/java/LonelyInteger.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class LonelyInteger {
7+
8+
public int solve(int[] array) {
9+
int num = 0;
10+
for (int i : array) {
11+
num = num ^ i;
12+
}
13+
return num;
14+
}
15+
}

src/main/java/Node.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
/**
3+
* @author medany
4+
*/
25

36
public class Node {
47
int data;

src/main/java/Primality.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class Primality {
7+
8+
public boolean solve(int number) {
9+
switch ("" + number) {
10+
case "0":
11+
case "1":
12+
return false;
13+
case "2":
14+
case "3":
15+
return true;
16+
default:
17+
if (number % 2 == 0)
18+
return false;
19+
int sqrt = (int) (Math.sqrt(number) + 1);
20+
for (int i = 3; i < sqrt; i += 2) {
21+
if (number % i == 0)
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class DavisStaircaseTest {
9+
10+
@Test
11+
public void Test_1() {
12+
13+
DavisStaircase alg = new DavisStaircase();
14+
15+
int actual = alg.solve(1), expected = 1;
16+
17+
Assert.assertEquals(expected, actual);
18+
}
19+
20+
@Test
21+
public void Test_2() {
22+
23+
DavisStaircase alg = new DavisStaircase();
24+
25+
int actual = alg.solve(7), expected = 44;
26+
27+
Assert.assertEquals(expected, actual);
28+
}
29+
}

test/main/java/FibonacciNumbersTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import static org.junit.Assert.assertEquals;
21

32
import java.math.BigInteger;
43

4+
import org.junit.Assert;
55
import org.junit.Test;
66

77
/**
@@ -17,7 +17,7 @@ public void Test_1() {
1717
BigInteger actual = alg.solve(1), expected = BigInteger.ONE;
1818

1919
System.out.println(actual);
20-
assertEquals(expected, actual);
20+
Assert.assertEquals(expected, actual);
2121
}
2222

2323
@Test
@@ -27,7 +27,7 @@ public void Test_2() {
2727
BigInteger actual = alg.solve(5), expected = BigInteger.valueOf(5);
2828

2929
System.out.println(actual);
30-
assertEquals(expected, actual);
30+
Assert.assertEquals(expected, actual);
3131
}
3232

3333
@Test
@@ -37,7 +37,7 @@ public void Test_3() {
3737
BigInteger actual = alg.solve(10), expected = BigInteger.valueOf(55);
3838

3939
System.out.println(actual);
40-
assertEquals(expected, actual);
40+
Assert.assertEquals(expected, actual);
4141
}
4242

4343
@Test
@@ -49,6 +49,6 @@ public void Test_4() {
4949
"43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875");
5050

5151
System.out.println(actual);
52-
assertEquals(expected, actual);
52+
Assert.assertEquals(expected, actual);
5353
}
5454
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class LonelyIntegerTest {
9+
10+
@Test
11+
public void Test_1() {
12+
13+
LonelyInteger alg = new LonelyInteger();
14+
15+
int actual = alg.solve(new int[] { 1, 1, 2 }), expected = 2;
16+
17+
Assert.assertEquals(expected, actual);
18+
19+
}
20+
21+
@Test
22+
public void Test_2() {
23+
24+
LonelyInteger alg = new LonelyInteger();
25+
26+
int actual = alg.solve(new int[] { 0, 0, 1, 2, 1 }), expected = 2;
27+
28+
Assert.assertEquals(expected, actual);
29+
30+
}
31+
32+
@Test
33+
public void Test_3() {
34+
35+
LonelyInteger alg = new LonelyInteger();
36+
37+
int actual = alg.solve(new int[] { 4, 9, 95, 93, 57, 4, 57, 93, 9 }), expected = 95;
38+
39+
Assert.assertEquals(expected, actual);
40+
41+
}
42+
43+
@Test
44+
public void Test_4() {
45+
46+
LonelyInteger alg = new LonelyInteger();
47+
48+
int actual = alg.solve(new int[] { 59, 88, 14, 8, 85, 1, 94, 74, 57, 96, 39, 2, 47, 43, 35, 17, 53, 52, 92, 31,
49+
99, 48, 94, 30, 92, 60, 32, 45, 88, 13, 39, 50, 22, 65, 89, 46, 65, 76, 57, 67, 99, 35, 76, 46, 85, 82,
50+
45, 62, 53, 80, 74, 22, 31, 52, 82, 13, 41, 96, 2, 1, 80, 62, 4, 20, 50, 89, 59, 67, 60, 8, 41, 14, 47,
51+
48, 17, 4, 43, 30, 32 }), expected = 20;
52+
53+
Assert.assertEquals(expected, actual);
54+
55+
}
56+
}

test/main/java/PrimalityTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class PrimalityTest {
9+
10+
@Test
11+
public void Test_1() {
12+
13+
Primality alg = new Primality();
14+
15+
boolean actual = alg.solve(10), expected = false;
16+
17+
Assert.assertEquals(expected, actual);
18+
19+
}
20+
21+
@Test
22+
public void Test_2() {
23+
24+
Primality alg = new Primality();
25+
26+
boolean actual = alg.solve(27), expected = true;
27+
28+
Assert.assertEquals(expected, actual);
29+
30+
}
31+
32+
@Test
33+
public void Test_3() {
34+
35+
Primality alg = new Primality();
36+
37+
boolean actual = alg.solve(99), expected = true;
38+
39+
Assert.assertEquals(expected, actual);
40+
41+
}
42+
43+
@Test
44+
public void Test_4() {
45+
46+
Primality alg = new Primality();
47+
48+
boolean actual = alg.solve(250), expected = false;
49+
50+
Assert.assertEquals(expected, actual);
51+
52+
}
53+
}

0 commit comments

Comments
 (0)