Skip to content

Instantly share code, notes, and snippets.

View bremac's full-sized avatar

Brendan MacDonell bremac

  • Sight Machine
  • San Francisco, California
View GitHub Profile
shift ← {(⍺×⍳↑⍴⍵)⌽⍵}
adjacent ← {∨/, 2∧⌿(1 shift ⍵),(¯1 shift ⍵)}
runends ← {(⍵=0)∧⍵≠(¯1⌽⍵)}
runlength ← {(↑lengths),2{⍵-⍺}/lengths←(runends ⍵)/+\⍵}
verticals ← {⍵∧(¯1⊖⍵)∨1⊖⍵}
ships ← {(runlength ,⍵≠v) , runlength ,⍉v←verticals ⍵}
allpresent ← {1 1 1 1 2 2 2 3 3 4 ≡ s[⍋s←ships ⍵]}
validate ← {(allpresent g) ∧ ~adjacent g←(0,0⍪⍵⍪0),0}
@bremac
bremac / rle.apl
Created September 27, 2016 06:39
Run-length encoding in APL
s ← 'AAABABBBBSSWSWSSSSSSS'
runEnds ← (2 ≠/ s) , 1 ⍝ 1 if an element ends a run
counts ← ¯2 -/ 0 , runEnds / ⍳⍴ s ⍝ num elements in each run
counts ,¨ runEnds / s
3 A 1 B 1 A 4 B 2 S 1 W 1 S 1 W 7 S
@bremac
bremac / bsearch-huge-file.py
Last active May 17, 2017 21:35
Approximate search for a prefixed line in a sorted file (ex. a huge log file)
import os.path
import sys
def bsearch(prefix, filename, steps, lines):
lo = 0
hi = os.path.getsize(filename)
with open(filename, 'r') as fp:
while steps > 0:

Keybase proof

I hereby claim:

  • I am bremac on github.
  • I am bmacdonellsm (https://keybase.io/bmacdonellsm) on keybase.
  • I have a public key ASDakYrO46mNrmDDwJzh_rIr1RY0JrU_ceGqOp9RRnM6_Qo

To claim this, I am signing this object: