Merge pull request #21 from cwiki-us-docs/code_bank
Finish prime number test to check prime number too 100
This commit is contained in:
commit
b567b76203
|
@ -1,17 +1,19 @@
|
|||
package com.ossez.toolkits.codebank.tests;
|
||||
|
||||
import com.google.api.gbase.client.NumberUnit;
|
||||
import com.ibm.icu.impl.CharTrie;
|
||||
import com.ossez.toolkits.codebank.common.model.request.TopicRequest;
|
||||
import org.apache.commons.lang3.CharUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* This is the test file for something like to quick run.
|
||||
* This is the test file for something like to quick run.a Diophantine equation is a polynomial equation
|
||||
* Once test finished, we need to move codes to different name, just like when we were student we like to eraser after using.
|
||||
*
|
||||
* @author YuCheng
|
||||
|
@ -21,16 +23,65 @@ public class BlackboardTest {
|
|||
|
||||
private static List<Integer> loopList = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Main Test to Run
|
||||
*/
|
||||
@Test
|
||||
@Test
|
||||
public void test2st() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test1st() {
|
||||
|
||||
Hashtable<String, String> hashTable = new Hashtable<>();
|
||||
|
||||
String data = "aass";
|
||||
char[] strArray = data.toCharArray();
|
||||
String retStr = "";
|
||||
// char xC = Character.toString("C");
|
||||
// CharUtils.toChar()
|
||||
|
||||
if (data.length() == 1) {
|
||||
retStr = data;
|
||||
}
|
||||
|
||||
for (int i = 0; i < strArray.length; i++) {
|
||||
String valStr = Character.toString(strArray[i]);
|
||||
String rData = data;
|
||||
rData = data.replace(valStr, "");
|
||||
rData = rData.replace(valStr.toUpperCase(Locale.ROOT), "");
|
||||
rData = rData.replace(valStr.toLowerCase(Locale.ROOT), "");
|
||||
|
||||
if (rData.length() == 0) {
|
||||
retStr = "";
|
||||
} else if (rData.length() + 1 == data.length()) {
|
||||
retStr = valStr;
|
||||
break;
|
||||
}
|
||||
|
||||
// char val1 = strArray[i];
|
||||
// for(int j=i; j <strArray.length; j++) {
|
||||
// if (val1 == strArray[i]) {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// c =val1;
|
||||
}
|
||||
|
||||
System.out.println(retStr);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Test to Run
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("TREE TEST");
|
||||
|
||||
TopicRequest topicRequest = new TopicRequest();
|
||||
TopicRequest topicRequest = new TopicRequest();
|
||||
// topicRequest.setTopic_id(11);
|
||||
logger.debug("HashCode 1 - {}", topicRequest.hashCode());
|
||||
logger.debug("HashCode 1 - {}", topicRequest.hashCode());
|
||||
|
||||
logger.debug("HashCode AaAaAa - {}", "AaAaAa".hashCode());
|
||||
logger.debug("HashCode BBAaBB - {}", "BBAaBB".hashCode());
|
||||
|
@ -40,11 +91,59 @@ public class BlackboardTest {
|
|||
// }
|
||||
|
||||
// topicRequest = new TopicRequest();
|
||||
logger.debug("HashCode 2 - {}", topicRequest.hashCode());
|
||||
logger.debug("HashCode 2 - {}", topicRequest.hashCode());
|
||||
|
||||
String data = "{1,2,3,4,5,#,6,#,#,7,8,#,#}";
|
||||
|
||||
String[] dataArray = new String[3];
|
||||
this.subLogic(data);
|
||||
// StringUtils.replace();
|
||||
String s = "Well-";
|
||||
System.out.println(s.replace(s, ""));
|
||||
s = s.replace(s, "");
|
||||
|
||||
// int lineLength1_1 = 20;
|
||||
// int lineLength1_2 = 20;
|
||||
|
||||
String[] words1 = {"The", "day", "began", "as", "still", "as", "the", "night", "abruptly", "lighted", "with", "brilliant", "flame"};
|
||||
|
||||
int lineLength = 20;
|
||||
|
||||
List<String> returnList = new ArrayList<>();
|
||||
|
||||
String dataStr = "";
|
||||
for (String arrayData : words1) {
|
||||
dataStr = dataStr + arrayData + "-";
|
||||
}
|
||||
|
||||
|
||||
while (StringUtils.isNotEmpty(dataStr)) {
|
||||
if (StringUtils.length(dataStr) <= lineLength) {
|
||||
returnList.add(dataStr);
|
||||
break;
|
||||
}
|
||||
|
||||
String subStr = dataStr.substring(0, lineLength);
|
||||
if (subStr.endsWith("-")) {
|
||||
subStr = StringUtils.removeEnd(subStr, "-");
|
||||
} else {
|
||||
if (StringUtils.equals(dataStr.substring(lineLength, lineLength + 1), "-")) {
|
||||
subStr = StringUtils.substring(subStr, 0, lineLength);
|
||||
} else {
|
||||
subStr = StringUtils.substring(subStr, 0, StringUtils.lastIndexOf(subStr, "-"));
|
||||
}
|
||||
}
|
||||
returnList.add(subStr);
|
||||
|
||||
|
||||
dataStr = StringUtils.replace(dataStr, subStr, "");
|
||||
dataStr = StringUtils.removeStart(dataStr, "-");
|
||||
dataStr = StringUtils.removeEnd(dataStr, "-");
|
||||
}
|
||||
|
||||
|
||||
System.out.println(returnList);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.ossez.toolkits.codebank.tests.algorithm;
|
||||
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -11,9 +10,9 @@ import java.util.List;
|
|||
|
||||
/**
|
||||
* <p>
|
||||
* More details about question see link below
|
||||
* Prime numbers from 1 to 100
|
||||
* <ul>
|
||||
* <li>@see <a href= "https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap">https://www.cwiki.us/display/ITCLASSIFICATION/Binary+Gap</a>
|
||||
* <li>@see <a href= "https://www.ossez.com/t/prime-numbers-from-1-to-100-100/13450">https://www.ossez.com/t/prime-numbers-from-1-to-100-100/13450</a>
|
||||
* </li>
|
||||
* </ul>
|
||||
* </p>
|
||||
|
@ -34,21 +33,21 @@ public class PrimeNumbersTest {
|
|||
|
||||
int N = 100;
|
||||
List<Integer> primeNumberList = new ArrayList<>();
|
||||
|
||||
for (int i = 2; i < N; i++) {
|
||||
if (isPrimeNumber(i))
|
||||
primeNumberList.add(i);
|
||||
}
|
||||
if (isPrimeNumber(i))
|
||||
primeNumberList.add(i);
|
||||
}
|
||||
|
||||
logger.debug("primeNumber Count -[{}]", primeNumberList.size());
|
||||
logger.debug("primeNumber Count -[{}]", primeNumberList);
|
||||
logger.debug("primeNumber Count -[{}]", primeNumberList);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param number
|
||||
* @return
|
||||
*/
|
||||
public boolean isPrimeNumber(int number) {
|
||||
System.out.println(5%3);;
|
||||
private boolean isPrimeNumber(int number) {
|
||||
for (int i = 2; i < number; i++) {
|
||||
if (number % i == 0) {
|
||||
return false;
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
package com.ossez.toolkits.codebank.tests.algorithm;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* word-wrap Function
|
||||
* <ul>
|
||||
* <li>@see <a href= "https://www.ossez.com/t/a-word-wrap-functionality/13452">https://www.ossez.com/t/a-word-wrap-functionality/13452</a>
|
||||
* </li>
|
||||
* </ul>
|
||||
* </p>
|
||||
*
|
||||
* @author YuCheng
|
||||
*/
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class WordWrapTest {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WordWrapTest.class);
|
||||
|
||||
/**
|
||||
* Test word-wrap Function
|
||||
*/
|
||||
@Test
|
||||
public void testMain() {
|
||||
logger.debug("BEGIN");
|
||||
|
||||
|
||||
String[] words1 = {"The", "day", "began", "as", "still", "as", "the", "night", "abruptly", "lighted", "with", "brilliant", "flame"};
|
||||
int lineLength1_1 = 13;
|
||||
int lineLength1_2 = 20;
|
||||
|
||||
int lineLength = lineLength1_1;
|
||||
|
||||
List<String> returnList = new ArrayList<>();
|
||||
|
||||
String dataStr = "";
|
||||
for (String arrayData : words1) {
|
||||
dataStr = dataStr + arrayData + "-";
|
||||
}
|
||||
|
||||
|
||||
while (StringUtils.isNotEmpty(dataStr)) {
|
||||
if (StringUtils.length(dataStr) <= lineLength) {
|
||||
returnList.add(dataStr);
|
||||
break;
|
||||
}
|
||||
|
||||
String subStr = dataStr.substring(0, lineLength);
|
||||
if (subStr.endsWith("-")) {
|
||||
subStr = StringUtils.removeEnd(subStr, "-");
|
||||
} else {
|
||||
if (StringUtils.equals(dataStr.substring(lineLength, lineLength + 1), "-")) {
|
||||
subStr = StringUtils.substring(subStr, 0, lineLength);
|
||||
} else {
|
||||
subStr = StringUtils.substring(subStr, 0, StringUtils.lastIndexOf(subStr, "-"));
|
||||
}
|
||||
}
|
||||
returnList.add(subStr);
|
||||
|
||||
|
||||
dataStr = StringUtils.replace(dataStr, subStr, "");
|
||||
dataStr = StringUtils.removeStart(dataStr, "-");
|
||||
dataStr = StringUtils.removeEnd(dataStr, "-");
|
||||
}
|
||||
|
||||
logger.debug("{}", returnList);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -35,11 +36,12 @@ public class LeetCode0387FirstUniqueCharacterTest {
|
|||
String data = "lovelycomossez";
|
||||
|
||||
System.out.println(firstUniqChar(data));
|
||||
System.out.println(firstUniqCharString(data));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize from array to tree
|
||||
* Return the first Uniq Char String index
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
|
@ -76,5 +78,39 @@ public class LeetCode0387FirstUniqueCharacterTest {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the first Uniq Char String without using Map
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private String firstUniqCharString(String data) {
|
||||
// NULL CHECK
|
||||
if (data.equals("")) {
|
||||
return "";
|
||||
}
|
||||
|
||||
char[] strArray = data.toCharArray();
|
||||
String retStr = "";
|
||||
|
||||
if (data.length() == 1) {
|
||||
retStr = data;
|
||||
}
|
||||
|
||||
for (int i = 0; i < strArray.length; i++) {
|
||||
String valStr = Character.toString(strArray[i]);
|
||||
String rData = data;
|
||||
rData = data.replace(valStr, "");
|
||||
rData = rData.replace(valStr.toUpperCase(Locale.ROOT), "");
|
||||
rData = rData.replace(valStr.toLowerCase(Locale.ROOT), "");
|
||||
|
||||
if (rData.length() == 0) {
|
||||
retStr = "";
|
||||
} else if (rData.length() + 1 == data.length()) {
|
||||
retStr = valStr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return retStr;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue