Skip to content

Instantly share code, notes, and snippets.

View compwron's full-sized avatar
💭
https://github.com/drop-ice/dear-github-2.0

compwron compwron

💭
https://github.com/drop-ice/dear-github-2.0
View GitHub Profile
@compwron
compwron / gist:eaa59d38c33e86f923ca
Created December 9, 2014 03:49
howto set all input fields from hash in ruby
def initialize(field_data)
field_data.each do |name, value|
instance_variable_set("@#{name}", value)
end
end
# You are asked to write a max_slice function which takes as an input an array of integers, and returns the slice with the largest sum of the elements
a = [1, 2, 3, 4, 5]
b = [1, -2, 6, -4, 5]
[1]
[1, -2]
[1, -2, 6]
[-2, 6]
@compwron
compwron / gist:a64972f2e3c527405923
Created January 9, 2015 19:08
alpha params finder
filename = ARGV[0] || 'app/models/transaction.rb'
f = File.open(filename, 'rb')
c = f.read
all = c.split(/\n|,/).map { |i| i.strip.gsub(/[^A-Za-z0-9\s\[\]\._\(\)]/, '') }.reject(&:empty?)
# too many false positives
DEFAULT = 'aaa --- DEFAULT --- '
alph_list = [DEFAULT]
all.each do |line|
if [alph_list.last, line].sort.last == line # this line is after everything in alph list alphabetically
@compwron
compwron / gist:d883710cf84bbd213eed
Last active August 29, 2015 14:13
matching parens
# Task: write a function to determine if a string is
# 'balanced'. Balanced means that opening and closing
# brackets match each other in the proper order. Other
# characters are possible and should be handled. The
# possible characters for balancing are {}, (), and []
# Example Input:
# '{}' -> True
# '{[]}' -> True
@compwron
compwron / gist:115a7cf9aaa9d5271e2a
Last active August 29, 2015 14:13
sum2 in n*2
def sum2 choices, result
arrays = []
choices.each_with_index {|i, index|
choices.each_with_index {|j, index2|
break if index == index2
arrays << [i, j] if i + j == result
}
}
arrays
end
@compwron
compwron / gist:65f8a1eedffe9cd2d208
Created January 23, 2015 16:20
max diff in moving subset window
def max_devi(choices, setlength) # v, d
largest_diff = 0
largest_diff_subset = []
(0..choices.length - setlength).each do|i|
subset = choices[i..i + setlength]
diff = subset.max - subset.min
if diff > largest_diff
largest_diff_subset = subset
largest_diff = diff
end
class OrbitalCoordinate
def initalize(lat, long, altit)
@lat = lat
@long = long
@altit = altit
# @veloc = veloc # meters per second
# how many seconds it will take to get to another point.
OrbitalVelocity..new(sec, OrbitalCoordinate)
end
end
l = [
"John",
"John",
"John",
"John",
"Adam",
"Adam",
"Adam",
"Linda",
@compwron
compwron / xit_finder.rb
Last active August 29, 2015 14:14
find xits in ruby tests
xits = `grep -Rwn "xit |skip" spec | grep " do"`.split("\n")
skips = `grep -Rwn "skip " spec | grep -v ".js" | grep -v ":skip"`.split("\n")
blame_cmds = (xits + skips).map {|x|
pieces = x.split(":")
file = pieces[0]
line_num = pieces[1]
[x, "git blame -L#{line_num},+1 -- #{file}"]
}