Skip to content

Instantly share code, notes, and snippets.

@jeffschulthies
Last active August 29, 2015 14:07
Show Gist options
  • Save jeffschulthies/31df5f96fafa666862bc to your computer and use it in GitHub Desktop.
Save jeffschulthies/31df5f96fafa666862bc to your computer and use it in GitHub Desktop.
vaidateRow Method
import java.util.Scanner;
public class Combine {
public static void main(String[] argv) {
Scanner scnr = new Scanner(System.in); // talk to Gabe Monday to better
// understand scanner
// String row = scnr.next();
// row = moveLeft(row); // remember to call methods
// row = combineLeft(row);
// System.out.print(row);
System.out.println(combineLeft("_4422"));
// asserts
assert validateChar('2') : "2 should be a valid char";
assert validateChar('4') : "4 should be a valid char";
assert validateChar('_') : "_ should be a valid char";
assert !validateChar(' ') : "space should not be a valid char";
assert !validateChar('3') : "3 should not be a valid char";
assert !validateRow("2222") : "2222 should not be valid";
// assert !validateRow("__") : "__ should not be valid";
assert !validateRow("aaa") : "aaa should not be valid";
assert !validateRow("333") : "333 should not be valid";
assert validateRow("_2_") : "_2_ should be valid";
assert validateRow("22_") : "22_ should be valid";
assert validateRow("242") : "242 should be valid";
assert "4__".equals(combineLeft("__4")) : "__4 doesn't change to 4__";
assert "8__".equals(combineLeft("_44")) : "_44 doesn't change to 8__";
assert "4__".equals(combineLeft("2_2")) : "2_2 doesn't change to 4__";
assert "4__".equals(combineLeft("22_")) : "22_ doesn't change to 4__";
assert "42_".equals(combineLeft("222")) : "222 doesn't change to 42_";
assert "44_".equals(combineLeft("422")) : "422 doesn't change to 44_";
assert "242".equals(combineLeft("242")) : "242 doesn't change to 242";
assert "8__".equals(combineLeft(combineLeft("422"))) : "Double invocation doens't work!";
assert "___".equals(combineLeft("___")) : "___should be invalid!";
}
public static boolean validateChar(char ch) {
if (ch == '2' || ch == '4' || ch == '_' || ch == '8') {
return true;
}
else {
return false;
}
}
public static boolean validateRow(String row) {
if (((row.charAt(0) == '_' || row.charAt(0) == '2' || row.charAt(0) == '4')) && ((row.charAt(1) == '_' || row.charAt(1) == '2' || row.charAt(1) == '4')) && ((row.charAt(2) == '_' || row.charAt(2) == '2' || row.charAt(2) == '4'))) {
if(row.length() !=3) {
return false;
}
else if(row == "___") {
return false;
}
else{
return true;
}
}
else {
return false;
}
}
public static String moveLeft(String row) {
if (validateRow(row) == true) {
row = row.replace("__2", "2__");
row = row.replace("_2_", "2__");
row = row.replace("__4", "4__");
row = row.replace("_4_", "4__");
row = row.replace("_44", "44_");
}
else {
return "Not a Valid String";
}
return row;
}
public static String combineLeft(String row) {
String row2 = moveLeft(row);
if (validateRow(row2) == true) {
row2 = row2.replace("22_", "4__");
row2 = row2.replace("_22", "4__");
row2 = row2.replace("2_2", "4__");
row2 = row2.replace("222", "42_");
row2 = row2.replace("44_", "8__");
row2 = row2.replace("_44", "8__");
row2 = row2.replace("4_4", "8__");
row2 = row2.replace("444", "84_");
row2 = row2.replace("422", "44_");
row2 = row2.replace("442", "82_");
row2 = row2.replace("224", "44_");
row2 = row2.replace("244", "28_");
}
return row2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment