Skip to content

Commit 6011b66

Browse files
committed
Adding another activity for lesson 1
1 parent 5fff4a5 commit 6011b66

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

src/main/java/com/packt/datastructuresandalg/lesson1/activity/improveintersection/solution/FastInterstectionSol1.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,12 @@ public List<Integer> intersectionFast(int[] a, int[] b) {
2222
private void mergeSort(int[] input) {
2323
Arrays.sort(input);
2424
}
25+
26+
public static void main(String[] args) {
27+
int[] numbers1 = new int[]{66, 24, 75, 22, 12, 87};
28+
int[] numbers2 = new int[]{32, 41, 98, 66, 39, 24};
29+
30+
List<Integer> result = new FastInterstectionSol1().intersectionFast(numbers1, numbers2);
31+
System.out.println(result);
32+
}
2533
}

src/main/java/com/packt/datastructuresandalg/lesson1/activity/improveintersection/solution/FastInterstectionSol2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ public List<Integer> intersectionFast(int[] a, int[] b) {
2525
public void mergeSort(int[] input) {
2626
Arrays.sort(input);
2727
}
28+
29+
public static void main(String[] args) {
30+
int[] numbers1 = new int[]{66, 24, 75, 22, 12, 87};
31+
int[] numbers2 = new int[]{32, 41, 98, 66, 39, 24};
32+
33+
List<Integer> result = new FastInterstectionSol2().intersectionFast(numbers1, numbers2);
34+
System.out.println(result);
35+
}
36+
2837
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.packt.datastructuresandalg.lesson1.activity.octaltodecimal;
2+
3+
public class OctalToDecimal {
4+
public int convertToDecimal(String octal) {
5+
return 999;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.packt.datastructuresandalg.lesson1.activity.octaltodecimal.solution;
2+
3+
public class OctalToDecimal {
4+
public int convertToDecimal(String oct) {
5+
int result = 0;
6+
for (int i = 1; i <= oct.length(); i++) {
7+
int octDigit = Integer.parseInt(oct.charAt(oct.length() - i) + "");
8+
result += Math.pow(8, i - 1) * octDigit;
9+
}
10+
return result;
11+
}
12+
13+
public static void main(String args[]) {
14+
OctalToDecimal octalToDecimal = new OctalToDecimal();
15+
System.out.println(octalToDecimal.convertToDecimal("17"));
16+
System.out.println(octalToDecimal.convertToDecimal("72625"));
17+
System.out.println(octalToDecimal.convertToDecimal("1"));
18+
System.out.println(octalToDecimal.convertToDecimal("55142"));
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.packt.datastructuresandalg.lesson1.activity.octaltodecimal;
2+
3+
import junit.framework.TestCase;
4+
5+
public class OctalToDecimalTest extends TestCase {
6+
public void testZeroOctal() {
7+
OctalToDecimal octalToDecimal = new OctalToDecimal();
8+
assertEquals(octalToDecimal.convertToDecimal("0"), 0);
9+
}
10+
11+
public void testOneOctal() {
12+
OctalToDecimal octalToDecimal = new OctalToDecimal();
13+
assertEquals(octalToDecimal.convertToDecimal("1"), 1);
14+
}
15+
16+
public void testFiveOctal() {
17+
OctalToDecimal octalToDecimal = new OctalToDecimal();
18+
assertEquals(octalToDecimal.convertToDecimal("5"), 5);
19+
}
20+
21+
public void test17Octal() {
22+
OctalToDecimal octalToDecimal = new OctalToDecimal();
23+
assertEquals(octalToDecimal.convertToDecimal("17"), 15);
24+
}
25+
26+
public void test72625Octal() {
27+
OctalToDecimal octalToDecimal = new OctalToDecimal();
28+
assertEquals(octalToDecimal.convertToDecimal("72625"), 30101);
29+
}
30+
31+
public void test55142Octal() {
32+
OctalToDecimal octalToDecimal = new OctalToDecimal();
33+
assertEquals(octalToDecimal.convertToDecimal("55142"), 23138);
34+
}
35+
}

0 commit comments

Comments
 (0)