Talk:Kangaroo
Jump to navigation
Jump to search
Matrix Kangaroo
Another kind of Kangaroo programming is the square matrix of natural numbers (where zero is considered a natural number). Therefore, you can also make up the tensor product of two programs. --Zzo38 (talk) 05:52, 12 June 2017 (UTC)
- Strangely enough I used that language in a programming competition a while back (marking it as a Kangaroo derivative). My specification was "the program is a square matrix of nonnegative integers; repeatedly, take the smallest row of the matrix, and add the first element of that row to each element of the first row of the matrix, the second element of that row to each element of the second row of the matrix, and so on"; this isn't quite the same language as Kangaroo, but it's Turing complete for the same reason. --ais523 22:33, 14 June 2017 (UTC)
Implementation
Here is an implementation in Java:
import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; import java.util.HashMap; public class KangarooInterpreter { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<String> code = new ArrayList<>(); HashMap<String, BigInteger> skips = new HashMap<>(); System.out.print(""" Enter your code line by line. To end code input, type "$END". """); while (true) { String line = scan.nextLine(); if (line.equals("$END")) { break; } code.add(line); } int counter = 0; for (int i = 0; i < code.size(); i++) skips.put(code.get(i).substring(0, code.get(i).indexOf(":")), BigInteger.ZERO); while (true) { String label = code.get(counter).substring(0, code.get(counter).indexOf(":")); if (skips.get(label).equals(BigInteger.ZERO)) { if (code.get(counter).matches("[a-z]+: skip ([a-z]+, )+[a-z]+")) { String[] arguments = code.get(counter).substring(code.get(counter).indexOf(":") + 7).split(", "); for (int i = 0; i < arguments.length; i++) { skips.put(arguments[i], skips.get(arguments[i]).add(new BigInteger("1"))); } } else if (code.get(counter).matches("[a-z]+: print [a-z]+")) System.out.println(skips.get(code.get(counter).substring(code.get(counter).indexOf(":") + 8))); else if (code.get(counter).matches("[a-z]+: input [a-z]+")) skips.put(code.get(counter).substring(code.get(counter).indexOf(":") + 8), new BigInteger(scan.nextLine())); else if (code.get(counter).matches("[a-z]+: halt")) break; } else skips.put(label, skips.get(label).add(new BigInteger("-1"))); counter++; if (counter >= code.size()) { counter = 0; } } } }
BoundedBeans (talk) 02:10, 12 July 2022 (UTC)
- I added extra commands to the above implementation, these being:
- Print - takes one label as an argument and prints out its skip count
- Input - takes one label as an argument, accepts input from the user as a number, and sets the skip count to that number
- Halt - takes no arguments, stops execution
- All three of these commands can be skipped, just like the original command
BoundedBeans (talk) 02:15, 12 July 2022 (UTC)