Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Created December 28, 2013 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chischaschos/8155907 to your computer and use it in GitHub Desktop.
Save chischaschos/8155907 to your computer and use it in GitHub Desktop.
require 'minitest/autorun'
SortSnail = -> (elements, result = []) {
return result if elements.empty?
bottom_limit = 0
top_limit = elements.count - 1
loop do
result.concat elements[bottom_limit][bottom_limit..top_limit]
(bottom_limit + 1..top_limit).each do |row|
result << elements[row][top_limit]
end
result.concat elements[top_limit][bottom_limit...top_limit].reverse
(top_limit - 1).downto(bottom_limit + 1).each do |row|
result << elements[row][bottom_limit]
end
bottom_limit += 1
top_limit -= 1
break if bottom_limit > top_limit
end
result
}
describe SortSnail do
it 'should handle empty arrays' do
elements = []
SortSnail.(elements).must_equal []
end
it 'should handle pair arrays' do
elements = [
[ 1, 2 ],
[ 4, 9 ],
]
SortSnail.(elements).must_equal [
1, 2, 9, 4
]
end
it 'should handle odd arrays' do
elements = [
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
]
SortSnail.(elements).must_equal [
1, 2, 3, 6, 9, 8, 7, 4, 5
]
end
it 'should handle big arrays' do
elements = [
[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[ 13, 14, 15, 16, 17, 19],
[ 20, 21, 22, 23, 24, 25],
[ 26, 27, 28, 29, 30, 31],
[ 32, 33, 34, 35, 36, 37],
]
SortSnail.(elements).must_equal [
1, 2, 3, 4, 5, 6, 12, 19, 25, 31, 37, 36, 35, 34, 33, 32,
26, 20, 13, 7, 8, 9, 10, 11, 17, 24, 30, 29, 28, 27, 21, 14,
15, 16, 23, 22
]
end
end
require 'ruby-prof'
elements = Array.new(500) {Array.new(500) { rand 20 }}
RubyProf.start
SortSnail.(elements)
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new result
printer.print STDOUT
➜ katas ruby my-snail-sort.rb
Thread ID: 70156823111380
Fiber ID: 70156822837020
Total: 0.096955
Sort by: self_time
%self total self wait child calls name
50.11 0.049 0.049 0.000 0.000 500 Integer#downto
44.50 0.043 0.043 0.000 0.000 250 Range#each
2.84 0.097 0.003 0.000 0.094 1 Kernel#loop
1.14 0.001 0.001 0.000 0.000 500 Array#concat
0.51 0.000 0.000 0.000 0.000 250 Array#reverse
0.50 0.000 0.000 0.000 0.000 500 Array#[]
0.35 0.044 0.000 0.000 0.043 250 Enumerator#each
0.04 0.097 0.000 0.000 0.097 1 Global#[No method]
0.01 0.097 0.000 0.000 0.097 1 Proc#call
0.00 0.000 0.000 0.000 0.000 1 Array#count
* indicates recursively called methods
Run options: --seed 59476
# Running tests:
....
Finished tests in 0.000605s, 6611.5702 tests/s, 6611.5702 assertions/s.
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips
require 'minitest/autorun'
SortSnail = -> (array) {
array.empty? ? [] : array.shift + SortSnail.(array.transpose.reverse)
}
describe SortSnail do
it 'should handle empty arrays' do
elements = []
SortSnail.(elements).must_equal []
end
it 'should handle pair arrays' do
elements = [
[ 1, 2 ],
[ 4, 9 ],
]
SortSnail.(elements).must_equal [
1, 2, 9, 4
]
end
it 'should handle odd arrays' do
elements = [
[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
]
SortSnail.(elements).must_equal [
1, 2, 3, 6, 9, 8, 7, 4, 5
]
end
it 'should handle big arrays' do
elements = [
[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[ 13, 14, 15, 16, 17, 19],
[ 20, 21, 22, 23, 24, 25],
[ 26, 27, 28, 29, 30, 31],
[ 32, 33, 34, 35, 36, 37],
]
SortSnail.(elements).must_equal [
1, 2, 3, 4, 5, 6, 12, 19, 25, 31, 37, 36, 35, 34, 33, 32,
26, 20, 13, 7, 8, 9, 10, 11, 17, 24, 30, 29, 28, 27, 21, 14,
15, 16, 23, 22
]
end
end
require 'ruby-prof'
elements = Array.new(500) {Array.new(500) { rand 20 }}
RubyProf.start
SortSnail.(elements)
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new result
printer.print STDOUT
➜ katas ruby smaller-snail-sort.rb
Thread ID: 70117983856340
Fiber ID: 70117983582840
Total: 24.090672
Sort by: self_time
%self total self wait child calls name
49.27 11.869 11.869 0.000 0.000 999 Array#transpose
0.03 0.007 0.007 0.000 0.000 999 Array#reverse
0.01 0.002 0.002 0.000 0.000 999 Array#shift
0.00 24.091 0.001 0.000 24.090 1000 *Proc#call
0.00 24.091 0.000 0.000 24.091 1 Global#[No method]
* indicates recursively called methods
Run options: --seed 38546
# Running tests:
....
Finished tests in 0.000584s, 6849.3151 tests/s, 6849.3151 assertions/s.
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment