Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python2
import BaseHTTPServer
import SimpleHTTPServer
import SocketServer
import socket
import ssl
import threading
import time
from os.path import expanduser
#!/usr/bin/swipl -f -q
:- initialization main.
% _ 1
% |_| 234
% |_| 567
% _ _ _ _ _ _ _ _
% | _| _||_||_ |_ ||_||_|| |
% ||_ _| | _||_| ||_| _||_|
@shawa
shawa / extensions.py
Created December 27, 2017 02:04
get yer cool extensions while they're hot!
import sys
import requests
URL = 'https://eventphone.de/guru2/phonebook?event=34C3&s=&page=1&format=json'
patterns = {
'doubled': lambda x: x[0] == x[1] and x[2] == x[3],
'alternate': lambda x: x[0] == x[2] and x[1] == x[3],
'end ten': lambda x: x[0] == x[1] and x[3] == '0',
'middle same': lambda x: x[1] == x[2],
@shawa
shawa / minesweeper.apl
Created November 21, 2017 18:37
Token Katas solution, in APL
⍝ An example board, the 4x4 reshape of a 16-vector
board ← 4 4 ⍴ 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0:
board
0 1 0 0
0 1 0 0
0 0 0 0
0 0 0 0
@shawa
shawa / facts.pl
Last active November 20, 2017 23:10
An Exposition of Facts
:- use_module(library(clpfd)). % grab some matrix functions
% Herein we present facts about boards.
% Given these facts, the user can consult Prolog for interesting
% inferences based on these facts.
% Example consultation:
%
% › swipl facts.pl
% Welcome to SWI-Prolog (threaded, 64 bits, version 7.6.1)
@shawa
shawa / expanding-brain.apl
Last active August 5, 2017 14:43
Checking bracket balances and nesting depth
⍝ extract only the parentheses from the character array ⍵
⍝ parens 'aa(bb)cc'
⍝ '()'
parens ← {(⍵∊'()')/⍵}
⍝ from a character array containing only '(' and ')',
⍝ map '(' to 1, and ')' to ¯1
⍝ open_close '()'
defmodule Runlength do
defp encode([], cur_char, n, cur_encoded), do: "#{cur_encoded}#{n}#{<<cur_char>>}"
defp encode([h | tail], cur_char, n, cur_encoded) do
case h do
^cur_char -> encode tail, h, n + 1, cur_encoded
_ -> encode tail, h, 1 , "#{cur_encoded}#{n}#{<<cur_char>>}"
end
end
def encode(string) do
@shawa
shawa / GPA.py
Created May 10, 2017 19:42
Maki will go to college we pray
import sys
infile = sys.argv[1]
with open(infile, 'r') as f:
triples = (line.split(',') for line in f)
GRADES = [(int(t[1]), int(t[2])) for t in triples]
total_credits = sum(credits for (credits, _) in GRADES)
@shawa
shawa / Katas_santa.apl
Last active March 25, 2020 18:00
Attempt at solving Katas with Linear Algebra in APL, dear me
⍝ A rotation matrix, rotates coordinates ⍵ rads
rotate ← {3 3⍴((2○⍵)(-1○⍵)0(1○⍵)(2○⍵)0 0 0 1)}
⍝ A translation matrix, displaces coordinates ⍵
⍝ steps downward (moves us ⍵ steps north)
translate ← {3 3⍴(1 0(-⍵)0 1 0 0 0 1)}
⍝ Lookup function to convert 'L' and 'R' to
⍝ rotation angles
pi ← ○1
@shawa
shawa / katas_santa.py
Last active April 27, 2017 22:12
Linear Algebra-based solution, using linear transformations
from numpy import sin, cos, array, pi
from operator import matmul
from functools import reduce
def move(theta, s):
"""Generate a translation and rotation matrix from a given angle theta
and displacement s. The transformation rotates the whole coordinate
system either left or right, then moves us `s` steps north
Given a vector v = [ x y 1 ], and a tranformation matrix A,