Created
December 15, 2012 16:50
-
-
Save itayadler/4297026 to your computer and use it in GitHub Desktop.
Spiral Printer
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 NotSupportedArraySize < StandardError; end | |
| class SpiralPrinter | |
| def print(array) | |
| finalResult = [] | |
| size = array.length | |
| modulo = Math.sqrt(size).to_i | |
| raise NotSupportedArraySize if modulo**2 != size | |
| peelingIndex = 0 | |
| startIndex = 0 | |
| endIndex = size | |
| method = :upto | |
| leftOvers = [] | |
| onTrack = array.dup | |
| return "#{array[0]}" if size == 1 | |
| while onTrack.length != 4 do | |
| sqrt = modulo if sqrt.nil? | |
| if peelingIndex > 0 | |
| startIndex = onTrack.length-1 | |
| endIndex = 0 | |
| method = :downto | |
| sqrt = Math.sqrt(onTrack.length).to_i | |
| end | |
| realIndex = 0 | |
| for i in startIndex.send(method, endIndex) do | |
| if realIndex < sqrt | |
| finalResult << onTrack[i] | |
| elsif (realIndex+1) % sqrt == 0 | |
| finalResult << onTrack[i] | |
| else | |
| leftOvers << onTrack[i] | |
| end | |
| realIndex += 1 | |
| end | |
| onTrack = leftOvers.compact | |
| leftOvers = [] | |
| peelingIndex += 1 | |
| end | |
| onTrack = onTrack.reverse if peelingIndex > 0 | |
| onTrack.each_with_index do |item, index| | |
| if index == 2 | |
| finalResult << onTrack[index+1] | |
| elsif index == 3 | |
| finalResult << onTrack[index-1] | |
| else | |
| finalResult << item | |
| end | |
| end | |
| finalResult.join(",") | |
| end | |
| end | |
| describe SpiralPrinter do | |
| it "should be able to print a spiral of an array of size 1" do | |
| subject.print([1]).should == "1" | |
| end | |
| it "should be able to print a spiral of an array of size 4" do | |
| subject.print([1,2,4,3]).should == "1,2,3,4" | |
| end | |
| ### |1,2,3| |1,2,3| | |
| ### |8,9,4| => |4,5,6| | |
| ### |7,6,5| |7,8,9| | |
| it "should be able to print a spiral of an array of size 9" do | |
| subject.print([1,2,3,8,9,4,7,6,5]).should == "1,2,3,4,5,6,7,8,9" | |
| end | |
| ### |01,02,03,04| |01,02,03,04| | |
| ### |12,13,14,05| => |05,06,07,08| | |
| ### |11,16,15,06| |09,10,11,12| | |
| ### |10,09,08,07| |13,14,15,16| | |
| it "should be able to print a spiral of an array of size 16" do | |
| subject.print([1,2,3,4,12,13,14,5,11,16,15,6,10,9,8,7]).should == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" | |
| end | |
| ### |01,02,03,04,05| |01,02,03,04,05| | |
| ### |16,17,18,19,06| => |06,07,08,09,10| | |
| ### |15,24,25,20,07| |11,12,13,14,15| | |
| ### |14,23,22,21,08| |16,17,18,19,20| | |
| ### |13,12,11,10,09| |21,22,23,24,25| | |
| it "should be able to print a spiral of an array of size 25" do | |
| subject.print([1,2,3,4,5,16,17,18,19,6,15,24,25,20,7,14,23,22,21,8,13,12,11,10,9]).should == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25" | |
| end | |
| ### |01,02,03,04,05,06| |01,02,03,04,05,06| | |
| ### |20,21,22,23,24,07| => |07,08,09,10,11,12| | |
| ### |19,32,33,34,25,08| |13,14,15,16,17,18| | |
| ### |18,31,36,35,26,09| |19,20,21,22,23,24| | |
| ### |17,30,29,28,27,10| |25,26,27,28,29,30| | |
| ### |16,15,14,13,12,11| |31,32,33,34,35,36| | |
| it "should be able to print a spiral of an array of size 36" do | |
| subject.print([1,2,3,4,5,6,20,21,22,23,24,7,19,32,33,34,25,8,18,31,36,35,26,9,17,30,29,28,27,10,16,15,14,13,12,11]).should == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36" | |
| end | |
| it "should fail if size of array isn't N^2" do | |
| expect { subject.print([1,2,3]) }.to raise_error { NotSupportedArraySize } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment