Skip to content

Instantly share code, notes, and snippets.

@stevenbarragan
Last active January 3, 2016 08:09
Show Gist options
  • Select an option

  • Save stevenbarragan/8434248 to your computer and use it in GitHub Desktop.

Select an option

Save stevenbarragan/8434248 to your computer and use it in GitHub Desktop.

Revisions

  1. stevenbarragan revised this gist Jan 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion diagonal.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def diagonal(a)
    out
    end

    def down x,y,a
    def down(x,y,a)
    out = []
    while y >= 0 && x < a.size
    out << a[x][y]
  2. stevenbarragan revised this gist Jan 15, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion diagonal.rb
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ def down x,y,a
    expect(down(1,3,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [4,3,2]
    end

    it 'return the correct diagonal 0,0' do
    it 'return the correct diagonal from 0,0' do
    expect(down(0,0,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [1]
    end
    end
  3. stevenbarragan revised this gist Jan 15, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions diagonal.rb
    Original file line number Diff line number Diff line change
    @@ -21,15 +21,15 @@ def down x,y,a
    end

    describe '#down' do
    it 'return the correct diagonal' do
    it 'return the correct diagonal from 0,3' do
    expect(down(0,3,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [4,3,2,1]
    end

    it 'return the correct diagonal' do
    it 'return the correct diagonal from 1,3' do
    expect(down(1,3,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [4,3,2]
    end

    it 'return the correct diagonal' do
    it 'return the correct diagonal 0,0' do
    expect(down(0,0,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [1]
    end
    end
  4. stevenbarragan created this gist Jan 15, 2014.
    41 changes: 41 additions & 0 deletions diagonal.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    def diagonal(a)
    x = y = 0
    out = []
    limit = a.size - 1
    (0..limit * 2).each do
    out << down(x,y,a)
    x += 1 if y == limit
    y += 1 if y < limit
    end
    out
    end

    def down x,y,a
    out = []
    while y >= 0 && x < a.size
    out << a[x][y]
    x += 1
    y -= 1
    end
    out
    end

    describe '#down' do
    it 'return the correct diagonal' do
    expect(down(0,3,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [4,3,2,1]
    end

    it 'return the correct diagonal' do
    expect(down(1,3,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [4,3,2]
    end

    it 'return the correct diagonal' do
    expect(down(0,0,[[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]])).to eq [1]
    end
    end

    describe '#diagonal' do
    it 'return the correct diagonals' do
    expect(diagonal [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]).to eq [[1],[2,1],[3,2,1],[4,3,2,1],[4,3,2],[4,3],[4]]
    end
    end