Skip to content

Instantly share code, notes, and snippets.

View kvchen's full-sized avatar

Kevin Chen kvchen

View GitHub Profile
@kvchen
kvchen / wordmaster.py
Last active August 29, 2015 14:03
Wordmaster Solver
#!/usr/bin/env python
import string
DICT_FILE = '/usr/share/dict/words'
def get_matches(word, guess):
word_set = set([c for c in word])
guess_set = set([c for c in guess])
return len(word_set.intersection(guess_set))
@kvchen
kvchen / iterative_hanoi.py
Last active August 29, 2015 14:03
Iterative Towers of Hanoi
# !/usr/bin/env python
# coding: utf-8
# http://en.wikipedia.org/wiki/Tower_of_Hanoi#Binary_solution
def iterative_hanoi(n):
# Optimal play will result in (2^n)-1 moves. This is a pretty fun proof!
total_moves = pow(2, n) - 1
for i in range(total_moves):
start = (i & i+1) % 3
end = ((i | i+1)+1) % 3
@kvchen
kvchen / gist:57d0e27af7969b962118
Created November 10, 2014 22:04
Newline Fixer
"""Fixes newline endings created in Windows. Run in Unix."""
for i in range(1, 17):
output_path = '../output{}.txt'.format(i)
with open(output_path, 'r') as output_file:
sequence = output_file.readline().strip()
with open(output_path, 'w') as output_file:
output_file.write(sequence + '\n')
Here's the current folder hierarchy:
- run.py
- util/
- __init__.py
- entry.py
- winrate.py
Let's say winrate.py straight up imports entry:
def copy(lst):
"""Returns a copy of the linked list."""
if lst.rest is Link.empty:
return Link(lst.first)
else:
return Link(lst.first, copy(lst.rest))
def repeat(lst, n):
if n == 0:
return Link.empty
from bs4 import BeautifulSoup
import json
def main():
with open('messages.htm', 'r') as infile:
soup = BeautifulSoup(infile)
bchus = []
import string
def fix(s):
return s.strip().lower().translate(string.maketrans('', ''), string.punctuation)
print(fix(" He@#@LL%^O WorL@#D@$"))
Promise = require 'bluebird'
bcrypt = Promise.promisifyAll(require 'bcrypt')
bookshelf = require 'app/utils/db'
logger = require 'app/utils/logger'
User = bookshelf.Model.extend
tableName: 'users'
hasTimestamps: ['createdAt', 'updatedAt']
it 'should fail authentication with an incorrect password', (done) ->
userMock.create()
.then (user) ->
return user.save()
.then (model) ->
return User.login model.get('email'), 'xyzzy'
.then (match) ->
expect(match).to.be.false
done()
it 'should authenticate with correct password', (done) ->
unhashed = null
userMock.create()
.then (user) ->
unhashed = user.get 'password'
return user.save()
.then (model) ->
return User.login model.get('email'), unhashed
.then (match) ->