Skip to content

Instantly share code, notes, and snippets.

@kevinlin1
Last active March 19, 2022 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinlin1/3b55408c842fa5fcefa308034352f195 to your computer and use it in GitHub Desktop.
Save kevinlin1/3b55408c842fa5fcefa308034352f195 to your computer and use it in GitHub Desktop.
Send file contents to Python Tutor's Java 8 visualizer: node visualize.js [code file] [stdin file]
<html>
<head>
<script>
let data = location.hash;
if (data.includes('java.util.')) {
const hasSet = data.includes('Set');
const hasMap = data.includes('Map');
const hasQueue = data.includes('Queue');
if (hasSet || hasMap || hasQueue) {
const repl = {
...(hasSet && {
'Set': 'SET',
'SortedSet': 'SET',
'HashSet': 'SET',
'TreeSet': 'SET',
}),
...(hasMap && {
'Map': 'ST',
'SortedMap': 'ST',
'HashMap': 'ST',
'TreeMap': 'ST',
'containsKey': 'contains',
'keySet': 'keys',
'remove': 'delete',
}),
...(hasQueue && {
'LinkedList': 'Queue',
'add': 'enqueue',
'remove': 'dequeue',
}),
};
data = data.replace(new RegExp(Object.keys(repl).join('|'), 'g'), m => repl[m]);
}
if (hasSet) {
data += '%0Aclass%20SET%3CKey%20extends%20Comparable%3CKey%3E%3E%20implements%20Iterable%3CKey%3E%20%7B%0A%20%20%20%20ST%3CKey%2C%20Boolean%3E%20map%20%3D%20new%20ST%3C%3E%28%29%3B%0A%0A%20%20%20%20void%20add%28Key%20key%29%20%7B%0A%20%20%20%20%20%20%20%20map.put%28key%2C%20true%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20boolean%20contains%28Key%20key%29%20%7B%0A%20%20%20%20%20%20%20%20return%20map.contains%28key%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20void%20remove%28Key%20key%29%20%7B%0A%20%20%20%20%20%20%20%20map.delete%28key%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20void%20delete%28Key%20key%29%20%7B%0A%20%20%20%20%20%20%20%20remove%28key%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20Iterator%3CKey%3E%20iterator%28%29%20%7B%0A%20%20%20%20%20%20%20%20return%20map.keys%28%29.iterator%28%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20int%20size%28%29%20%7B%0A%20%20%20%20%20%20%20%20return%20map.size%28%29%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20boolean%20isEmpty%28%29%20%7B%0A%20%20%20%20%20%20%20%20return%20size%28%29%20%3D%3D%200%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20static%20%3CKey%20extends%20Comparable%3CKey%3E%3E%20SET%3CKey%3E%20of%28Key...%20keys%29%20%7B%0A%20%20%20%20%20%20%20%20SET%3CKey%3E%20result%20%3D%20new%20SET%3C%3E%28%29%3B%0A%20%20%20%20%20%20%20%20for%20%28Key%20key%20%3A%20keys%29%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20result.add%28key%29%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20return%20result%3B%0A%20%20%20%20%7D%0A%7D';
}
}
location.replace('https://pythontutor.com/java.html' + data + '&mode=display')
</script>
</head>
</html>
class SET<Key extends Comparable<Key>> implements Iterable<Key> {
ST<Key, Boolean> map = new ST<>();
void add(Key key) {
map.put(key, true);
}
boolean contains(Key key) {
return map.contains(key);
}
void remove(Key key) {
map.delete(key);
}
void delete(Key key) {
remove(key);
}
public Iterator<Key> iterator() {
return map.keys().iterator();
}
int size() {
return map.size();
}
boolean isEmpty() {
return size() == 0;
}
static <Key extends Comparable<Key>> SET<Key> of(Key... keys) {
SET<Key> result = new SET<>();
for (Key key : keys) {
result.add(key);
}
return result;
}
}
const fs = require('fs');
function printLink(text, url) {
console.log('\033]8;;' + url + '\007' + text + '\033]8;;\007');
}
const url = 'https://pythontutor.com/java.html#' + new URLSearchParams({
code: fs.readFileSync(process.argv[2], 'utf8'),
...(process.argv[3] && { stdin: fs.readFileSync(process.argv[3], 'utf8') })
});
printLink('Open the Visualizer', url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment