Friday, 25 November 2016

Bubble sort revised

Difficulty:
Easy

Instructions:
Implement in place sorting algorithm with a worst case time complexity of O(n2)

Problem and Solution:

class BubbleSort
def self.sort(inA)
n = inA.length
1.upto(n-1) do |k|
swapped = false
0.upto(n-k-1) do |i|
inA[i], inA[i+1] = inA[i+1], inA[i] if inA[i] > inA[i+1]
end
end
inA
end
end
inA = [1, 10,7, 6, 9]
puts BubbleSort.sort(inA)

Bubble Sort

Difficulty:
Easy

Instructions:
Implement in place sorting algorithm with a worst case time complexity of O(n2)

Problem and Solution:

class Bubblesort
def initialize(unsorted_array, order='ASC')
@input_array = unsorted_array
@len = unsorted_array.length
@opr = order == 'ASC' ? '>' : '<'
end
def sort
@len.times do
@input_array.each_with_index do |ele, i|
break if i+2 > @len
@input_array[i], @input_array[i+1] =
@input_array[i+1], ele if ele.to_i.send(@opr, @input_array[i+1].to_i)
end
end
@input_array
end
end
p Bubblesort.new([98,23,54,6,nil,'string',90,0,5,4,1,2], 'ASC').sort
p Bubblesort.new([98,23,54,6,nil,90,0,5,4,1,2], 'DESC').sort
p Bubblesort.new([98,23,54,6,nil,90,0,5,4,1,2]).sort
view raw bubble_sort.rb hosted with ❤ by GitHub

Tuesday, 22 November 2016

Selection Sort

Difficulty:
Easy

Instructions:
Implement in place sorting algorithm with a worst case time complexity of O(n2)

Problem and Solution:

class SelectionSort
def initialize(unsorted_array, order='ASC')
@input_array = unsorted_array
@len = unsorted_array.length
@opr = order == 'ASC' ? '>' : '<'
@current_min_index = 0
end
def sort
@len.times do |i|
index_min = i
(i + 1).upto(@len) do |j|
# Find the minimum value's index
index_min = j if @input_array[j].to_i < @input_array[index_min].to_i
end
@input_array[i], @input_array[index_min] = @input_array[index_min], @input_array[i] if index_min != i
end
@input_array
end
end
p 'Input: [98,23,54,6,nil,90,0,5,4,1,2]'
p SelectionSort.new([98,23,54,6,nil,90,0,5,4,1,2]).sort

Tuesday, 12 July 2016

Select Map

Difficulty:
Easy

Tags:
enumerables, procs

Instructions:
The Pattern object not only functions as a Regexp. For example, check out what happens if you do Pattern === element. This problem demonstrates that using a Pattern with #grep will work much like using select and map in combination.

Problem and Solution:

# Solution to 'Select Map' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/select-map
assert_equal ["1", 2, "3", 4, "5"].grep(/[1-5]/){ |e| e.succ }, ["2", "4", "6"]
assert_equal [0, 1, 3, 5, 6].grep(1..5, &:succ), [2, 4, 6]
assert_equal ["1", "2", "3", "4", "5"].grep(proc{|ele| ele.to_i%2 ==1}){ |e| e.succ }, ["2", "4", "6"]
assert_equal [1, 2, 3, 4, 5].grep(proc{|ele| ele.to_i%2 ==1}, &:succ), [2, 4, 6]
view raw Select Map.rb hosted with ❤ by GitHub

Pack Template UTF-8

Difficulty:
Easy

Tags:
pack

Instructions:
Write pack template of UTF-8.

Problem and Solution:

# Solution to 'Pack Template UTF-8' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/pack-template-utf-8
# -*- coding: utf-8 -*-
assert_equal [0, 1].pack('U*'), "\000\001"
assert_equal [0x7f].pack('U*'), "\177"
assert_equal [0x80].pack('U*'), "\302\200"
assert_equal "\u3042".unpack('U*'), [0x3042]
assert_equal "あいうえお".unpack('U*'), [12354, 12356, 12358, 12360, 12362]
assert_equal [12354, 12356, 12358, 12360, 12362].pack('U*'), "あいうえお"

Simple String Substitution

Difficulty:
Easy

Tags:
strings

Instructions:
One of the challenges in implementing Rubeque is performing string substitution. See how well you can do.

Problem and Solution:

# Solution to 'Simple String Substitution' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/simple-string-substitution
class String
def simple_sub(pattern, replace)
gsub(/#{Regexp.escape(pattern)}/ ) {replace}
end
end
subject = "?? == ??"
assert_equal subject.simple_sub("==", "<="), "?? <= ??"
assert_equal subject.simple_sub("abc", "def"), "?? == ??"
assert_equal subject.simple_sub("??", "fun??"), "fun?? == fun??"
naruse_answer = "Date.parse(date.sub(/(\d+)[-\/](\d\d).(\d{4})/,'\\3-\\1-\\2')).strftime('%b %d, %Y')"
assert_equal subject.simple_sub("??", naruse_answer), "#{naruse_answer} == #{naruse_answer}"

Quelle Heure Est-Il?

Difficulty:
Easy

Tags:
dates, strings

Instructions:
Write a function to parse some dates and return a standard format. Hint: the help page might be of use.

Problem and Solution:

# Solution to 'Quelle Heure Est-Il?' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/quelle-heure-est-il-ques-
def pretty_date(date)
months = ["January","February","March","April","May","June","July","August","Sepetember","October","November","December"]
mon_to_mon3 = {}
mon_nbr_to_mon3 = {}
months.each_with_index {|m,i| mon_to_mon3[m] = m[0...3]; mon_nbr_to_mon3[i+1] = mon_to_mon3[m]}
case date
when /^\d*\d-\d*\d-\d{4}/ # mm-dd-yyyy
a = date.split('-')
"" << mon_nbr_to_mon3[a[0].to_i] << " " << a[1] << ", " << a[2]
when /^[a-zA-Z]+ \d*\d, \d{4}/ # MONTH dd, yyyy
a = date.split
mon_to_mon3[a[0]] << " " << a[1] << " " << a[2]
when /^\d{4}-\d*\d-\d*\d/ # yyyy-mm-dd
a = date.split('-')
mon_nbr_to_mon3[a[1].to_i] << " " << a[2] << ", " << a[0]
when /^\d*\d\/\d*\d\/\d{4}/ # mm/dd/dddd
a = date.split(/\//)
mon_nbr_to_mon3[a[0].to_i] << " " << a[1] << ", " << a[2]
else
puts "no match"
end
end
assert_equal pretty_date("11-30-1835"), "Nov 30, 1835"
assert_equal pretty_date("July 20, 1933"), "Jul 20, 1933"
assert_equal pretty_date("1922-11-11"), "Nov 11, 1922"
assert_equal pretty_date("9/20/1978"), "Sep 20, 1978"