Skip to content

Instantly share code, notes, and snippets.

@TalkativeTree
TalkativeTree / test.scss
Created February 16, 2014 05:01
Useful test function to test complicated Sass functions.
@function test($statement, $assertion, $expectation) {
@if $assertion != $expectation {
$result: "FAIL";
@return "____ #{$result} ____ " + $statement + " ____ Expected #{$assertion} to equal EXPECTATION: #{$expectation}";
}
@if $assertion == $expectation {
$result: "PASS";
@return "____ #{$result} ____ " + $statement;
}
}
@TalkativeTree
TalkativeTree / Count the numbers in an array between a given range.rb
Last active December 15, 2015 10:59
putting two numbers into an array and have to count the numbers in between the upper and lower bound numbers.
# count_between is a method with three arguments:
# 1. An array of integers
# 2. An integer lower bound
# 3. An integer upper bound
#
# It returns the number of integers in the array between the lower and upper bounds,
# including (potentially) those bounds.
#
# If +array+ is empty the method should return 0
def count_between(array, lower_bound, upper_bound)
@TalkativeTree
TalkativeTree / median_array.rb
Created March 26, 2013 22:52
Finding the median of an array
#median([1,2,3]) # => 2
#median([4.5, 0, -1]) # => 0
#median([-100, 100]) # => 0.0
def median(array)
if array.length.odd?
array.fetch(array.length / 2)
elsif array.length.even?
(array.fetch(array.length / 2 -1) + array.fetch(array.length / 2)) / 2.0
end
@TalkativeTree
TalkativeTree / array_ala_mode.rb
Last active December 15, 2015 11:08
Need to figure out the mode of an array, if two elements tie for the mode, must output all of the tying elements.
def mode(array)
freq = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
max = freq.values.max
freq.select { |k, f|; (f==max) }.map {|k, f| k}
end
mode([1,2,2,3,3,4])
#Explanation of method order of processing method(array)
#freq = array.inject(Hash.new(0)) { |h,v| h[v] += 1; h }
@TalkativeTree
TalkativeTree / coin_flip_variations.rb
Last active December 15, 2015 13:19
finding the number of potential outcomes of flipping a coin or some other chance related outcome.
def combination(num_tosses)
if num_tosses == 0
potential_outcomes = 0
elsif potential_outcomes = ["heads", "tails"].length ** num_tosses
end
return "There are #{potential_outcomes} potential outcomes"
end
puts combination(8) #=> "There are 256 potential outcomes"
# another way to do this is with #repeated_permutation
@TalkativeTree
TalkativeTree / print_triangle.rb
Last active December 15, 2015 13:29
print a right triangle out of *'s in ascending order. so print_triangle(4) => * ** *** ****
def print_triangle(n)
return if n.zero?
result = []
result << "*" * n
print_triangle(n-1)
puts result
end
print_triangel(4)
#=> *
@TalkativeTree
TalkativeTree / atm_program.rb
Last active December 15, 2015 23:59
ATM program
class Account
def initialize(first_name, last_name, acc_num, pin, check_bal, sav_bal)
@first_name = first_name
@last_name = last_name
@acc_num = acc_num
@pin = pin
@check_bal = check_bal
@sav_bal = sav_bal
@balance = []
@TalkativeTree
TalkativeTree / SittingDucks.rb
Last active December 16, 2015 10:19
reddit.com/r/dailyprogrammer challenge#121[HARD] The input is a list of enemies for each guest (with empty lines for guests without enemies). Each guest have a number which is equivalent to the line number in the list. It is a newline-separated file (text file or standard in). Each line is a comma-separated (no space) list of positive integers. …
class SittingDucks
def initialize filename
@filename = filename
@mortal_enemies = []
@table_1 = []
@table_2 = []
@guest_list = []
@guest = 0
end
@TalkativeTree
TalkativeTree / rectangle_review.rb
Created April 20, 2013 22:40
Review of someone else's code
class Rectangle
attr_accessor :width, :height
def initialize(width, height)
@width = width
@height = height
end
def ==(other)
(other.width == self.width && other.height == self.height ) ||
@TalkativeTree
TalkativeTree / roman_numeral.rb
Created April 25, 2013 08:14
convert an arabic number to a roman numeral without using modulus.
#i played around a lot with ideas of how to model the arrays/hashes. I decided to make the key of the hash a string,
##because I wanted to use methods that only worked on strings and didn't want to keep using to_s.
#So I created one array that returns an array of the index of each
#digit in the number we want to convert and another method that returns the pattern for each method.
#What I was thinking was using gsub to replace the values of the pattern
@roman_digit_pattern = Hash["1" => 'X',
"2" => 'XX',
"3" => 'XXX',
"4" => 'XY',