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

Commit c7b072c

Browse files
committed
Pangram & PowerofTow algorithms
1 parent 98b1966 commit c7b072c

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Diff for: src/main/java/Pangram.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
/*
7+
* a word or sentence is called to be pangram if it contains all the alphabet
8+
* letters.
9+
*/
10+
public class Pangram {
11+
12+
public String solve(String[] strings) {
13+
StringBuilder result = new StringBuilder();
14+
for (String string : strings) {
15+
String alphabet = "abcdefghijklmnopqrstuvwxyz";
16+
for (int i = 0; i < string.length(); i++) {
17+
char current = string.charAt(i);
18+
if (alphabet.indexOf(current) != -1) {
19+
alphabet = alphabet.replace("" + current, "");
20+
if (alphabet.length() == 0) {
21+
result.append(1);
22+
}
23+
}
24+
}
25+
if (!(alphabet.length() == 0)) {
26+
result.append(0);
27+
}
28+
}
29+
return result.toString();
30+
}
31+
32+
}

Diff for: src/main/java/PowerofTwo.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
/**
3+
* @author medany
4+
*/
5+
6+
public class PowerofTwo {
7+
8+
public int[] solve(int[] nums) {
9+
int[] result = new int[nums.length];
10+
for (int i = 0; i < nums.length; i++) {
11+
boolean test = nums[i] > 0 && ((nums[i] & (nums[i] - 1)) == 0);
12+
result[i] = test ? 1 : 0;
13+
}
14+
return result;
15+
}
16+
17+
}

Diff for: test/main/java/PangramTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class PangramTest {
9+
10+
private Pangram alg = new Pangram();
11+
private String actual, expected;
12+
13+
@Test
14+
public void Test_1() {
15+
16+
actual = alg.solve(new String[] { "we promptly judged antique ivory buckles for the next prize",
17+
"we promptly judged antique ivory buckles for the prizes",
18+
"the quick brown fox jumps over the lazy dog", "the quick brown fox jump over the lazy dog" });
19+
expected = "1010";
20+
21+
Assert.assertEquals(expected, actual);
22+
}
23+
24+
}

Diff for: test/main/java/PowerofTwoTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author medany
6+
*/
7+
8+
public class PowerofTwoTest {
9+
10+
private PowerofTwo alg = new PowerofTwo();
11+
private int[] actual, expected;
12+
13+
@Test
14+
public void Test_1() {
15+
16+
actual = alg.solve(new int[] { 1, 2, 3, -1, 15, 11, 12 });
17+
expected = new int[] { 1, 1, 0, 0, 0, 0, 0 };
18+
19+
Assert.assertArrayEquals(expected, actual);
20+
}
21+
22+
}

0 commit comments

Comments
 (0)