Skip to content

Instantly share code, notes, and snippets.

View andrewkowalik's full-sized avatar
👋

Andrew Kowalik andrewkowalik

👋
View GitHub Profile
@andrewkowalik
andrewkowalik / gist:5608882
Created May 19, 2013 20:36
Recursive range between two numbers passed as parameters
def range(start_num,end_num)
return [end_num] if start_num == end_num
nums = []
if start_num > end_num
nums += [start_num] + range(start_num-1,end_num)
else
nums += [start_num] + range(start_num+1,end_num)
end
nums
end
@andrewkowalik
andrewkowalik / gist:5610034
Created May 20, 2013 02:09
Generates an array of prime numbers
def prime(num)
return [] if num < 2
primes = []
2.upto(num) { |x| primes[x] = x}
2.upto(Math.sqrt(num)) { |x| primes[x] ? (x*x).step(num,x) { |x2| primes[x2] = nil } : next}
primes.compact
end
@andrewkowalik
andrewkowalik / gist:5612545
Created May 20, 2013 14:21
Ruby implementation of quick_sort.
def quick_sort(nums)
return nums if nums.length == 1
mid = nums.length / 2
left = []
right = []
0.upto(nums.length-1) do |i|
if nums[i] <= nums[mid]
left << nums[i]
@andrewkowalik
andrewkowalik / gist:5612602
Created May 20, 2013 14:33
Calculate powers using ruby recursively
def powers_rec(num,power)
return num if power == 1
total = num * powers_rec(num,power-1)
end
@andrewkowalik
andrewkowalik / gist:5625633
Last active December 17, 2015 14:39
Josephus Problem, add one to method return.
def josephus(count, skip)
#catch errors
return 0 if count == 1
#base case
return count-1 if skip == 1
#generally calls base case
if skip>count
return (josephus(count-1,skip)+skip) % count
@andrewkowalik
andrewkowalik / chess stalemate
Created May 22, 2013 17:35
Chess stalemate moves
# c2 c4
# h7 h5
# h2 h4
# a7 a5
# d1 a4
# a8 a6
# a4 a5
# a6 h6
# a5 c7
@andrewkowalik
andrewkowalik / gist:5702775
Created June 4, 2013 00:51
SQLZoo SELECT from Nobel Tutorial #8 solution using single join, no sub-query
SELECT DISTINCT a.yr
FROM nobel a
LEFT OUTER JOIN nobel b
ON a.yr=b.yr
AND b.subject NOT IN ('Economics','Literature','Medicine','Peace','Physics')
WHERE b.subject IS NULL
AND a.subject = 'Physics'
@andrewkowalik
andrewkowalik / .sqliterc
Created June 12, 2013 18:24
sqlite3 defaults, save in home directoy
.headers on
.mode column
@andrewkowalik
andrewkowalik / test
Last active December 18, 2015 21:29
bind examples
function bind(func, object) {
return function () {
return func.apply(object, arguments);
};
}
Function.prototype.myBind = function (context) {
var that = this;
return function () {
return that.apply(context, arguments);
https://github.com/addyosmani/backbone-fundamentals