This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fullName = "Aarti Parikh" | |
var fullNameArr = split(fullName) {$0 == " "} | |
println(fullNameArr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BreakStr | |
attr_reader :tree | |
def initialize | |
@tree = {} | |
@dict = ["I", "am", "a", "happy", "hap", "sad", "silly", "with"] | |
end | |
def get_valid_prefixes(word) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
total= (1..999).reduce(0) do |sum,n| | |
n%3 == 0 || n%5 == 0 ? sum + n : sum | |
end | |
puts total |