Skip to content

Instantly share code, notes, and snippets.

@avanishgiri
avanishgiri / merge_unique.js
Last active December 30, 2015 02:39
merge two sorted arrays, ignoring duplicates.
var merge_unique = function(array1, array2){
var len1 = array1.length;
var len2 = array2.length;
var buff = []
var i = 0, j = 0
while(i != len1 && j != len2){
if(array1[i] < array2[j])
buff.push(array1[i++]);
else
function Promise(){
this.callbacks = [];
this.value = undefined;
}
Promise.prototype.success = function(callback){
this.callbacks.push(callback);
if(this.value)
this.execute_callbacks();
}
def is_lower(c)
c.ord >= 'a'.ord && c.ord <= 'z'.ord
end
def is_upper(c)
c.ord >= 'A'.ord && c.ord <= 'Z'.ord
end
def rotx(x, string, encrypt = true)
x = encrypt ? x : -x
function Promise(){
this.callbacks = [];
this.value = undefined;
}
Promise.prototype.success = function(callback){
this.callbacks.push(callback);
if(this.value)
this.execute_callbacks();
}
### ALGORITHM ###
# for each index 0 .. last_index
#for each index i .. 0
# during each iteration of the inner loop
# we store one member of a diagonal from the upper-left half
# and the element that reflects it across the line y=x (the middle diagonal)
# the member from the upper-left half is at index [i-j][i]
# we push this member into the final array at index i
matrix = ('a'..'y').each_slice(5).to_a
def all_diagonals(m)
len = m[0].length
final = Array.new(2*len - 1){[]}
0.upto(len-1) do |i|
i.downto(0) do |j|
final[i] << m[i-j][j]
final[2*len-i-2] << m[len-1-j][j+len-i-1] if 2*len-i-2 != len-1
@avanishgiri
avanishgiri / max_sum.rb
Created November 19, 2013 08:01
dynamic programming solution to robbers problem -- runs in linear time in the size of the array. tested against my previous brute force solution.
a = [447, 462, 9, 272, 242, 0, 206, 998, 35, 414]
def max(a, i, memo = {})
return 0 if i == 0
return a[i] if i == 1
return memo[i] if memo[i]
return memo[i] = [max(a,i-2) + a[i], max(a,i-1)].max
end
p max(a, 9)
@avanishgiri
avanishgiri / index.html
Last active December 28, 2015 07:19
Problem 4 solution. Uses jQuery for DOM manipulation.
<!DOCTYPE HTML>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" src="timer.js"></script>
</head>
<input id="seconds" type="text" value="0">
<div id="countdown">
<span class="days">0 days</span>
<span class="hours">0 hours</span>
@avanishgiri
avanishgiri / ruby_solutions.rb
Last active December 28, 2015 07:09
ruby solutions to problems 1 & 2.
##################################### P1
def reverse(string)
string.length == 0 ? "" : string[-1] + reverse(string[0..-2])
end
##################################### P2
def tokenize(url)
url.split(/&/).map { |i| i.split(/=/) }
@avanishgiri
avanishgiri / hash_url.rb
Created November 14, 2013 04:47
ruby solution for converting url into dictionary
def tokenize(url)
url.split(/&/).map { |i| i.split(/=/) }
end
def dict(array_of_pairs)
array_of_pairs.inject(Hash.new{[]}) { |hash,pair| hash[pair.first] += [pair.last] ; hash }
end
def scrub(hash)
hash.map { |k,v| hash[k] = v.first if v.length == 1 }