Skip to content

Instantly share code, notes, and snippets.

@lostmarinero
Created January 14, 2014 18:39
Show Gist options
  • Save lostmarinero/8423369 to your computer and use it in GitHub Desktop.
Save lostmarinero/8423369 to your computer and use it in GitHub Desktop.
This is the 10 questions given by Causes - Target Time 20 Minutes
def problem_1(input_string)
arr = input_string.split(" ")
arr.reverse!
arr.join(" ")
end
puts "#############################"
puts "Problem 1 - Reverse the Words"
puts problem_1("I am hungry")
puts problem_1("I am hungry") == "hungry am I"
def problem_2(input_array)
word_count = {}
input_array.each do |word|
word_count.has_key?(word) ? word_count[word] += 1 : word_count[word] = 1
end
word_count.to_a
end
puts "#############################"
puts "Problem 2 - Word Count"
p problem_2(['hello', 'world', 'world'])
puts problem_2(['hello', 'world', 'world']) == [['hello', 1], ['world', 2]]
def problem_3(input_array)
result_string = "<table><tbody>"
input_array.each do |table_row|
result_string += "<tr>"
table_row.each do |table_cell|
result_string += "<td>#{table_cell}</td>"
end
result_string += "</tr>"
end
result_string += "</tbody></table>"
end
puts "#############################"
puts "Problem 3 - Create HTML"
p problem_3([["one", "uno"], ["two", "dos"]])
puts problem_3([["one", "uno"], ["two", "dos"]]) == "<table><tbody><tr><td>one</td><td>uno</td></tr><tr><td>two</td><td>dos</td></tr></tbody></table>"
def problem_4(input_array)
suffix, type_array = ["B", "K", "M", "G"], [[],[],[],[]]
input_array.each do |file_size|
ind_suffix = file_size.slice(-1)
file_type = suffix.index(ind_suffix) || 0
type_array[file_type] << file_size
end
type_array.map! do |each_type|
each_type.sort! { |file_size2| file_size2[/\d+/].to_i }
end
type_array.flatten
end
puts "#############################"
puts "Problem 4 - Order the bytes"
p problem_4(["1M", "20G", "5012"])
puts problem_4(["1M", "20G", "5012"]) == ["5012", "1M", "20G"]
def problem_5(num)
all_nums, prime_nums = (2..num).to_a, [2]
while !all_nums.empty?
prime_nums.each {|prime| all_nums.delete_if {|number| number % prime == 0 } }
prime_nums.push(all_nums.shift)
end
prime_nums
end
puts "#############################"
puts "Problem 5 - Prime Numbers up to N"
p problem_5(17)
puts problem_5(17) == [2,3,5,7,11,13,17]
def problem_6(target, word_list)
search_word = target.dup.chars.sort
word_list.select {|word| word.chars.sort == search_word && word.length == target.length }
end
puts "#############################"
puts "Problem 6 - Anagrams"
p problem_6("cat", ["act", "cat", "hat"])
puts problem_6("cat", ["act", "cat", "hat"]) == ["act", "cat"]
def problem_7(template, values)
values.each {|value| template.gsub!("{#{value[0]}}", value[1]) }
template
end
template = "{name} uses {product}? I use {product} too!"
values = [["name", "John"], ["product", "vim"]]
puts "#############################"
puts "Problem 7 - Template and Interpolation"
p problem_7(template, values)
puts problem_7(template, values) == "John uses vim? I use vim too!"
def problem_8(arr1, arr2)
arr1.flatten == arr2.flatten
end
puts "#############################"
puts "Problem 8 - Check if Array elements are Equal"
array1 = [1, [2, [3]], [4, 5]]
array2 = [1, [2, [3]], [4, 5]]
p problem_8(array1, array2)
def problem_9(parenth_string)
stack = []
string_characters = parenth_string.chars
while !string_characters.empty?
char = string_characters.shift
if char.ord == 40
stack.push(char)
elsif char.ord == 41 && !stack.empty?
stack.pop
elsif char.ord == 41 && stack.empty?
return false
end
end
stack.empty?
end
puts "#############################"
puts "Problem 9 - Even Sided Parentheses"
p problem_9('(()())') == true
p problem_9(')(()') == false
p problem_9('(()))') == false
require 'date'
def problem_10(year, month)
date_string, start_date, end_value = "", Date.new(year, month, 1).wday, Date.new(year, month, -1).day
calendar_arr = (Array.new( ( (start_date) % 7), 0) + (1..end_value).to_a).each_slice(7).to_a
calendar_arr.each do |week|
week.each do |day|
date_string += (day == 0 ? " " : (day < 10 ? " #{day} " : "#{day} ") )
end
date_string += "\n"
end
date_string
end
puts "#############################"
puts "Problem 10 - Print a calendar month"
puts problem_10(2013, 2)
puts problem_10(2012, 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment