Skip to content

Instantly share code, notes, and snippets.

@codemaniac
codemaniac / powerset.py
Created June 27, 2012 17:22
Powerset in python using list comprehensions
#!/usr/bin/python
# -*- coding: utf-8 -*-
def powerset(s):
return [[s[j] for j in xrange(len(s)) if (i&(1<<j))] for i in xrange(1<<len(s))]
s = ['a','b','c','d']
print powerset(s)
@codemaniac
codemaniac / datetime2unixticks.py
Created June 27, 2012 17:25
MySQL datetime to unix ticks in python
#!/usr/bin/python
# -*- coding: utf-8 -*-
from datetime import datetime
import time
def datetime2unixticks(ts):
ts = ts.split()[0]
dt = datetime.strptime(ts, '%Y-%m-%d')
return int(time.mktime(dt.timetuple()))
@codemaniac
codemaniac / ValueFileParser.py
Created June 27, 2012 17:26
property file/value file parser in python
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
class _ValueFileParserError(Exception):
pass
class ValueFileParser:
def __init__(self, path):
@codemaniac
codemaniac / str-proper-case.js
Created July 13, 2012 08:47
Javascript function to convert a string to Proper/Title case
(function($, undefined){
String.prototype.toProperCase = function () {
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
};
})(jQuery);
abstract class Person {
String name;
Person(String name) {
this.name = name;
}
}
interface ICatchPlayer<T extends Throwable> {
void throww(T object);
}
@codemaniac
codemaniac / subcypher.py
Last active December 15, 2015 06:08
substitution cypher with DSL
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
literals = string.printable
HOP_FORWARD = 1
HOP_BACKWARD = -1
function AttachmentCtrl($scope, $location, $timeout, Docs) {
$(function() {
$('#detail-form-doc').fileupload({
dataType: 'json',
url: '/angular-ib/app/fileupload?id=' + $location.search().id,
add: function(e, data) {
$scope.$apply(function(scope) {
// Turn the FileList object into an Array
for (var i = 0; i < data.files.length; i++) {
$scope.project.files.push(data.files[i]);
day = 1
month = 12
year = 1988
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
is_leap_year = lambda y : y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
leap_years = len([y for y in xrange(1, year) if is_leap_year(y)])
normal_years = year - 1 - leap_years
package sandbox;
import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystem;
@codemaniac
codemaniac / dawg.py
Last active September 21, 2015 10:31 — forked from smhanov/dawg.py
Use a DAWG as a map
#!/usr/bin/python3
# By Steve Hanov, 2011. Released to the public domain.
# Updated 2014 to use DAWG as a mapping.
import sys
import time
DICTIONARY = "/usr/share/dict/words"
QUERY = sys.argv[1:]
# This class represents a node in the directed acyclic word graph (DAWG). It