Created
June 28, 2025 13:20
-
-
Save luizrobertofreitas/202b55daafc974c858e31d993154f84a to your computer and use it in GitHub Desktop.
my-bitcoin-basic-script-executor
This file contains hidden or 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
package org.javatests; | |
import java.util.Stack; | |
public class Ex1 { | |
public Boolean transactions(String input) { | |
String[] arr = input.split(" "); | |
final var stack = new Stack<Integer>(); | |
for (String s : arr) { | |
String op = s.replace("OP_", ""); | |
if (op.chars().allMatch(Character::isDigit)) { | |
Integer number = Integer.parseInt(op); | |
stack.push(number); | |
} else if (!stack.isEmpty()) { | |
if (op.equals("EQUAL")) { | |
if (stack.pop().equals(stack.pop())) stack.push(1); | |
else stack.push(0); | |
} else if (op.equals("ADD")) { | |
Integer res = stack.pop() + stack.pop(); | |
stack.push(res); | |
} else if (op.equals("SUB")) { | |
Integer top = stack.pop(); | |
Integer second = stack.pop(); | |
stack.push(second - top); | |
} else if (op.equals("VERIFY")) { | |
if (stack.peek().equals(0)) return false; | |
else stack.pop(); | |
} | |
} | |
} | |
return !stack.isEmpty() && stack.pop() != 0; | |
} | |
} |
This file contains hidden or 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
package org.javatests; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.params.ParameterizedTest; | |
import org.junit.jupiter.params.provider.CsvSource; | |
class Ex1Test { | |
@ParameterizedTest | |
@CsvSource({ | |
"OP_1 OP_2 OP_ADD,true", | |
"OP_1 OP_1 OP_ADD OP_3 OP_EQUAL,false", | |
"OP_3 OP_2 OP_SUB OP_1 OP_EQUAL,true", | |
"OP_3 OP_2 OP_SUB OP_5 OP_ADD OP_5 OP_EQUAL,false", | |
"OP_ADD,false", | |
"OP_1 OP_2,true", | |
"OP_0 OP_VERIFY OP_1,false", | |
"OP_1 OP_VERIFY OP_2,true" | |
}) | |
void test(String input, Boolean expected) { | |
Ex1 ex = new Ex1(); | |
Assertions.assertEquals(expected, ex.transactions(input)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment