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
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)
}
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
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]
@aarti
aarti / StringExtensions
Created May 2, 2015 21:21
Swift extensions for String
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)}
}
@aarti
aarti / splitstring
Created March 29, 2015 22:50
Swift global functions
var fullName = "Aarti Parikh"
var fullNameArr = split(fullName) {$0 == " "}
println(fullNameArr)
@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 / 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 / config.lua
Last active August 29, 2015 14:07
Corona Simple config.lua supporting multiple devices
--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 =
@aarti
aarti / resize_hash
Created September 26, 2014 18:39
Ruby lang hash resize algorithim
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 */
@aarti
aarti / linked_list.rb
Created September 8, 2014 01:54
Simple Linked List
class Element
attr_accessor :datum, :next
def initialize(datum,next_element)
@datum = datum
@next = next_element
end
def reverse