Skip to content

Instantly share code, notes, and snippets.

View danverbraganza's full-sized avatar

Danver Braganza danverbraganza

View GitHub Profile
@danverbraganza
danverbraganza / connect4.py
Last active May 25, 2017 02:36
Another tiny python game
licence, itertools, board, vectors, get, put, coloured, board_string, winner = 'MIT', __import__('itertools'), [[] for _ in xrange(7)], [(0, 1), (1, 0), (1, 1), (1, -1)], lambda i, j: board[i][j] if 0 <= i < 7 and len(board[i]) > j >= 0 else ' ', lambda i, p: board[i].append(p) or True if 0 <= i < 7 and len(board[i]) < 6 else False, (lambda string, colour: {'red': '\033[1;31m{}\033[1;m', 'yellow': '\033[1;33m{}\033[1;m', 'blue': '\033[1;34m{}\033[1;m'}.get(colour, '{}').format(string)) if not __import__('os').getenv('NOCOLOR') else lambda string, colour: string, lambda: '\n'.join(coloured(' | ', 'blue') + coloured(' | ', 'blue').join(get(column, row) for column in range(7)) + coloured(' | ', 'blue') for row in range(5, -1, -1)) + '\n' + coloured(' | ', 'blue') + coloured(' | ', 'blue').join(coloured('{}', 'blue').format(i + 1) for i in range(7)) + coloured(' | ', 'blue'), lambda: any(all(get(i, j) != ' ' and get(i, j) == get(i + vi * step, j + vj * step) for step in range(4)) for vi, vj in vectors for j in ra
c,g,s,m,l,p=''.join(x for x in __import__('random').choice(list(open('/usr/share/dict/words'))).upper() if x.isalpha()),set(),'|===\n| |\n|{3} {0} {5}\n| {2}{1}{4}\n| {6} {7}\n| {8} {9}'.format,list(r'QT-\-//\||'),10,' '
while not all(q in g for q in c) and l:_,l=[g.add(x) for x in raw_input('%s %s left\n%s\n%s:'%(p.join(sorted(g)),l,s(*(m[:-l]+[p]*l)),p.join(q if q in g else '_' for q in c))).upper() if x.isalpha()],max(10-len(g-set(c)),0)
print 'You',['win!','lose!\n'+s(*m)][not l],'\nWord was',c
@danverbraganza
danverbraganza / hangman.py
Last active April 20, 2017 18:52
Hangman implemented in 3 lines of Python! For an explanation of how this works, see http://danverbraganza.com/writings/hangman-in-3-lines-of-python
license, chosen_word, guesses, scaffold, man, guesses_left = 'https://opensource.org/licenses/MIT', ''.join(filter(str.isalpha, __import__('random').choice(open('/usr/share/dict/words').readlines()).upper())), set(), '|======\n| |\n| {3} {0} {5}\n| {2}{1}{4}\n| {6} {7}\n| {8} {9}\n|', list('OT-\\-//\\||'), 10
while not all(letter in guesses for letter in chosen_word) and guesses_left: _, guesses_left = map(guesses.add, filter(str.isalpha, raw_input('%s(%s guesses left)\n%s\n%s:' % (','.join(sorted(guesses)), guesses_left, scaffold.format(*(man[:10-guesses_left] + [' '] * guesses_left)), ' '.join(letter if letter in guesses else '_' for letter in chosen_word))).upper())), max((10 - len(guesses - set(chosen_word))), 0)
print 'You', ['lose!\n' + scaffold.format(*man), 'win!'][bool(guesses_left)], '\nWord was', chosen_word
@danverbraganza
danverbraganza / .bashrc
Created November 2, 2014 23:52
Addenda to my .bashrc file.
# I have tried to fork-bomb myself in the past.
ulimit -u 1000
export ANDROID_HOME=/usr/local/android/sdk
export PATH=$PATH:/usr/local/go/bin:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
alias set_gopath='export GOPATH=$(pwd)'
export EDITOR=emacs
export GOPATH=~/projects/go
@danverbraganza
danverbraganza / rc.lua
Created November 2, 2014 03:34
My personal rc.lua file.
-- Standard awesome library
require("awful")
require("awful.autofocus")
require("awful.rules")
-- Theme handling library
require("beautiful")
-- Notification library
require("naughty")
-- Load Debian menu entries
@danverbraganza
danverbraganza / .emacs
Last active October 4, 2015 11:37
My personal .emacs file for convenience.
(when (load "flymake" t)
(defun flymake-pyflakes-init ()
(let* ((temp-file (flymake-init-create-temp-buffer-copy
'flymake-create-temp-inplace))
(local-file (file-relative-name
temp-file
(file-name-directory buffer-file-name))))
(list "pyflakes" (list local-file))))
(add-to-list 'flymake-allowed-file-name-masks
@danverbraganza
danverbraganza / connect4.py
Created March 10, 2012 22:03
Dynamic programming solution for finding the longest sequence of length 2^n in a m x o matrix (Winner
'''Expressing "Find the winner in connect 4" as a dynamic programming
problem.
'''
import json
lines = []
while True:
try:
lines.append(raw_input())
except EOFError:
@danverbraganza
danverbraganza / setup.py
Last active October 1, 2015 10:07
Standard starting for a file.
from setuptools import setup, find_packages
setup(
name = 'Undefined',
version = '0.0.0',
author = 'Danver Braganza',
author_email = 'danverbraganza@gmail.com',
description = ('A description of my project goes here.'),
license = 'WTFPL',
# keywords = 'space separated',
@danverbraganza
danverbraganza / tox.ini
Created January 26, 2012 00:54
My standard tox.ini file that allows you to run coverage, lettuce, nosetests and lint, and to pick out a given feature or a module for nosetest (So that you don't have to run the whole suite)
[tox]
envlist=py27,lint
[testenv]
downloadcache={homedir}/.pipcache
distribute=True
sitepackages=False
[testenv:py27]
deps=nose