mannerh1
BAN USERpublic class T3 {
private static String input = "1010503";
private static String result = new String();
public static void main(String[] args) {
int count = 0;
processing(count);
System.out.println(result);
}
private static void processing(int count) {
if (count > input.length() - 1)
return;
else {
if ((count % 3) == 0) {
result += input.charAt(count);
if (count != input.length() - 1) {
result += ",";
}
} else {
result += input.charAt(count);
}
count++;
processing(count);
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
public class T2 {
private static ArrayList<ArrayList<Integer>> twoDimen = new ArrayList<>();
public static void main(String[] args) {
init();
int col = -1;
int row = -1;
Stack<Map<Integer, Integer>> result = new Stack<>();
Map<Integer, Integer> getPrev = new HashMap<>();
boolean firstRow = true;
for (int i = 0; i < twoDimen.size(); i++) {
ArrayList<Integer> getA = twoDimen.get(i);
for (int j = 0; j < getA.size(); j++) {
if (getA.get(j) == 1) {
if (firstRow) {
row = i;
col = j;
} else {
getPrev = result.peek();
if (getPrev.isEmpty() || getPrev == null)
continue;
else {
int value = -1;
if (getPrev.containsKey(i - 1)) {
value = getPrev.get(i - 1);
if ((value == j)) {
for (int k = j; k < getA.size(); k++) {
if (getA.get(k) == 1) {
row = i;
col = k;
}
}
break;
} else {
continue;
}
}
}
}
}
}
firstRow = false;
Map<Integer, Integer> getOne = new HashMap<>();
if ((row != -1) || (col != -1))
getOne.put(row, col);
result.add(getOne);
}
System.out.println(result);
}
private static void init() {
ArrayList<Integer> inner = new ArrayList<>();
inner.add(1);
inner.add(0);
inner.add(0);
inner.add(0);
twoDimen.add(inner);
inner = new ArrayList<>();
inner.add(1);
inner.add(1);
inner.add(0);
inner.add(0);
twoDimen.add(inner);
inner = new ArrayList<>();
inner.add(0);
inner.add(1);
inner.add(1);
inner.add(0);
twoDimen.add(inner);
inner = new ArrayList<>();
inner.add(1);
inner.add(1);
inner.add(1);
inner.add(1);
twoDimen.add(inner);
inner = new ArrayList<>();
}
}
import java.util.ArrayList;
import java.util.Stack;
public class T1 {
private static int totalNum = 5;
private static int distance = 5;
private static int[] position = { 1, 3, 5, 9, 14 };
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> spreading = new ArrayList<>();
for (int i = 0; i < totalNum; i++) {
ArrayList<Integer> inside = new ArrayList<>();
for (int j = 0; j < totalNum; j++) {
if (i == j) {
continue;
} else {
if (position[i] < position[j])
if (position[i] >= (position[j] - distance)) {
inside.add(position[j]);
}
if (position[i] > position[j])
if (position[i] <= (position[j] + distance)) {
inside.add(position[j]);
}
}
} // end for j
spreading.add(inside);
} // end for i
// result
System.out.println(spreading);
// Analyze the result
int max = 0;
int min = totalNum;
Stack<Integer> minSpreading = new Stack<>();
Stack<Integer> maxSpreading = new Stack<>();
int positionCount = 0;
for (ArrayList<Integer> inside : spreading) {
positionCount++;
int count = 0;
for (Integer getInside : inside) {
count++;
}
if (count > max) {
max = count;
maxSpreading.add(positionCount);
}
if (count < min) {
min = count;
minSpreading.add(positionCount);
}
}
System.out.println("max : " + maxSpreading.peek());
System.out.println("min : " + minSpreading.peek());
}
}
package T12;
import java.util.ArrayList;
public class T12 {
private static final String input = "a{d,c,b}e";
public static void main(String[] args) {
boolean first = false, second = false;
ArrayList<String> firstS = new ArrayList<>();
ArrayList<String> secondS = new ArrayList<>();
ArrayList<String> thirdS = new ArrayList<>();
for (int i = 0; i < input.length(); i++) {
char getChar = input.charAt(i);
switch (getChar) {
case '{':
first = true;
break;
case '}':
second = true;
break;
case ',':
break;
default:
if ((first == false) && (second == false)) {
firstS.add(String.valueOf(getChar));
} else if ((first == true) && (second == false)) {
secondS.add(String.valueOf(getChar));
} else if ((first == true) && (second == true)) {
thirdS.add(String.valueOf(getChar));
}
break;
}
}
String beforeS = new String();
String afterS = new String();
for (String temp : firstS) {
beforeS += temp;
}
for (String temp : thirdS) {
afterS += temp;
}
ArrayList<String> result = new ArrayList<>();
for (int k = 0; k < secondS.size(); k++) {
String temp = new String();
String mid = secondS.get(k);
temp = beforeS + mid + afterS;
result.add(temp);
}
System.out.println(result);
}
}
package T15;
import java.util.ArrayList;
public class T15 {
private static ArrayList<ArrayList<Integer>> input = new ArrayList<>();
private static ArrayList<Integer> result = new ArrayList<>();
public static void main(String[] args) {
init();
process();
System.out.println(result);
}
private static void process() {
ArrayList<Integer> getInner1 = new ArrayList<>();
ArrayList<Integer> getInner2 = new ArrayList<>();
for (int i = 0; i < input.size(); i++) {
getInner1 = input.get(i);
for (int j = i + 1; j < input.size(); j++) {
getInner2 = input.get(j);
compare(getInner1, getInner2);
}
}
}
private static void compare(ArrayList<Integer> g1, ArrayList<Integer> g2) {
for (int t : g1) {
compare2(t, g2);
}
}
private static void compare2(int t, ArrayList<Integer> g) {
for (int i = 0; i < g.size(); i++) {
if (t == g.get(i))
result.add(g.get(i));
}
}
private static void init() {
ArrayList<Integer> inner = new ArrayList<>();
inner.add(1);
inner.add(3);
inner.add(5);
input.add(inner);
inner = new ArrayList<>();
inner.add(1);
inner.add(3);
inner.add(9);
input.add(inner);
inner = new ArrayList<>();
inner.add(9);
inner.add(5);
input.add(inner);
}
}
package T13;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;
public class T13 {
private static final int seatTotal = 12;
private static final ArrayList<Integer> result = new ArrayList<>();
private static final Stack<Integer> midResult = new Stack<>();
public static void main(String[] args) {
for (int i = 0; i < seatTotal; i++) {
if (i == 0) {
midResult.add(i + 1);
continue;
} else if (i == 1) {
midResult.add(seatTotal);
result.addAll(midResult);
Collections.sort(result);
midResult.removeAll(midResult);
System.out.println("result : " + result);
continue;
}
distance(i);
}
System.out.println(result);
}
private static void distance(int index) {
int min, max, mid = 0;
HashMap<Integer, Integer> getH = new HashMap<>();
if (midResult.isEmpty()) {
min = result.get(0);
max = result.get(1);
mid = calc(min, max);
if (mid == -1) {
if (result.size() == seatTotal)
return;
else {
getH = findMoreThanOne();
Set<Integer> tempSet = getH.keySet();
Iterator<Integer> it = tempSet.iterator();
min = it.next();
max = getH.get(min);
System.out.println("====================== : min : " + min + " max : " + max);
mid = calc(min, max);
midResult.push(mid);
result.addAll(midResult);
Collections.sort(result);
midResult.removeAll(midResult);
}
} else {
midResult.push(mid);
reset(min, max);
}
} else {
min = findNext(midResult.peek());
max = findNext(min);
mid = calc(min, max);
if (mid == -1)
return;
midResult.push(mid);
reset(min, max);
}
}
private static HashMap<Integer, Integer> findMoreThanOne() {
HashMap<Integer, Integer> retVal = new HashMap<>();
int min, max = 0;
for (int i = 0; i < result.size(); i++) {
min = result.get(i);
for (int j = i + 1; j < result.size();) {
max = result.get(j);
if (calc(min, max) != -1) {
retVal.put(min, max);
System.out.println("findMoreThanOne : min : " + min + " max : " + max);
return retVal;
}
break; // only one time processing i & i+1
}
}
return retVal;
}
private static void reset(int min, int max) {
Collections.sort(midResult);
System.out.println("min : " + min + " max : " + max + " midResult : " + midResult);
if (max == seatTotal) {
result.addAll(midResult);
Collections.sort(result);
midResult.removeAll(midResult);
System.out.println("result : " + result);
}
}
private static int findNext(int min) {
int retVal = 0;
for (int getI : result) {
if (getI > min) {
retVal = getI;
break;
}
}
return retVal;
}
private static int calc(int min, int max) {
int retVal = 0;
if (((max - min) / 2) < 1)
return -1;
retVal = (max - min) / 2 + min;
return retVal;
}
}
package T11;
import java.util.ArrayList;
import java.util.Stack;
public class T11 {
static int[] L = { 1, 2, 3, 4, 5};
static int N = 3;
static ArrayList<Integer> input = new ArrayList<>();
static ArrayList<Object[]> output = new ArrayList<>();
static Stack<Integer> process = new Stack<>();
public static void main(String[] args) {
init();
process();
// find out the result
for(Object[] temp : output) {
System.out.print("[");
for(int i = 0 ; i < temp.length; i++) {
String t = temp[i].toString();
if(i != temp.length-1)
System.out.print(Integer.valueOf(t)+", ");
else
System.out.print(Integer.valueOf(t));
}
System.out.println("]");
}
}
private static void init() {
for (int i = 0; i < L.length; i++) {
input.add(L[i]);
}
// System.out.println(input);
}
private static void process() {
for (int i = 0; i < input.size() - N + 1; i++) {
process.removeAllElements();
process.push(input.get(i));
oneScan(i, i, false);
}
}
private static void oneScan(int index, int secondIndex, boolean last) {
int getIndex = index;
int getSecondIndex = secondIndex;
if (process.size() > 1) {
process.pop();
}
if (last == true)
return;
Object[] temp = null;
for (int i = index; i < input.size(); i++) {
if (process.size() == N) {
if (process.size() < N)
continue;
temp = (Object[]) process.toArray();
if(checkDup(temp) == true) continue;
output.add(temp);
process.pop();
getSecondIndex = findIndex(process.peek());
i--;
continue;
} else {
if (input.get(i) == input.get(getSecondIndex))
continue;
if (process.peek() >= input.get(i)) {
continue;
} else {
process.push(input.get(i));
}
}
} // end of for i
if (process.size() == N) {
temp = (Object[]) process.toArray();
if(checkDup(temp) == false) {
output.add(temp);
process.pop();
getSecondIndex = findIndex(process.peek());
}
}
getIndex = getIndex + 1;
if (getIndex == input.size())
oneScan(getIndex, getSecondIndex, true);
else
oneScan(getIndex, getSecondIndex, false);
}
private static int findIndex(int value) {
int retVal = 0;
for (int i = 0; i < input.size(); i++) {
if (value == input.get(i)) {
retVal = i;
break;
}
}
return retVal;
}
private static boolean checkDup(Object[] toCompare) {
boolean retVal = false;
for(Object[] getOutput : output) {
for(int i = 0 ; i < getOutput.length; i++) {
String getEachElement = getOutput[i].toString();
if(getEachElement.equalsIgnoreCase(toCompare[i].toString())) {
retVal = true;
}else {
retVal = false;
break;
}
}
if(retVal == true) return retVal;
}
return retVal;
}
}
package T9;
import java.util.ArrayList;
public class T9Class {
private static ArrayList<String> input = new ArrayList<>();
private static ArrayList<ArrayList<String>> inputSet = new ArrayList<>();
private static ArrayList<ArrayList<Integer>> scoreSet = new ArrayList<>();
public static void main(String[] args) {
// TODO Auto-generated method stub
// [a,b,c,d] [a.b.f.g] [a,b,h,i] [j,k,l,m]
// find least in common set
init();
processing();
analyze();
}
private static void processing() {
// check one by one with the similarity
for (int i = 0; i < inputSet.size(); i++) {
ArrayList<Integer> score = new ArrayList<>();
ArrayList<String> getOne = inputSet.get(i);
for (int j = 0; j < inputSet.size(); j++) {
if(i == j) continue;
ArrayList<String> getTwo = inputSet.get(j);
score.add(check(getOne, getTwo));
}
scoreSet.add(score);
}
System.out.println(scoreSet);
}
private static void analyze() {
// find the lowest sum
int findOut = 10000000;
int group = 0;
for(int i = 0; i < scoreSet.size(); i++) {
ArrayList<Integer> getS = scoreSet.get(i);
int sum = 0;
for(int j = 0; j < getS.size(); j++) {
sum= sum + getS.get(j);
}
if(findOut > sum) {
findOut = sum;
group = i;
}
}
System.out.println("find out sum : " + findOut);
System.out.println("final group number (starting from 0) : "+group);
}
private static int check(ArrayList<String> getOne, ArrayList<String> getTwo) {
int retVal = 0;
for (int i = 0; i < getOne.size(); i++) {
String temp = getOne.get(i);
for (int j = 0; j < getTwo.size(); j++) {
if(temp.equalsIgnoreCase(getTwo.get(j))) {
retVal++;
}
}
}
return retVal;
}
private static void init() {
input.add("a");
input.add("b");
input.add("c");
input.add("d");
inputSet.add(input);
input = new ArrayList<String>();
input.add("a");
input.add("b");
input.add("f");
input.add("g");
inputSet.add(input);
input = new ArrayList<String>();
input.add("a");
input.add("b");
input.add("h");
input.add("i");
inputSet.add(input);
input = new ArrayList<String>();
input.add("j");
input.add("k");
input.add("l");
input.add("m");
inputSet.add(input);
System.out.println(inputSet);
}
}
package test1;
import java.util.Stack;
public class Test1 {
public static Stack<Integer> s = new Stack<>();
public static void main(String[] args) {
int a[] = { 2, 10, 14, 19, 51, 71 };
int b[] = { 2, 9, 19, 40, 51 };
merge(a, b);
System.out.println(s);
}
private static void merge(int a[], int b[]) {
int t1, t2 = 0;
for (int i = 0; i < a.length; i++) {
t1 = a[i];
for (int j = 0; j < b.length; j++) {
t2 = b[j];
if (s.isEmpty()) {
if (t1 < t2)
s.push(t1);
else if (t1 > t2)
s.push(t2);
else
s.push(t1);
} else {
if (t1 == t2) {
if (s.peek() < t1) {
s.push(t1);
}
} else {
if (t1 < t2) {
if (s.peek() < t1) {
s.push(t1);
break;
}
} else {
if (s.peek() < t2) {
s.push(t2);
}
}
}
}
}
}
}
}
package test1;
public class Test1 {
public static void main(String[] args) {
printSome(1);
}
private static int printSome(int input) {
if (input > 10)
return 0;
for (int i = 0; i < input; i++) {
System.out.print("*");
}
System.out.println();
input += 2;
return printSome(input);
}
}
- mannerh1 October 12, 2019package test1;
public class Test1 {
public static void main(String[] args) {
printSome(1);
}
private static int printSome(int input) {
if (input > 10)
return 0;
for (int i = 0; i < input; i++) {
System.out.print("*");
}
System.out.println();
input += 2;
return printSome(input);
}
}
public void main()
{
ArrayList <String> arrayList = new ArrayList<String>();
arrayList.add("a,b,c,d,e,f");
arrayList.add("1,2,3,4");
arrayList.add("w,x,y,z");
String result = new String();
for(String getS : checkBeforeFinal(arrayList)){
result = result + getS + "\n";
}
System.out.print(result);
}
private ArrayList<String> checkBeforeFinal(ArrayList<String> input){
ArrayList<String> finalRetVal = new ArrayList<String>();
String retVal = null;
String charBeforeComma = null;
for(int i=0; i< input.size(); i++){
String getStr = input.get(i);
for(int j=0; j < getStr.length(); j++){
char getChar = getStr.charAt(j);
String compare = String.valueOf(getChar);
if(!compare.equals(",")){
charBeforeComma = String.valueOf(getChar);
}else{ // the char is comma
// reset charBeforeComma
retVal = charBeforeComma;
charBeforeComma = null;
}
} // end of for j
finalRetVal.add(retVal);
} // end of for i
return finalRetVal;
}
public class Test10 {
public static void main(String[] args) {
char[] input = { 'A', 'R', 'G', 'E', 'N', 'T', 'I', 'N', 'A' };
processing(input);
}
public static void processing(char[] input) {
for(int i = input.length-1; i > -1; i--){
System.out.println(input[i]);
}
}
}
public class Test9 {
public static void main(String[] args) {
// find second lowest number
int[] input = {31,22,13,54,45,26,87,18,39,60};
int lowestNum = 100000;
int targetNum = 10000;
for(int i = 0; i < input.length;i++){
if(input[i] < targetNum){
targetNum = input[i];
}else if(targetNum < lowestNum){
// swap
int temp = 0;
temp = targetNum;
targetNum = lowestNum;
lowestNum = temp;
}
}
System.out.println("target Num : "+targetNum);
System.out.println("lowest Num : "+lowestNum);
}
}
package com.test.six;
import java.util.ArrayList;
public class Test8 {
public static void main(String[] args) {
ArrayList<Test88> input = new ArrayList<Test88>();
ArrayList<Test88> output = new ArrayList<Test88>();
input = initialize();
output = processing(input);
// print out
for (int i = 0; i < output.size(); i++) {
System.out.print(output.get(i).name + " | ");
}
}
private static ArrayList<Test88> processing(ArrayList<Test88> input) {
ArrayList<Test88> retVal = new ArrayList<Test88>();
int lowestNum = 10000;
Test88 lowestObj = null;
boolean nullFlag = false;
for (int i = 0; i < input.size(); i++) {
for (int j = 0; j < input.size(); j++) {
// find lowest one first
Test88 get88 = input.get(j);
if (get88.flag != true) {
// check Null
if (get88.id != null) {
// find lowest one
if (get88.id < lowestNum) {
lowestObj = get88;
lowestNum = input.get(j).id;
}
} else { // if null
retVal.add(get88);
get88.flag = true;
nullFlag = true;
break;
}
}
} // end of j
// reset
lowestNum = 10000;
if (nullFlag == true) {
nullFlag = false;
continue;
} else {
retVal.add(lowestObj);
lowestObj.flag = true;
}
}
return retVal;
}
private static ArrayList<Test88> initialize() {
ArrayList<Test88> retVal = new ArrayList<Test88>();
for (int i = 0; i < 5; i++) {
Test88 test88 = new Test88();
test88.num = i;
switch (i) {
case 0:
test88.name = "ABC";
test88.id = 2;
test88.flag = false;
break;
case 1:
test88.name = "PRW";
// test88.id = null;
test88.flag = false;
break;
case 2:
test88.name = "DEF";
test88.id = 2;
test88.flag = false;
break;
case 3:
test88.name = "PRE";
test88.id = 3;
test88.flag = false;
break;
case 4:
test88.name = "DKF";
test88.id = 4;
test88.flag = false;
break;
default:
break;
}
retVal.add(test88);
}
return retVal;
}
}
public class Test88 {
int num;
String name;
Integer id;
boolean flag;
}
package com.test.four;
import java.util.ArrayList;
import java.util.Iterator;
public class Test4 {
public static void main(String[] args) {
// store the result
ArrayList<Integer[]> ret = new ArrayList<Integer[]>();
// the value of total sum which you can change
final int SET = 10;
// input
Integer[] aa = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < aa.length; i++) {
for (int j = i + 1; j < aa.length; j++) {
for (int k = j + 1; k < aa.length; k++) {
if (aa[i] + aa[j] + aa[k] <= SET) {
try {
ret.add(new Integer[] { aa[i], aa[j], aa[k] });
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// confirm the results
for (int i = 0; i < ret.size(); i++) {
Iterator<Integer[]> it = (Iterator<Integer[]>) ret.iterator();
while (it.hasNext()) {
Integer temp[] = (Integer[]) it.next();
System.out.print("[");
for (int j = 0; j < temp.length; j++) {
if (j == 0 || j == 1)
System.out.print(temp[j] + ", ");
else
System.out.print(temp[j]);
}
System.out.println("]");
}
}
}
}
package com.test.four;
import java.util.ArrayList;
import java.util.Iterator;
public class Test4 {
public static void main(String[] args) {
// store the result
ArrayList<Integer[]> ret = new ArrayList<Integer[]>();
// the value of total sum which you can change
final int SET = 10;
// input
Integer[] aa = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for (int i = 0; i < aa.length; i++) {
for (int j = i + 1; j < aa.length; j++) {
for (int k = j + 1; k < aa.length; k++) {
if (aa[i] + aa[j] + aa[k] <= SET) {
try {
ret.add(new Integer[] { aa[i], aa[j], aa[k] });
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
// confirm the results
for (int i = 0; i < ret.size(); i++) {
Iterator<Integer[]> it = (Iterator<Integer[]>) ret.iterator();
while (it.hasNext()) {
Integer temp[] = (Integer[]) it.next();
System.out.print("[");
for (int j = 0; j < temp.length; j++) {
if (j == 0 || j == 1)
System.out.print(temp[j] + ", ");
else
System.out.print(temp[j]);
}
System.out.println("]");
}
}
}
}
One scan is needed for finding one lower digit.
For the first time you can register first digit to compare.
For the second time, you can compare the first digit and present one.
If they have the same value, you can move next round.
If not you can see what is the lower one between two digits with saving the values and indexes.
For the third time, you can compare the second digit and third digit.
If they have the same value, you have to compare the results of the previous round. That means you have to pick up one of two different values.
For the fourth time, you can compare the third digit and fourth digit.
If previous results are setting, you can find which one is lower.
Using preResultIndex2 variable you can see the results of previous round.
public class test2 {
public static void main(String[] args) {
int[] a = {3,3,3,1};
int compare = 0;
int preResultIndex1 = 0;
int finalValue1 = 0;
int preResultIndex2 = 0;
int finalValue2 = 0;
for(int i=0; i<a.length;i++){
if(i ==0)
compare = a[i];
else if(i == 1){
if(compare != a[i]){
preResultIndex1 = 0;
preResultIndex2 = 1;
finalValue1 = compare;
finalValue2 = a[i];
}
}else if(i == 2){
compare = a[i-1];
if(compare != a[i]){
if(preResultIndex2 != 0){
if(finalValue1 == a[i]){
System.out.println("found:" + preResultIndex2);
break;
}else{
System.out.println("found:" + preResultIndex1);
break;
}
}else{
preResultIndex1 = i-1;
preResultIndex2 = i;
finalValue1 = compare;
finalValue2 = a[i];
}
}else{
if(preResultIndex2 != 0){
if(finalValue1 == a[i]){
System.out.println("found:" + preResultIndex2);
break;
}else{
System.out.println("found:" + preResultIndex1);
break;
}
}
}
}else if(i == 3){
compare = a[i-1];
if(compare != a[i]){
if(preResultIndex2 != 0){
if(finalValue1 == a[i]){
System.out.println("found:" + preResultIndex2);
break;
}else{
System.out.println("found:" + preResultIndex1);
break;
}
}else{
if(preResultIndex2 != 0){
if(finalValue1 == a[i]){
System.out.println("found:" + preResultIndex2);
break;
}else{
System.out.println("found:" + preResultIndex1);
break;
}
}else{
System.out.println("found:"+i);
}
}
}
}
}
}
}
- mannerh1 November 17, 2020