Skip to content

Instantly share code, notes, and snippets.

View nimamehanian's full-sized avatar
🎧
Focused

Nima Mehanian nimamehanian

🎧
Focused
View GitHub Profile
@nimamehanian
nimamehanian / rpn_calc.rb
Created February 21, 2013 02:28
RPN Calculator
class Rpn
def evaluate(expression)
operators = ['+', '-', '*']
stack = []
symbols = expression.split(' ')
symbols.each do |symbol|
if operators.include?(symbol)
number_two = stack.pop
number_one = stack.pop
@nimamehanian
nimamehanian / anagrams.rb
Created February 21, 2013 02:29
Determine whether two strings are anagrams.
def anagrams?(word_one, word_two)
word_one = word_one.split('').sort
word_two = word_two.split('').sort
word_one == word_two ? true : false
end
@nimamehanian
nimamehanian / reverse_array.rb
Created February 21, 2013 02:31
Reverse the order of an array's elements, without using Ruby's built-in methods.
class Array
def backwards
length = self.length
i = 1
while i != length
i += 1
value = self.slice!(length - i)
self << value
break if self[length - i] == self[0]
end
@nimamehanian
nimamehanian / shuffle_array.rb
Created February 21, 2013 02:32
Shuffle an array's elements, without using Ruby's built-in methods.
class Array
def mixup
length = self.length
while length > 0
length -= 1
break if length == 0
randomIndex = (rand * (length - 1)).floor
self[length], self[randomIndex] = self[randomIndex], self[length]
end
end
@nimamehanian
nimamehanian / countem.rb
Created February 21, 2013 02:38
Display how many times an integer occurs at each index, within a nested array.
def countem(list_of_lists)
occurrences = {}
list_of_lists.each do |inner_list|
inner_list.each_with_index do |integer, index_of_integer|
if occurrences.has_key?("#{integer} @ #{index_of_integer}") == false
# if not there, add it
occurrences["#{integer} @ #{index_of_integer}"] = 1
else
# if there, increment
occurrences["#{integer} @ #{index_of_integer}"] += 1
@nimamehanian
nimamehanian / prime.rb
Created March 5, 2013 21:54
A predicate method that determines whether a number is prime. Useful for some of Project Euler's challenges.
class Integer
def prime?
if self.even? && self != 2 ||
self.send(:sum_of_digits_div_by_three?) && self != 3 ||
self.send(:last_two_digits_div_by_four?) ||
self.send(:last_digit_div_by_five?) && self != 5
return false
else
return true
end
@nimamehanian
nimamehanian / stringifyJSON.js
Created March 24, 2013 15:11
Recursive implementation of JSON.stringify
var stringifyJSON = function(object){
// Base case: If not object and is a string,
// wrap in quotes and return.
// If not passed in (i.e., null),
// return empty string.
if(typeof object !== 'object' || object === null){
if(typeof object === 'string'){
object = '"' + object + '"';
}
return String(object);
@nimamehanian
nimamehanian / primeNumbers.js
Last active December 15, 2015 09:29
Find a collection of prime numbers between a range. I began with the primeNumbers function and wrote it the way I'd want to use it; go through a range and call prime() on each number. So that meant I had to create a prime() method for all Numbers using their prototype property. This lead me to research what rules define a number to be prime. The…
var primeNumbers = function(from, to) {
var primes = [];
for(var i = from; i <= to; i++){
if(i.prime()){
primes.push(i);
}
}
return primes;
};
@nimamehanian
nimamehanian / largestPalPerfSq.rb
Created May 22, 2013 03:57
Find the largest palindrome (of number_of_digits length) that is also a perfect square. E.g., largest_pal_perf_sq(15) returns 900_075_181_570_009
def palindrome?(text)
return true if text.length <= 1
return false if text[0] != text[text.length - 1]
return palindrome?(text[1..-2])
end
def prepare_range(number_of_digits)
big = ''
sml = '1'
@nimamehanian
nimamehanian / obligation.js
Created August 16, 2015 02:15
Obligation
var isResolvable = function (val) {
return val && typeof val.success === 'function';
};
var reference = function (data) {
if (isResolvable(data)) { return data; }
return {
success: function (callback) {
return reference(callback(data));
}