Skip to content

Instantly share code, notes, and snippets.

@chaoxu
Last active September 2, 2016 04:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaoxu/3fcae8a646f151ffebc6ebe7f0c6cdc1 to your computer and use it in GitHub Desktop.
Save chaoxu/3fcae8a646f151ffebc6ebe7f0c6cdc1 to your computer and use it in GitHub Desktop.
1. mean
+/⍵÷(≢⍵)
2. median
{.5×+/(⍵,⍵)[(⍋⍵,⍵)[(≢⍵),1+≢⍵]]}(⌊/⍵),⍵,⌈/⍵
3. mode
{(⍵[;2]=⌈/⍵[;2])/⍵[;1]}{⍺,⍴⍵}⌸,
4. Write a dfn that takes vectors as its left and right arguments and returns them "meshed" into a single vector
formed by alternately taking successive elements from each argument. The arguments do not have to be the
same length.
{A ← (⍴⍺)⌊⍴⍵ ⋄ (,⍉↑A↑¨⍺ ⍵),⊃,/A↓¨⍺ ⍵}
5. element appears exactly once
{(⍵[;2]=1)/⍵[;1]}{⍺,⍴⍵}⌸
6. sort elements by array length
{⍵[⍋≢¨⍵]}
7. divisible by 3 and 5
{(0=(3|⍵)⌊5|⍵)/⍵}
8. negative and then positive
{((⍵<0)/⍵) (⍵≥0)/⍵}
9. deliminate text
{1↓¨(A∊⍺)⊂A←(⊃⍺),⍵}
10. dot product
+.×
BBFS←{ ⍝ bidirectional BFS, saves time and space
n←≢⍵
lis←(,(⍳n)∘.<⍳n)/,(⍳n)∘.,(⍳n) ⍝ list of reversal operations
tb←↑(⊂⍳n)reverse¨lis ⍝ table of all possible reversals
⍝ ⍺[1] is consist seen vertices and their corresponding tree edges
⍝ ⍺[2] is the new wave of vertices
(1 2⍴⍺(1 1))(1⍴⊂⍺){
⍝ one more iteration of BFS for the first tree
seen←(⊃⍺)[;1]
w←2⊃⍺
m←≢w
join←w∩2⊃⍵ ⍝ check if there is an intersection
0<≢join:((⊃⍺)(⊃⍵)(⊃join)) ⍝ output the intersection
next←(↓(m×2!n)n⍴(↑w)[;tb]),[1.5](m×2!n)⍴lis ⍝ corresponding tree edge
select←{⍵⍳(∪⍵)~seen}next[;1] ⍝ restrict to non-duplicate or unseen vertices
next←next[select;]
⍝ recurse, swap the trees
⍵ ∇(next⍪⊃⍺)(next[;1])
}(1 2⍴⍵(1 1))(1⍴⊂⍵)
}
interleave←{
a←' ',⍺ ⍝ prepend a space to make sure the first pair always match
b←' ',⍵
c←a lcs b
⍝ partition the solution by the longest common subsequence
part←{((⍳⍴⍵)∊(2≠/c)/1↓⍺)⊂⍵}
p1←(⊖⍳⍴c)part a
p2←1↓¨c part b
1↓⊃,/,⍉↑p1 p2
}
lcs←{
pos←{⊃(↓((⍳⍴⍵),[1.5]((⍴⍵)⍴⍺[2]-1))⍪⍺)[((⍺[1]>⍳⍴⍵)∧⍺[2]=⍵)⍳1]}
tab←⍺ table ⍵ ⍝ build the longest common subsequence table
l←⌈/,tab ⍝ the length of the longest common subsequence
⊃¨((1+≢⍵)l)pos scanl⊖↓tab
}
maxMult←{⊃{(⌈/⍵),|(⍵=⌈/⍵)/⍺}/↓⍉{⍺,⍴⍵}⌸,⍺∘.-⍵}
path←{
V←⍵[;1]
E←⍵[;2]
⍬{
e←E[V⍳⍵] ⍝ find the edge
e≡(⊂(1 1)):⍺ ⍝ edge is a loop, so we are at the root vertex
v←⊂(⊃⍵)reverse(⊃e) ⍝ follow the edge to the parent vertex
(⍺,e)∇ v ⍝ recurse
}⍺
}
reversals←{
z←(⍵ BBFS ⍺) ⍝ build two BFS trees and pick some intersection of the tree
pa←(z[3])path⊃z ⍝ compute paths from the intersection to the first root
pb←(z[3])path 2⊃z ⍝ compute paths from the intersection to the second root
{(≢⍵)⍵}{(⊖⊃⍵),2⊃⍵}((⍴pa)≡⍴pb)⊖(pa pb) ⍝ join the paths
}
reverse←{
r←⍺
ind←(⍵[1]-1)+⍳1+⍵[2]-⍵[1]
r[ind]←⍺[⊖ind]
r
}
table←{
⍝ build the longest common subsequence table
g←{⍺⌈1+(¯1,0,⍺)[(⌈\⍵×⍳≢⍵)+1]}
⍝ this is a modification of the standard dynamic programming algorithm
⍝ it allows us to do row by row computations
⍝ scan over the rows from the left
{⍵-0=⍵}(⍺∘.=⍵)×↑1↓((≢⍵)⍴0)g scanl↓⍺∘.=⍵
}
scanl←{⎕ML←0 ⍝ Scan from the left. took this from dfns
2>⊃⌽⍴⍵:⍵
⌽↑⍺⍺{
(⊂(⊃⍵)⍺⍺ ⍺),⍵
}/(⌽⍵),⊂⊂⍺
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment