Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View Nevraeka's full-sized avatar

Erik Isaksen Nevraeka

View GitHub Profile
def is_perfect_square?(x)
return false if x < 1
sqrt = Math::sqrt(x)
x == sqrt.floor**2
end
def quicksort a
(pivot = a.pop) ? quicksort(a.select{|i| i <= pivot}) + [pivot] + quicksort(a.select{|i| i > pivot}) : []
end
def find_in(arr, obj)
return -1 if !arr.include?(obj)
arr.find_index obj
end
@Nevraeka
Nevraeka / Floyds Linked List Algorithm
Created July 1, 2013 05:52
Linked List detection experiment
#taken from Floyds Algorithm (http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare)
def floyd(f, x0)
tortoise = f(x0)
hare = f(f(x0))
while tortoise != hare
tortoise = f(tortoise)
hare = f(f(hare))
end
mu = 0
@Nevraeka
Nevraeka / Character count
Created June 29, 2013 04:14
Some basic example code approaches and problem solving
def char_count(paragraph)
paragraph.split(%r{\s*}).count
end
def each_word_counts(paragraph)
details = {}
paragraph.split(/\s/).each { |word| details[word] = word.length }
details
end