Created
April 4, 2020 14:58
-
-
Save 71m024/f6f7becc4b0139f44b8622684e7a7651 to your computer and use it in GitHub Desktop.
Timo-UPN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
private char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; | |
private char[] operators = {'+', '*'}; | |
private char[] basement = {'$', '_', '_', '_', '_', '_', '_', '_'}; | |
private int basementPointer = 0; | |
private char basementBottom = '$'; | |
private char basementPlaceholder = '_'; | |
private char[] word = {'3', '4', '+', '6', '2', '+', '*'}; | |
public static void main(String[] args) throws Exception { | |
new Main(); | |
} | |
public Main() throws Exception { | |
// transition from state-0 to state-1 | |
char nexCharacter = popFromBasement(); | |
if (isDigit(read()) && nexCharacter == this.basementBottom) { | |
char[] result = {nexCharacter, '$'}; | |
putToBasement(result); | |
} else { | |
throw new Exception("this word is not acceptable"); | |
} | |
} | |
private boolean isDigit(char character) { | |
return charsContain(character, this.digits); | |
} | |
private boolean isOperator(char character) { | |
return charsContain(character, this.operators); | |
} | |
private boolean charsContain(char character, char[] characterList) { | |
for (char digit : characterList) { | |
if (character == digit) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private char popFromBasement() { | |
char value = this.basement[this.basementPointer]; | |
this.basement[this.basementPointer] = '_'; | |
this.basementPointer--; | |
return value; | |
} | |
private void putToBasement(char[] word) { | |
for (char character : word) { | |
putToBasement(character); | |
} | |
} | |
private void putToBasement(char character) { | |
this.basementPointer++; | |
this.basement[this.basementPointer] = character; | |
} | |
/** | |
* Read the first character of the word and remove it. | |
* | |
* @return the next character from the left | |
*/ | |
private char read() { | |
char character = this.word[0]; | |
char[] newWord = new char[this.word.length - 1]; | |
System.arraycopy(this.word, 1, newWord, 0, newWord.length); | |
this.word = newWord; | |
return character; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment