This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
extension String { | |
func scan(regex : String) -> [String] { | |
let regex = NSRegularExpression(pattern: regex, | |
options: nil, error: nil)! | |
let nsString = self as NSString | |
let results = regex.matchesInString(nsString as String, | |
options: nil, range: NSMakeRange(0, nsString.length)) | |
as! [NSTextCheckingResult] | |
return map(results) { nsString.substringWithRange($0.range)} | |
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
--http://forums.coronalabs.com/topic/37362-improvement-to-the-ultimate-configlua-file/ | |
--The effect of that bit of math is to set the content width or height appropriately for letterbox scaling according to the aspect ratio of the device. Why is that good? Because the upper-left corner is now (0,0), and display.contentWidth and display.contentHeight now cover the entire screen. For example, on an iPhone 5, display.pixelHeight is 1136 and display.pixelHeight is 640. If you do the math, you'll see that it'll set width to 320 and height to 568. | |
-- It'll set the width and height appropriately for every single device, no matter what the resolution or aspect ratio. And there's no need to code into your config.lua different blocks for different devices. | |
--the upper-left corner isn't at (0,0). Instead, it's at (display.screenOriginX, display.screenOriginY) | |
local aspectRatio = display.pixelHeight/display.pixelWidth | |
application = | |
{ | |
content = |
This file contains hidden or 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
new_size(st_index_t size) | |
{ | |
st_index_t i; | |
for (i=3; i<31; i++) { | |
if ((st_index_t)(1<<i) > size) return 1<<i; | |
} | |
#ifndef NOT_RUBY | |
rb_raise(rb_eRuntimeError, "st_table too big"); | |
#endif | |
return -1; /* should raise exception */ |
This file contains hidden or 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 Element | |
attr_accessor :datum, :next | |
def initialize(datum,next_element) | |
@datum = datum | |
@next = next_element | |
end | |
def reverse |