This file contains 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 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 |
This file contains 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
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 |
This file contains 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 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 |
This file contains 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 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 |
This file contains 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
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 |
This file contains 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 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 |
This file contains 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 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); |
This file contains 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
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' |
This file contains 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 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)); | |
} |
OlderNewer