From fe513cac6b4c0370c554731927ce83b7451e0188 Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 17:35:22 +0530 Subject: [PATCH 1/9] changes for extracting first row from a matrix --- lib/matrix.rb | 18 ++++++++++++++++++ spec/matrix_spec.rb | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/matrix.rb b/lib/matrix.rb index e69de29..0e02eb6 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -0,0 +1,18 @@ +class Matrix + @matrix + + def initialize(string) + @matrix = string + end + + def extract_row(row_number) + array = [] + rows_of_matrix = @matrix.split("\n") + selected_row = rows_of_matrix[row_number] + selected_row.split(" ").each do |string| + array << string.to_i + end + array + end + +end \ No newline at end of file diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index e69de29..6e820e7 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -0,0 +1,12 @@ +require 'matrix.rb' +describe Matrix do + + context "when extracting a row of a matrix" do + it "returns [1, 2] for the matrix \n1 2\n10 20" do + matrix = Matrix.new("1 2\n10 20") + expect(matrix.extract_row(0)).to eq([1, 2]) + end + + end +end + From 2e9aea016606148ac4d1e39865709a6c6d14e4bf Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 19:35:43 +0530 Subject: [PATCH 2/9] add test case for checking the extraction of second row --- spec/matrix_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 6e820e7..65e0122 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -2,11 +2,15 @@ describe Matrix do context "when extracting a row of a matrix" do - it "returns [1, 2] for the matrix \n1 2\n10 20" do + it "returns [1, 2] as the first row of the matrix \n1 2\n10 20" do matrix = Matrix.new("1 2\n10 20") - expect(matrix.extract_row(0)).to eq([1, 2]) + expect(matrix.extract_row(0)).to eq([1 , 2]) end + it "returns [8, 6] as the second row of the matrix \n9 7\n8 6" do + matrix = Matrix.new("9 7\n8 6") + expect(matrix.extract_row(1)).to eq([8 , 6]) + end end end From 4d58f9dd358433b00562edf6b7bd3e4517034557 Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 19:48:13 +0530 Subject: [PATCH 3/9] add test for extraction of row from a non-regular matrix --- spec/matrix_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 65e0122..beeb434 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -11,6 +11,12 @@ matrix = Matrix.new("9 7\n8 6") expect(matrix.extract_row(1)).to eq([8 , 6]) end + + it "returns [9, 8, 7] as the first row of the matrix\n9 8 7\n19 18 17" do + matrix = Matrix.new("9 8 7\n19 18 17") + expect(matrix.extract_row(0)).to eq([9, 8, 7]) + end end + end From a003698dada2313f8ae42e450924b624a8156416 Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 20:07:50 +0530 Subject: [PATCH 4/9] changes for extracting first column of a matrix --- lib/matrix.rb | 9 +++++++++ spec/matrix_spec.rb | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/lib/matrix.rb b/lib/matrix.rb index 0e02eb6..dcd11c6 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -15,4 +15,13 @@ def extract_row(row_number) array end + def extract_column(column_number) + array = [] + rows_of_matrix = @matrix.split("\n") + rows_of_matrix.each do |row| + array << row.split(" ")[0].to_i + end + array + end + end \ No newline at end of file diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index beeb434..d7d1885 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -18,5 +18,11 @@ end end + context "when extracting a column of a matrix" do + it "returns [1 , 10] as the first column of the matrix \n1 2\n10 20" do + matrix = Matrix.new("1 2\n10 20") + expect(matrix.extract_column(0)).to eq([1 , 10]) + end + end end From b7af6b65803a8137cbd977d3c59574f518f08263 Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 20:17:02 +0530 Subject: [PATCH 5/9] add test for extracting second column of the matrix --- lib/matrix.rb | 2 +- spec/matrix_spec.rb | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/matrix.rb b/lib/matrix.rb index dcd11c6..e54f48a 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -19,7 +19,7 @@ def extract_column(column_number) array = [] rows_of_matrix = @matrix.split("\n") rows_of_matrix.each do |row| - array << row.split(" ")[0].to_i + array << row.split(" ")[column_number].to_i end array end diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index d7d1885..653787e 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -23,6 +23,11 @@ matrix = Matrix.new("1 2\n10 20") expect(matrix.extract_column(0)).to eq([1 , 10]) end + + it "returns [5 , 20] as the second column of the matrix \n4 5\n19 20" do + matrix = Matrix.new("4 5\n19 20") + expect(matrix.extract_column(1)).to eq([5 , 20]) + end end end From b9bb22dfa38d2fdb4564095e7682bd12c7c6911d Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 20:25:11 +0530 Subject: [PATCH 6/9] add test case for extraction of second column of non-regular matrix --- spec/matrix_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 653787e..1fb3a32 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -28,6 +28,11 @@ matrix = Matrix.new("4 5\n19 20") expect(matrix.extract_column(1)).to eq([5 , 20]) end + + it "returns [] as the second column of the non-regular matrix\n10 19 15\n11 13 16" do + matrix = Matrix.new("10 19 15\n11 13 16") + expect(matrix.extract_column(1)).to eq([19, 13]) + end end end From 161af954def8aede4c2fafd7229ba844e2f58af3 Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Sun, 13 Jan 2019 20:30:35 +0530 Subject: [PATCH 7/9] correct typo in previous commit --- spec/matrix_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 1fb3a32..86171ca 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -29,7 +29,7 @@ expect(matrix.extract_column(1)).to eq([5 , 20]) end - it "returns [] as the second column of the non-regular matrix\n10 19 15\n11 13 16" do + it "returns [19, 13] as the second column of the non-regular matrix\n10 19 15\n11 13 16" do matrix = Matrix.new("10 19 15\n11 13 16") expect(matrix.extract_column(1)).to eq([19, 13]) end From 6f23c1e3e80f1555d7b80b8ac71e01ef602f7e5c Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Mon, 14 Jan 2019 11:02:00 +0530 Subject: [PATCH 8/9] correct format for matrix_spec.rb --- spec/matrix_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index 86171ca..f648d16 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -4,12 +4,12 @@ context "when extracting a row of a matrix" do it "returns [1, 2] as the first row of the matrix \n1 2\n10 20" do matrix = Matrix.new("1 2\n10 20") - expect(matrix.extract_row(0)).to eq([1 , 2]) + expect(matrix.extract_row(0)).to eq([1, 2]) end it "returns [8, 6] as the second row of the matrix \n9 7\n8 6" do matrix = Matrix.new("9 7\n8 6") - expect(matrix.extract_row(1)).to eq([8 , 6]) + expect(matrix.extract_row(1)).to eq([8, 6]) end it "returns [9, 8, 7] as the first row of the matrix\n9 8 7\n19 18 17" do @@ -21,12 +21,12 @@ context "when extracting a column of a matrix" do it "returns [1 , 10] as the first column of the matrix \n1 2\n10 20" do matrix = Matrix.new("1 2\n10 20") - expect(matrix.extract_column(0)).to eq([1 , 10]) + expect(matrix.extract_column(0)).to eq([1, 10]) end it "returns [5 , 20] as the second column of the matrix \n4 5\n19 20" do matrix = Matrix.new("4 5\n19 20") - expect(matrix.extract_column(1)).to eq([5 , 20]) + expect(matrix.extract_column(1)).to eq([5, 20]) end it "returns [19, 13] as the second column of the non-regular matrix\n10 19 15\n11 13 16" do From e18d51376c3871021d979b9ced16d37171eb748b Mon Sep 17 00:00:00 2001 From: Abhishek Katiyar Date: Mon, 14 Jan 2019 11:04:32 +0530 Subject: [PATCH 9/9] remove inclusion of test data in description --- spec/matrix_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/matrix_spec.rb b/spec/matrix_spec.rb index f648d16..fd91f91 100644 --- a/spec/matrix_spec.rb +++ b/spec/matrix_spec.rb @@ -2,34 +2,34 @@ describe Matrix do context "when extracting a row of a matrix" do - it "returns [1, 2] as the first row of the matrix \n1 2\n10 20" do + it "returns [1, 2] as the first row of the matrix" do matrix = Matrix.new("1 2\n10 20") expect(matrix.extract_row(0)).to eq([1, 2]) end - it "returns [8, 6] as the second row of the matrix \n9 7\n8 6" do + it "returns [8, 6] as the second row of the matrix" do matrix = Matrix.new("9 7\n8 6") expect(matrix.extract_row(1)).to eq([8, 6]) end - it "returns [9, 8, 7] as the first row of the matrix\n9 8 7\n19 18 17" do + it "returns [9, 8, 7] as the first row of the matrix" do matrix = Matrix.new("9 8 7\n19 18 17") expect(matrix.extract_row(0)).to eq([9, 8, 7]) end end context "when extracting a column of a matrix" do - it "returns [1 , 10] as the first column of the matrix \n1 2\n10 20" do + it "returns [1 , 10] as the first column of the matrix" do matrix = Matrix.new("1 2\n10 20") expect(matrix.extract_column(0)).to eq([1, 10]) end - it "returns [5 , 20] as the second column of the matrix \n4 5\n19 20" do + it "returns [5 , 20] as the second column of the matrix" do matrix = Matrix.new("4 5\n19 20") expect(matrix.extract_column(1)).to eq([5, 20]) end - it "returns [19, 13] as the second column of the non-regular matrix\n10 19 15\n11 13 16" do + it "returns [19, 13] as the second column of the non-regular matrix" do matrix = Matrix.new("10 19 15\n11 13 16") expect(matrix.extract_column(1)).to eq([19, 13]) end