Skip to content

Instantly share code, notes, and snippets.

View aarti's full-sized avatar
🎯
Focusing

aarti

🎯
Focusing
  • San Francisco Bay Area
View GitHub Profile
@aarti
aarti / integer_is_binary_array
Created October 22, 2014 21:13
ruby integers can be accessed as binary arrays
irb(main):019:0> a
=> 12
irb(main):014:0> a.to_s(2)
=> "1100"
irb(main):015:0> a[0]
=> 0
irb(main):016:0> a[1]
=> 0
irb(main):017:0> a[2]
=> 1
@aarti
aarti / gist:c507aa5aa5bcfd6807f3
Last active August 29, 2015 14:10
Javascript prototype inheritance
// There are several ways to instantiate objects in Javascript and show how the prototype chaining works.
// I have demonstrated two such ways here
// Prototype inheritance Using Object.create()
house = { rooms: 4, square_foot: 2000 };
// Creates an object house, whose prototype is Object.prototype
house_in_sf = Object.create(house);
@aarti
aarti / splitstring
Created March 29, 2015 22:50
Swift global functions
var fullName = "Aarti Parikh"
var fullNameArr = split(fullName) {$0 == " "}
println(fullNameArr)
class SudokuSolver
attr_accessor :grid
def initialize
@grid = Array.new(9) { Array.new(9) }
end
def initial_values(vals)
vals.each do |v|
x,y,val = v[0],v[1],v[3]
a = [1,2,3,4,5]
n = 5
def match_sum_pairs_naive(a,n)
for i in 0..a.length-2
for j in i+1..a.length-1
return [a[i],a[j]] if a[i]+a[j]==n
end
end
end
function fibonacci(n) {
return n < 2 ? 1 : fibonacci(n-1)+fibonacci(n-2)
}
function memoize(fn) {
var cache = {}
return function(x) {
if (x in cache) {
console.log('retrieved value from cache for', x)
}
var Fib = {
compute: function(n) {
return n < 2 ? 1 : Fib.compute(n-1)+Fib.compute(n-2)
}
}
function memoize(obj,prop) {
var cache = {}
var fn = obj[prop]
return function(x) {
def max_slice(a)
max_start,max_end = 0,0
largest_sum = 0
curr_start,curr_end = 0,0
curr_sum = 0
for i in 0..a.length-1
if (curr_sum + a[i] < 0 ) then
curr_start = i+1
curr_sum = 0
elsif (curr_sum + a[i] > curr_sum) then
@aarti
aarti / BreakStringIntoWords
Created September 30, 2015 05:45
Break a string Into valid words
class BreakStr
attr_reader :tree
def initialize
@tree = {}
@dict = ["I", "am", "a", "happy", "hap", "sad", "silly", "with"]
end
def get_valid_prefixes(word)
@aarti
aarti / project_euler_problem1.rb
Last active December 15, 2015 18:59
Find the sum of all the multiples of 3 or 5 below 1000.
total= (1..999).reduce(0) do |sum,n|
n%3 == 0 || n%5 == 0 ? sum + n : sum
end
puts total