Skip to content

Instantly share code, notes, and snippets.

@dragon0
dragon0 / ScriptRunner.java
Created February 11, 2015 23:49
Simple Java Script Executor GUI
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.script.*;
public class ScriptRunner extends JFrame{
public static void main(String[] args){
new ScriptRunner();
}
@dragon0
dragon0 / ScriptEngineQuery.java
Created February 12, 2015 00:22
Print Available Java Script Engines
import javax.script.*;
public class ScriptEngineQuery{
public static void main(String[] args){
ScriptEngineManager manager = new ScriptEngineManager();
for(ScriptEngineFactory factory : manager.getEngineFactories()){
System.out.println("Engine: " + factory.getEngineName());
@dragon0
dragon0 / ScriptInterfaceCast.java
Created February 12, 2015 01:44
Cast an object defined in a script to a Java Interface
import javax.script.*;
public class ScriptInterfaceCast{
public static void main(String[] args) throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine rhino = manager.getEngineByName("javascript");
String script
= "function JSRunnable(){\n"
+ " this.run = function(){\n"
@dragon0
dragon0 / ACMAirport.java
Last active September 14, 2015 18:53
ACM Airports problem
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
@dragon0
dragon0 / resistance_is_not_futile.py
Last active September 16, 2015 21:51
Resistance Is Not Futile ACM Problem
#!/usr/bin/env python3
import math
from pprint import pprint
from collections import deque
#resistors = [10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82]
resistors = [82, 68, 56, 47, 39, 33, 27, 22, 18, 15, 12, 10]
decades = {}
# generate and cache decade lists on demand
@dragon0
dragon0 / fifty_coats_of_gray.py
Created September 12, 2015 06:33
Fifty Coats Of Gray ACM Problem
#!/usr/bin/env python3
from math import ceil
def base_area(n, width, length, height):
return n * ( 2 * width * height + 2 * length * height + length * width)
def run_case(in_it):
n, width, length, height, area, m = next(in_it).split()
n = int(n)
width = int(width)
@dragon0
dragon0 / testmain.py
Created December 1, 2017 20:58
Python: execute doctests before running
def main():
pass
if __name__ == '__main__':
import doctest
failure_count, test_count = doctest.testmod()
if failure_count == 0:
main()
@dragon0
dragon0 / 3D.py
Created December 1, 2017 21:39
Manually-rendered 3D starfield in Python 2
import pygame
import atexit
pygame.init()
atexit.register(pygame.quit)
size = width, height = 500,500
black = 0, 0, 0
white = 0xff, 0xff, 0xff
@dragon0
dragon0 / Vec3D.py
Created December 1, 2017 21:41
3D Vector utility class
########################################################################
import operator
import math
class Vec3d(object):
"""3d vector class, supports vector and scalar operators,
and also provides a bunch of high level functions.
reproduced from the vec2d class on the pygame wiki site.
@dragon0
dragon0 / game.py
Created December 1, 2017 21:53
Object-Oriented Game Design example
import pygame
from pygame.locals import *
#http://ezide.com/games/writing-games.html
DIRECTION_UP = 0
DIRECTION_DOWN = 1
DIRECTION_LEFT = 2
DIRECTION_RIGHT = 3