Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JUnit Test #203

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions DecodeWays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main;

//A message containing letters from A-Z is being encoded to numbers using the following mapping:

//'A' -> 1
//'B' -> 2
//...
//'Z' -> 26
//tự viết
//Example 1
//Input: 34
//Output: 1
//Example 2
//Input: 12
//Output: 2
//Example 3
//Input: 16
//Output: 2
//Example 4
//Input:299
//Output: 1
//Example 5
//Input: 53
//Output: 1

//Given an encoded message containing digits, determine the total number of ways to decode it.

//For example,
//Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12).

//The number of ways decoding "12" is 2.

public class DecodeWays {
public static int numDecodings(String s) {
int n = s.length();

if(n == 0) {
return 0;
}

int[] dp = new int[n + 1];
dp[n] = 1;
dp[n - 1] = s.charAt(n - 1) != '0' ? 1 : 0;

for(int i = n - 2; i >= 0; i--) {
if(s.charAt(i) == '0') {
continue;
} else {
dp[i] = (Integer.parseInt(s.substring(i, i + 2)) <= 26) ? dp[i + 1] + dp[i + 2] : dp[i + 1];
}
}

return dp[0];
}
}

29 changes: 29 additions & 0 deletions MainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import main.Power;

public class MainTest {

@Test
void testExample1() {

assertEquals(true, Power.isPowerOfTwo(1));

}
@Test
void testExample4() {

assertEquals(true, Power.isPowerOfTwo(0));

}
@Test
void testExample5() {

assertEquals(true, Power.isPowerOfTwo(100));

}

}
11 changes: 11 additions & 0 deletions Power.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main;
public class Power {
public static boolean isPowerOfTwo(int n) {
long i = 1;
while(i < n) {
i <<= 1;
}

return i == n;
}
}
31 changes: 31 additions & 0 deletions ReverseWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main;
//Given an input string, reverse the string word by word.
//For example,
//Given s = "the sky is blue",
//return "blue is sky the".
//tự viết
//Example 1
//Input: String a = "Hello I am Nhi"
//Output: String a1 = "Nhi am I Hello"
//Example 2
//Input: String b = "I Love You"
//Output: String b1 = "You Love I"
//Example 3
//Input: String c = "Sorry"
//Output: String c1 = "Sorry"
//Example 4
//Input: String d = "Thank You"
//Output: String d1 = "You Thank"

public class ReverseWord {
public static String reverseWords(String s) {
String[] words = s.trim().split("\\s+");
String result = "";
for(int i = words.length - 1; i > 0; i--) {
result += words[i] + " ";
}

return result + words[0];
}
}

27 changes: 27 additions & 0 deletions TestDecodeFacebook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

import main.DecodeWays;

public class TestDecodeFacebook {
@Test
public void testEx1() {

assertEquals(1, DecodeWays.numDecodings("34"));

}
@Test
public void testEx2() {

assertEquals(2, DecodeWays.numDecodings("12"));

}
@Test
public void testEx5() {

assertEquals(1, DecodeWays.numDecodings("53"));

}
}
30 changes: 30 additions & 0 deletions TestReverseWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package test;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

import main.ReverseWord;


public class TestReverseWord {
@Test
public void Example1TestReverse() {

System.out.println(ReverseWord.reverseWords("Hello I am Nhi"));
assertEquals("Nhi am I Hello", ReverseWord.reverseWords("Hello I am Nhi"));


}
@Test
public void Example3TestReverse() {
System.out.println(ReverseWord.reverseWords("Sorry"));
assertEquals("Sorry", ReverseWord.reverseWords("Sorry"));

}
@Test
public void Example4TestReverse() {
System.out.println(ReverseWord.reverseWords("Thank You"));
assertEquals("You Thank", ReverseWord.reverseWords("Thank You"));

}
}