Skip to content

Instantly share code, notes, and snippets.

View ephekt's full-sized avatar

Mike R ephekt

  • maker of things
  • California
View GitHub Profile
@ephekt
ephekt / rb_array_iterate.rb
Created August 2, 2012 02:27
array iteration problem
def add1(arr, val, n)
# how many times are we going to increment
increment_count = n == 0 ? arr.length : n.abs
# are we going up or down the array
indices = n < 0 ? (-1..-arr.length) : (0...arr.length)
# iterate and update the array in place
indices.each do |index|
if (arr_value = arr[index]) == val
arr[index] = arr_value + 1
# decrement and check the count all in one go
@ephekt
ephekt / array_iteration.rb
Created August 2, 2012 02:20 — forked from DanKnox/array_iteration.rb
Array iteration interview question
def add1(arr, val, n)
stop = arr.length - 1
count = 0
(0..stop).each do |i|
index = n < 0 ? stop - i : i
if arr[index] == val
arr[index] += 1
@ephekt
ephekt / interval.js
Created April 25, 2012 20:02 — forked from manast/interval.js
Accurate Javascript setInterval replacement
function interval(duration, fn){
this.baseline = undefined
this.run = function(){
if(this.baseline === undefined){
this.baseline = new Date().getTime()
}
fn()
var end = new Date().getTime()
this.baseline += duration