Skip to content

Instantly share code, notes, and snippets.

@jkaihsu
jkaihsu / arithmetic.rb
Created March 7, 2013 08:13
Define four methods which correspond to the four basic arithmetic operations: add, subtract, multiply, divide.They should accept either integers or floating point numbers as input. divide should perform floating point division.
def add(x,y)
x + y
end
def subtract(x,y)
x - y
end
def multiply(x,y)
x * y
@jkaihsu
jkaihsu / mode.rb
Created March 7, 2013 08:16
Write a method mode which takes an Array of numbers as its input and returns an Array of the most frequent values. If there's only one most-frequent value, it returns a single-element Array. For example, mode([1,2,3,3]) # => [3] mode([4.5, 0, 0]) # => [0] mode([1.5, -1, 1, 1.5]) # => [1.5] mode([1,1,2,2]) # => [1,2] mode([1,2,3]) # => [1,2,3], b…
def mode(array)
mode = array.inject(Hash.new(0)) { |h,v| h[v] = h[v] + 1; h}
mode.select{ |h,v| v == mode.values.max }.keys
end
@jkaihsu
jkaihsu / longest_string.rb
Created March 7, 2013 08:17
Write a method longest_string which takes as its input an Array of Strings and returns the longest String in the Array. For example: # 'zzzzzzz' is 7 characters long longest_string(['cat', 'zzzzzzz', 'apples']) # => "zzzzzzz" If the input Array is empty longest_string should return nil.
def longest_string(array)
if array.empty?
nil
else
long_string = array.group_by(&:size).max.last
long_string[0]
end
end
@jkaihsu
jkaihsu / reverse_words.rb
Created March 7, 2013 08:20
Write a method reverse_words which takes a sentence as a string and reverse each word in it.
def reverse_words(str)
words = str.split(' ')
reverse_str = []
words.length.times do |i|
reverse_str[i] = words[i].reverse
end
return reverse_str.join(" ")
@jkaihsu
jkaihsu / commas1.rb
Created March 7, 2013 08:21
Write a method separate_comma which takes an integer as its input and returns a comma-separated integer as a string.
def separate_comma(number)
number.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
@jkaihsu
jkaihsu / pad.rb
Created March 7, 2013 08:23
Implement Array#pad and Array#pad!. Each method accepts a minimum size (non-negative integer) and an optional pad value as arguments. If the array's length is less than the minimum size, Array#pad should return a new array padded with the pad value up to the minimum size. For example, ruby [1,2,3].pad(5) should return [1,2,3,nil,nil] And ruby [1…
class Array
def pad!(min_size, value = nil)
x = min_size - self.count
x.times {self << value}
self
end
def pad(min_size, value = nil)
self.clone.pad!(min_size, value)
end
@jkaihsu
jkaihsu / count_bwtn.rb
Created March 7, 2013 08:15
Write a method count_between which takes three arguments as input: An Array of integers An integer lower bound An integer upper bound count_between should return the number of integers in the Array between the two bounds, including the bounds It should return 0 if the Array is empty.
def count_between(array, lower_bound, upper_bound)
array.count{|x| x >= lower_bound && x <= upper_bound}
end
@jkaihsu
jkaihsu / triangle.rb
Created March 7, 2013 08:19
Write a method print_triangle which takes at its input an integer representing the number of rows to print, and prints out a right triangle consisting of * characters, one line per row. For example, print_triangle(5) should print out:
def print_triangle(rows)
for j in 1..rows do
for i in 1..j do
print "*" * 1
end
puts
end
end
@jkaihsu
jkaihsu / sudoku_refactoring.rb
Created March 19, 2013 15:06
Not done yet.
class Sudoku
def initialize(string)
string_of_array = string.split("")
@board = Array.new(9) {string_of_array.shift(9)}
end
def solve!
first_empty = find_empty_cell