Commit a122fdf
Changed files (4)
lib
data_structures
lib/data_structures/dynamic_array_stack.rb
@@ -0,0 +1,13 @@
+class DynamicArrayStack
+ def initialize(items = [])
+ @items = items
+ end
+
+ def pop
+ @items.pop
+ end
+
+ def push(item)
+ @items.push(item)
+ end
+end
lib/insertion_sort.rb
@@ -1,14 +0,0 @@
-class InsertionSort
- def sort(items)
- items.size.times do |n|
- j = n
- while j >= 0 do
- if (items[j] <=> items[j+1]) == 1
- items[j], items[j+1] = items[j+1], items[j]
- end
- j-=1
- end
- end
- items
- end
-end
spec/dynamic_array_stack_spec.rb
@@ -0,0 +1,30 @@
+require "spec_helper"
+
+describe DynamicArrayStack do
+ let(:sut) { DynamicArrayStack.new }
+
+ context "when there is nothing on the stack" do
+ it "should be able to pop off nil" do
+ sut.pop.should be_nil
+ end
+ end
+
+ context "when there is one item on the stack" do
+ it "should be able to pop it off" do
+ n = rand
+ sut.push(n)
+ sut.pop.should == n
+ end
+ end
+
+ context "when there are multiple items on the stack" do
+ it "should pop each one off in the right order" do
+ (0..10).each do |n|
+ sut.push(n)
+ end
+ (10..0).each do |n|
+ sut.pop.should == n
+ end
+ end
+ end
+end
spec/spec_helper.rb
@@ -5,3 +5,4 @@ require_relative '../lib/sorting/quick_sort'
require_relative '../lib/sorting/merge_sort'
require_relative '../lib/data_structures/linked_list_stack'
+require_relative '../lib/data_structures/dynamic_array_stack'