Skip to content

Instantly share code, notes, and snippets.

View bennuttall's full-sized avatar

Ben Nuttall bennuttall

View GitHub Profile
@bennuttall
bennuttall / conway.py
Last active October 13, 2015 20:18
Conway's Game of Life
def evolve_cell(alive, neighbours):
if alive:
return neighbours in [2, 3]
return neighbours == 3
def count_neighbours(alive_cells, position):
r, c = position
neighbours = [(r - 1, c - 1), (r - 1, c + 0), (r - 1, c + 1),
(r + 0, c - 1), (r + 0, c + 1),
(r + 1, c - 1), (r + 1, c + 0), (r + 1, c + 1)]
@bennuttall
bennuttall / dict_comp.py
Last active December 11, 2015 19:49
Comprehension in Python
my_dict = {i: i ** 2 for i in range(1, 11)}
-> {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
my_dict[7]
-> 49
@bennuttall
bennuttall / bp.py
Last active December 11, 2015 19:58
Hack Manchester blog post code snippets
{
birthday: '18-12',
name: 'Brad Pitt',
movies: ['Fight Club', 'Inglourious Basterds']
}
@bennuttall
bennuttall / installs.sh
Created January 28, 2013 01:10
Code snippets from Bash Batch Install Basics blog post
apps=(
vim
git
tree
php5
)
# Loop over apps and install each one with default 'yes' flag
<?php
$thing = $_GET['thing'];
$md5 = md5($thing);
$col1 = substr($md5, 0, 6);
$col2 = substr($md5, 6, 6);
$col3 = substr($md5, 12, 6);
@bennuttall
bennuttall / bar_chart.py
Last active March 9, 2016 18:21
Code snippets from ASCII Bar Charts blog post
2000 ||
2001 |||||||||
2002 ||||||||||
2003 |||||||||
2004 ||||||||||||||
2005 |||||||||||
2006 ||||||||
2007 ||||||||||
2008 ||||||||||||||
2009 |||||||||||||||||||
@bennuttall
bennuttall / array.php
Last active December 11, 2015 19:58
Code snippets from PHP 5.4 blog post
<?php
// PHP 5.3
$arr = array();
$arr2 = array('a','b','c');
$arr3 = array('a' => 2, 'b' => 4, 'c' => 8);
$arr4 = array(array('abc','def'), array(2,4,8), 16);
// PHP 5.4
$arr = [];
@bennuttall
bennuttall / add_if_condition.py
Created January 28, 2013 01:58
Code snippets from Ternary Operator & Shorthand Code blog post
if x > 0:
var += 1
@bennuttall
bennuttall / questions.py
Created June 13, 2013 23:33
Answers to some short fizzbuzz-style interview questions - found via @jackweirdy on Stack Overflow: http://stackoverflow.com/questions/117812/alternate-fizzbuzz-questions/117891#117891
#1 reverse a string
assert 'Hello'[::-1] == 'olleH'
#2 reverse a sentence
assert ' '.join('bob likes dogs'.split(' ')[::-1]) == 'dogs likes bob'
#3 find the minimum in a list
assert min([10, 8, 6, 7, 3, 4]) == 3
#4 find the maximum in a list
@bennuttall
bennuttall / grid.py
Last active December 20, 2015 14:59
Fill an n x n grid with incrementing numbers, in a clockwise spiral
class Grid:
def __init__(self, n):
self.n = n
self.size = pow(n, 2)
self.filled_in = set()
self.next = 1
self.grid = []
for i in range(n):
self.grid.append(['0'] * n)