Skip to content

Commit 251ba69

Browse files
author
Mauricio Klein
committed
Add solution for challenge #1
1 parent 642d207 commit 251ba69

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Challenges list
2+
3+
- [x] [Challenge 1: Spirally traversing a matrix](challenge-1/README.md)

challenge-1/Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Gemfile
2+
source "https://rubygems.org"
3+
4+
gem "rspec"

challenge-1/Gemfile.lock

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
diff-lcs (1.3)
5+
rspec (3.8.0)
6+
rspec-core (~> 3.8.0)
7+
rspec-expectations (~> 3.8.0)
8+
rspec-mocks (~> 3.8.0)
9+
rspec-core (3.8.0)
10+
rspec-support (~> 3.8.0)
11+
rspec-expectations (3.8.2)
12+
diff-lcs (>= 1.2.0, < 2.0)
13+
rspec-support (~> 3.8.0)
14+
rspec-mocks (3.8.0)
15+
diff-lcs (>= 1.2.0, < 2.0)
16+
rspec-support (~> 3.8.0)
17+
rspec-support (3.8.0)
18+
19+
PLATFORMS
20+
ruby
21+
22+
DEPENDENCIES
23+
rspec
24+
25+
BUNDLED WITH
26+
1.16.6

challenge-1/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Spirally traversing a matrix
2+
3+
The original challenge description can be found [here](https://practice.geeksforgeeks.org/problems/spirally-traversing-a-matrix/0)
4+
5+
## Description
6+
7+
Given a 2D matrix of size M*N. Traverse and print the matrix in spiral form.
8+
9+
## Input
10+
- Array of values
11+
- Dimension M (number of rows)
12+
- Dimension N (number of columns)
13+
14+
## Output
15+
Elements when travelled in Spiral form, will be displayed in a single line.
16+
17+
### Constraints:
18+
1 <=T<= 100
19+
20+
2 <= M, N <= 10
21+
22+
0 <= Ai <= 100
23+
24+
### Example:
25+
26+
```
27+
Input:
28+
Array: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
29+
M: 4
30+
N: 4
31+
32+
Output:
33+
[1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10]
34+
```

challenge-1/lib/solver.rb

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solver
2+
def call(arr, n_rows, n_columns)
3+
length = n_rows * n_columns
4+
5+
row, col = 0, 0
6+
inc_row, inc_col = 0, 1
7+
8+
result = Array.new(length)
9+
10+
length.times do |pos|
11+
offset = calculate_offset(row, col, n_columns)
12+
13+
# Assign the element to result and
14+
# mark the cell as visited (= nil)
15+
result[pos] = arr[offset]
16+
arr[offset] = nil
17+
18+
# If found a wall (end of array or visited cell),
19+
# turn the walk diretion to the right
20+
if wall?(arr, row + inc_row, col + inc_col, n_columns, n_rows)
21+
inc_row, inc_col = turn_right(inc_row, inc_col)
22+
end
23+
24+
# Update indexes
25+
row += inc_row
26+
col += inc_col
27+
end
28+
29+
result
30+
end
31+
32+
private
33+
34+
def calculate_offset(row, col, n_columns)
35+
n_columns * row + col
36+
end
37+
38+
def wall?(arr, row, col, n_columns, n_rows)
39+
outbound?(row, n_rows) || outbound?(col, n_columns) || arr[calculate_offset(row, col, n_columns)].nil?
40+
end
41+
42+
def outbound?(i, length)
43+
i < 0 || i >= length
44+
end
45+
46+
def turn_right(inc_row, inc_col)
47+
return [ 1, 0] if inc_col == 1
48+
return [ 0, -1] if inc_row == 1
49+
return [-1, 0] if inc_col == -1
50+
[ 0, 1]
51+
end
52+
end

challenge-1/spec/solver_spec.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Solver do
4+
subject { described_class.new.call(arr, n_rows, n_columns) }
5+
6+
context "with 4x4 table" do
7+
let(:arr) { (1..16).to_a }
8+
let(:n_rows) { 4 }
9+
let(:n_columns) { 4 }
10+
11+
it { is_expected.to eq([1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10]) }
12+
end
13+
14+
context "with 3x4 table" do
15+
let(:arr) { (1..12).to_a }
16+
let(:n_rows) { 3 }
17+
let(:n_columns) { 4 }
18+
19+
it { is_expected.to eq([1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]) }
20+
end
21+
22+
context "with 4x3 table" do
23+
let(:arr) { (1..12).to_a }
24+
let(:n_rows) { 4 }
25+
let(:n_columns) { 3 }
26+
27+
it { is_expected.to eq([1, 2, 3, 6, 9, 12, 11, 10, 7, 4, 5, 8]) }
28+
end
29+
30+
context "with 2x2 table" do
31+
let(:arr) { (1..4).to_a }
32+
let(:n_rows) { 2 }
33+
let(:n_columns) { 2 }
34+
35+
it { is_expected.to eq([1, 2, 4, 3]) }
36+
end
37+
38+
context "with 1x8 table" do
39+
let(:arr) { (1..8).to_a }
40+
let(:n_rows) { 1 }
41+
let(:n_columns) { 8 }
42+
43+
it { is_expected.to eq([1, 2, 3, 4, 5, 6, 7, 8]) }
44+
end
45+
46+
context "with 8x1 table" do
47+
let(:arr) { (1..8).to_a }
48+
let(:n_rows) { 8 }
49+
let(:n_columns) { 1 }
50+
51+
it { is_expected.to eq([1, 2, 3, 4, 5, 6, 7, 8]) }
52+
end
53+
54+
context "with empty table" do
55+
let(:arr) { [] }
56+
let(:n_rows) { 1 }
57+
let(:n_columns) { 0 }
58+
59+
it { is_expected.to eq([]) }
60+
end
61+
end

challenge-1/spec/spec_helper.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Dir["#{__dir__}/../lib/*.rb"].each { |file| require file }

0 commit comments

Comments
 (0)