Skip to content

Instantly share code, notes, and snippets.

View mthelander's full-sized avatar

Michael Thelander mthelander

  • Comscore
  • Portland, OR
View GitHub Profile
@mthelander
mthelander / gitmergeopen
Created October 1, 2013 18:15
Open in $EDITOR all files with merge conflicts
function gitmergeopen() {
eval "$EDITOR \$(git diff --name-status --diff-filter=U | sed -re 's/^.+\s//')"
}
@mthelander
mthelander / gitopen
Created August 8, 2012 18:48
gitopen function
function gitopen() {
if [ -z $1 ]; then # Open all changed files
eval "$EDITOR \$(git status --porcelain --untracked-files=all | sed -re 's/^.+\s//')"
else # All files changed in the last number of specified commits
eval "$EDITOR \$(git show -$1 --name-only --oneline | sed -re '/^.*[0-9]+.*/d')"
fi
}
@mthelander
mthelander / morse_code_decoder
Created March 19, 2012 03:10
Morse code decoder
# Expects characters to be separated by a single space, with words separated by three spaces.
def morse_to_eng(morse)
t,r,n = [32,69,84,73,65,78,77,83,85,82,87,68,75,71,79,72,86,70,95,76,95,80,74,66,88,67,89,90,81,95,95],[],0
morse.bytes do |b|
n = case b
when 46, 45 then 2 * n + 1 + (46 - b)
else r << t[n] unless n == 0 && r[-1] == 32; 0
end
end
r.pack('c*')
@mthelander
mthelander / git open previous
Created January 24, 2012 17:19
Open all files changed in previous commit
vim $(git show --name-only --oneline | sed -re "/^[0-9]+.*/d")
@mthelander
mthelander / gitopen
Created January 10, 2012 23:44
Bash script to open all modified/untracked files for the current git repository
vim $(git status --porcelain --untracked-files=all | sed -re "s/^.+\s//")
@mthelander
mthelander / palindrome_finder.rb
Created January 10, 2012 04:14
palindrome finder
# Finds longest palindromic substring in O(m*n) time
def find_longest_palindrome(string)
matrix, s, size = {}, string.reverse, string.size
0.upto(size-1) do |x|
0.upto(size-1) do |y|
matrix[[x,y]] = (matrix[[x-1,y-1]] || 0) + 1 if string[x] == s[y]
end
end
string[matrix.key(m=matrix.values.max).first-m+1, m]
end
@mthelander
mthelander / xor.rb
Created January 8, 2012 03:39
string xor cipher
class String
def xor(key)
''.tap do |result|
self.size.times do |n|
result << (self[n].ord ^ key[n % key.size].ord).chr
end
end
end
end
@mthelander
mthelander / palindromes.rb
Created January 8, 2012 01:37
longest palindrome finder
def find_longest_palindrome(string)
(string.length/2).downto(1) do |i|
backrefs = "\\#{(-i..-1).map(&:abs).join("\\").sub(i.to_s, "#{i}?")}"
string.scan(/#{'(.)'*i}(#{backrefs})/).tap do |a|
return a.take(i).join unless a.empty?
end
end
end