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
# 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) |
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
#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 |
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 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 } |
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 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 |
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 print_triangle(n) | |
return if n.zero? | |
result = [] | |
result << "*" * n | |
print_triangle(n-1) | |
puts result | |
end | |
print_triangel(4) | |
#=> * |
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 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 = [] |
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 SittingDucks | |
def initialize filename | |
@filename = filename | |
@mortal_enemies = [] | |
@table_1 = [] | |
@table_2 = [] | |
@guest_list = [] | |
@guest = 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 Rectangle | |
attr_accessor :width, :height | |
def initialize(width, height) | |
@width = width | |
@height = height | |
end | |
def ==(other) | |
(other.width == self.width && other.height == self.height ) || |
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
#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', |
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
# Create your 16 dice, each with 6 possible sides | |
# each side has a value of a capital letter | |
# values of Q should be represented as Qu in the final board | |
# organize the possible results for each die in as a set | |
# randomly assign each die to 4 rows, each 4 dice long | |
# find the columns by flipping the rows over the diagonal | |
# create the method "include?" for your your boggle game | |
# if the board doesn't include that letter, return false immediately | |
# if it does, find each square that contains the first letters |
OlderNewer