This file contains hidden or 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
require "rspec" | |
require "block_problems" | |
describe "#doubler" do | |
let(:array) { [1, 2, 3] } | |
it "doubles the elements of the array" do | |
expect(doubler(array)).to eq([2, 4, 6]) | |
end |
This file contains hidden or 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
# ### Bubble Sort | |
# | |
# http://en.wikipedia.org/wiki/bubble_sort | |
# | |
# Implement Bubble sort in a method, `Array#bubble_sort!`. Your method should | |
# modify the array so that it is in sorted order. | |
# | |
# > Bubble sort, sometimes incorrectly referred to as sinking sort, is a | |
# > simple sorting algorithm that works by repeatedly stepping through | |
# > the list to be sorted, comparing each pair of adjacent items and |
This file contains hidden or 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 ComputerPlayer | |
def initialize(name, mark=0) | |
@name = name | |
@mark = mark | |
end | |
def name | |
@name | |
end | |
This file contains hidden or 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 HumanPlayer | |
def initialize(name, mark=0) | |
@name = name | |
@mark = mark | |
end | |
def name | |
@name | |
end | |
This file contains hidden or 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 Board | |
def initialize(grid=[[nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]) | |
@grid = grid | |
end | |
def grid | |
@grid | |
end | |
def place_mark(position, symbol) |
This file contains hidden or 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 Code | |
attr_reader :pegs | |
def initialize | |
@pegs = random | |
end | |
def random | |
colors = ["R", "G", "B", "Y", "O", "P"] | |
[colors[rand(6)], colors[rand(6)], colors[rand(6)], colors[rand(6)]].join | |
end |
This file contains hidden or 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 Fixnum | |
def ones_place(integer) | |
portion = integer.to_s[-1].to_i | |
teen_test = integer.to_s[-2..-1].to_i | |
teenager = true if (teen_test > 10 && teen_test < 20) | |
return "" if portion == 0 || teenager | |
ones(portion) | |
end | |
def tens_place(integer) |
This file contains hidden or 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 HumanPlayer | |
attr_accessor :name | |
def get_play | |
gets.chomp.split(",").map{|item| item.to_i} | |
end | |
def prompt | |
puts "Where do you want to fire? (Input like this: 2,3)\n>\n" | |
end |
This file contains hidden or 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 merge_sort(array) | |
total_length = array.length | |
size = 2 | |
while size < total_length + 1 | |
sorted_array = [] | |
array.each_slice(size).to_a.each do |group| | |
slice1 = group[0...(group.length / 2)] | |
slice2 = group[(group.length / 2)..-1] | |
combined = [] | |
This file contains hidden or 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 selection_sort(array) | |
array.each_index do |i| | |
break if i == array.length - 1 | |
current = array[i] | |
smaller = array[i] | |
smaller_index = i | |
array.each_with_index do |item, j| | |
next if j <= i |
OlderNewer