Skip to content

Instantly share code, notes, and snippets.

for case in range(input()):
print "Case #%d:" % (case+1)
r,c,m = map(int, raw_input().split())
flip = r>c
if flip: c,r = r,c
rem = r*c-m
if r==1 or rem==1:
res = ()
elif r>1 and rem in {2,3,5,7} or (r==2 and rem%2):
print "Impossible"
@Nabb
Nabb / sudoku.py
Created July 12, 2012 03:35
Sudoku Solver
# Sudoku solver for Udacity CS258.
def get_groups():
q = lambda f:[[f(i,j) for j in range(9)] for i in range(9)]
return q(lambda i,j:(i,j)) + q(lambda i,j:(j,i)) + q(lambda i,j:(i/3*3+j/3,i%3*3+j%3))
# I think this is more readable than the one-liner:
# >> return [l for i in range(9) for l in zip(*[[(i,j),(j,i),(i/3*3+j/3,i%3*3+j%3)] for j in range(9)])]
def check_sudoku(grid):
if len(grid) != 9: return None
@Nabb
Nabb / gist:2396963
Created April 16, 2012 07:40
Whitespace Assembler
"":d;
[]:VAR;
{|2>~2base(;{" \t"=}%n+}:LB;
{|~F}:N;{..abs<\2base+{" \t"=}%+"\n"+}:F;
{n%{{1<"#"=!}1$!!*},{"|"%~}%{
:|' '/0=.58,48>-\,1>{'-'-}*"PUSH"or:I;[
["PUSH"{" "N}]
@Nabb
Nabb / gist:1376606
Created November 18, 2011 14:34
NFA in Golfscript
# This is a response to http://swizec.com/blog/strangest-line-of-python-you-have-ever-seen
# Since the only input in golfscript is stdin, we'll assume the strings to test are on the last line of stdin, separated by spaces.
# 82B -- The first implementation avoids making assumptions about the data, prints one line for each input. Formatted as e.g. `ab YES'
n%)" ":s/\:|;{s|(s/(;3$1/{:c;{s+c+2|>\`{?)}+,~}%{.-1%s?~)>}%}/\0=s/&!"YNEOS">2%n}/
# 71B -- Simplifying the output and printing just the matching strings saves 11B. Formatted as e.g. `["ab" "abb"]'
n%)" ":s/\:a;{a(s/(;@1/{:c;{s+c+2a>\`{?)}+,~}%{.-1%s?~)>}%}/\0=s/&!!},p