Skip to content

Instantly share code, notes, and snippets.

@dragon0
dragon0 / sequence_ops_example.cc
Created December 1, 2017 23:41
Using sequence operations (map/filter/reduce) in C++
#include <iostream> // cout
#include <algorithm> // transform
#include <vector> // vector
using namespace std;
void print (int i) { cout << ' ' << i; }
struct c_unique {
int current;
// constructor
@dragon0
dragon0 / ImageViewer.java
Created December 1, 2017 22:29
A simple image viewer in Java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageViewer implements KeyListener{
private JFrame frame;
private ImageIcon image;
private ImageIcon origImage;
private JLabel imageLabel;
private String[] files;
private int index;
@dragon0
dragon0 / signal_handler.py
Created December 1, 2017 22:03
Debug a Python program on Ctrl-C
# Signal handler example. Debugger starts with Ctrl-C:
import signal
def int_handler(signal, frame):
import pdb
pdb.set_trace(frame)
signal.signal(signal.SIGINT, int_handler)
# Put that at the top of your script and you can start debugging your script at any
# point by type Ctrl-C. Resume programe execution by typing exit at the Pdb prompt.
@dragon0
dragon0 / perlin.py
Created December 1, 2017 22:01
Perlin noise generator based on pseudocode from http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
#!/usr/bin/env python3
'''
Perlin noise generator based on pseudocode from
http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
'''
import math
def linear_interp(a, b, x):
return a * (1 - x) + b * x
@dragon0
dragon0 / hexagons.py
Created December 1, 2017 21:55
Utilities for a hexagonal grid system
#!/usr/bin/env python3
import math
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def __repr__(self):
return 'Point({0.x}, {0.y})'.format(self)
@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
@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 / 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 / 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 / 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)