This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChessKnight { | |
//Possible moves | |
//for each possible way we can move in x, we have a corresponding y | |
int[] x ={-2,-2,-1,-1,1,1,2,2}; | |
int[] y = {-1,1,-2,2,-2,2,-1,1}; | |
double[][][] results; | |
private static final int BOARD_SIZE = 8; | |
public double probAfterNSteps(int x, int y, int n){ | |
init_memoize(n); //initialize our memoization array |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# not pretty, I quickly moved to another idea | |
# not involving the Twitter stream. Good starting | |
# point. | |
import re | |
import base64 | |
import socket | |
import asyncmongo | |
from tornado import ioloop | |
from tornado import iostream |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def linkify(words, urls): | |
if isinstance(words, basestring) or not isinstance(words, (list, tuple)): | |
if isinstance(urls, basestring) or not isinstance(urls, (list, tuple)): | |
return linkify([words], [urls]) | |
else: | |
return words | |
else: | |
html_links = ['<a href="{1}">{0}</a>'.format(word,url) | |
for word, url in zip(words, urls) | |
if word is not None and url is not None] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseHandler(tornado.web.RequestHandler): | |
#lazy initializer of Connection reference | |
@property | |
def db(self): | |
if not hasattr(self, '_db'): | |
self._db = Connection(options.db_master, options.db_port)[options.db_name] | |
return self._db | |
#overriding the default get_current_user to use an encrypted, pickled python object | |
def get_current_user(self): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def post(self): | |
user = self.get_argument("username") | |
pwd = self.get_argument("password") | |
login_info = self.db.logins.find_one({'$or' : [{'username': user.lower() }, {'email': user }]}) | |
if login_info is not None and bcrypt.hashpw(pwd, login_info['pwd_hash']) == login_info['pwd_hash']: | |
user = self.db.users.find_one({"user_id": login_info['user_id']}) | |
self.set_secure_cookie(options.cookie_user, pickle.dumps(user)) | |
self.redirect(self.get_argument("next","/")) | |
else: | |
self.set_secure_cookie(options.cookie_alerts, pickle.dumps([{'content': "Bad login", 'level': 'error'}])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="content"> | |
{% block alerts %} | |
{% try %} | |
{% if alerts is not None %} | |
{% for alert in alerts %} | |
<div class="{{alert.get('level', 'alert') }}"> | |
{{ alert.get('content') }} | |
</div> | |
{% end %} | |
{% end %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapBackedBean implements GroovyInterceptable { | |
private Map map = [:] | |
def invokeMethod(String name, args){ | |
if (name == "putAt"){ | |
map[args[0]] = args[1] | |
} else if (name == "getAt"){ | |
return map[args[0]] | |
} else if (args.size() > 1 || name.length() <= 3){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CSVBeanBuilder { | |
public static final Closure STRING_TO_DOUBLE = { token ->return Double.parseDouble(token)} | |
public static final Closure STRING_TO_INT = { token ->return Integer.parseInt(token)} | |
public static final Closure STRING_TO_DATE = { token -> return new Date(java.sql.Date.valueOf(token).time)} | |
def static loadAs(Class clazz, String csv, conversions){ | |
def results = [] | |
def headers = [:] | |
csv.eachLine { line, lineNumber -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test | |
import static org.junit.Assert.* | |
import org.junit.Before | |
class MapBackedBeanTest { | |
private MapBackedBean mbb | |
@Before |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The class we need | |
class Transaction { | |
String name | |
int itemCount | |
double cost | |
Date purchaseDate | |
} | |
//our conversion closures | |
def stringToDouble = { |