From c477e90c452a3a689a23bd99a11f7cf2338f63f4 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 03:20:44 +0530 Subject: [PATCH 1/6] implemented start_with along with tests --- lib/parfait/word.rb | 19 +++++++++++++++++-- test/parfait/test_word2.rb | 5 +++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index f9ea7e25..e8e4804f 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -13,7 +13,8 @@ module Parfait # Object length is measured in non-type cells though class Word < Data8 - attr_reader :char_length , :next_word + attr_reader :char_length , :str, :prefix ,:next_word + def self.type_length 3 # 0 type , 1 char_length , next_word @@ -27,6 +28,8 @@ module Parfait def initialize( len ) super() @char_length = 0 + #@str=str1 + #@prefix=prefix1 raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer raise "Must init with positive, not #{len}" if len < 0 fill_to( len , 32 ) unless len == 0 #32 being ascii space @@ -189,7 +192,19 @@ module Parfait def padded_length Object.padded( 4 * get_type().instance_length + @char_length ) end - + def start_with(str1,prefix1) + @str=str1 + @prefix=prefix1 + s = @str.size() + temp="" + for i in 0..s-1 + temp=temp+@str[i] + if temp==@prefix + return true + end + end + return false + end private def check_length raise "Length out of bounds #{char_length}" if @char_length > 1000 diff --git a/test/parfait/test_word2.rb b/test/parfait/test_word2.rb index fc8e2e23..de533dac 100644 --- a/test/parfait/test_word2.rb +++ b/test/parfait/test_word2.rb @@ -6,6 +6,11 @@ module Parfait super @word = Parfait::Word.new(5) end + def test_start_with + assert_equal true , @word.start_with("hello","hell") + assert_equal true , @word.start_with("Adbfgsj","Adbf") + assert_equal false , @word.start_with("Vanila","van") + end def test_len assert_equal 5 , @word.length end From fa28475b71c545079caad2089bc9c39f274e177f Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 16:54:29 +0530 Subject: [PATCH 2/6] review comments incorporated --- lib/parfait/word.rb | 11 +++++++---- test/parfait/test_word2.rb | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index e8e4804f..1dd123c6 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -192,16 +192,19 @@ module Parfait def padded_length Object.padded( 4 * get_type().instance_length + @char_length ) end - def start_with(str1,prefix1) - @str=str1 - @prefix=prefix1 + def start_with(other) + return false if other.length > self.length + @str=self.to_string + @prefix=other.to_string s = @str.size() temp="" - for i in 0..s-1 + i=0 + while i<=s-1 temp=temp+@str[i] if temp==@prefix return true end + i=i+1 end return false end diff --git a/test/parfait/test_word2.rb b/test/parfait/test_word2.rb index de533dac..9908d24e 100644 --- a/test/parfait/test_word2.rb +++ b/test/parfait/test_word2.rb @@ -6,11 +6,7 @@ module Parfait super @word = Parfait::Word.new(5) end - def test_start_with - assert_equal true , @word.start_with("hello","hell") - assert_equal true , @word.start_with("Adbfgsj","Adbf") - assert_equal false , @word.start_with("Vanila","van") - end + def test_len assert_equal 5 , @word.length end @@ -88,6 +84,30 @@ module Parfait two = Parfait.new_word("one") assert one.compare(two) end + + def test_start_with + one = Parfait.new_word("Hello") + two = Parfait.new_word("Hel") + assert one.start_with(two) + one = Parfait.new_word("Vanilla") + two = Parfait.new_word("Va") + assert one.start_with(two) + one = Parfait.new_word("hello") + two = Parfait.new_word("hellooo") + assert_equal false, one.start_with(two) + one = Parfait.new_word("bajjs") + two = Parfait.new_word("bgjj") + assert_equal false, one.start_with(two) + #one = Parfait.new_word("hel") + #two = Parfait.new_word("hellooo") + #assert_equal false, one.start_with(two) + #one = Parfait.new_word("Vanilla") + #two = Parfait.new_word("vani") + #assert one.start_with(two) + #assert_equal true , @word.start_with("hello","hell") + #assert_equal true , @word.start_with("Adbfgsj","Adbf") + #assert_equal false , @word.start_with("Vanila","van") + end def test_first_char one = Parfait.new_word("one") one.set_char(0 , "T".ord) From 2e3b614c927f34fddfb82a768897559dd1e94966 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 18:57:29 +0530 Subject: [PATCH 3/6] review comments incorporated --- lib/parfait/word.rb | 12 ++++++------ test/parfait/test_word2.rb | 18 ++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index 1dd123c6..ecb4f5a6 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -13,7 +13,7 @@ module Parfait # Object length is measured in non-type cells though class Word < Data8 - attr_reader :char_length , :str, :prefix ,:next_word + attr_reader :char_length ,:next_word def self.type_length @@ -194,14 +194,14 @@ module Parfait end def start_with(other) return false if other.length > self.length - @str=self.to_string - @prefix=other.to_string - s = @str.size() + str=self.to_string + prefix=other.to_string + s = str.size() temp="" i=0 while i<=s-1 - temp=temp+@str[i] - if temp==@prefix + temp=temp+str[i] + if temp==prefix return true end i=i+1 diff --git a/test/parfait/test_word2.rb b/test/parfait/test_word2.rb index 9908d24e..cddc13e0 100644 --- a/test/parfait/test_word2.rb +++ b/test/parfait/test_word2.rb @@ -85,29 +85,27 @@ module Parfait assert one.compare(two) end - def test_start_with + def test_start_with1 one = Parfait.new_word("Hello") two = Parfait.new_word("Hel") assert one.start_with(two) + end + def test_start_with2 one = Parfait.new_word("Vanilla") two = Parfait.new_word("Va") assert one.start_with(two) + end + def test_start_with3 one = Parfait.new_word("hello") two = Parfait.new_word("hellooo") assert_equal false, one.start_with(two) + end + def test_start_with4 one = Parfait.new_word("bajjs") two = Parfait.new_word("bgjj") assert_equal false, one.start_with(two) - #one = Parfait.new_word("hel") - #two = Parfait.new_word("hellooo") - #assert_equal false, one.start_with(two) - #one = Parfait.new_word("Vanilla") - #two = Parfait.new_word("vani") - #assert one.start_with(two) - #assert_equal true , @word.start_with("hello","hell") - #assert_equal true , @word.start_with("Adbfgsj","Adbf") - #assert_equal false , @word.start_with("Vanila","van") end + def test_first_char one = Parfait.new_word("one") one.set_char(0 , "T".ord) From 5bef23d6e16a1921f6fec647cd23fbc62949b420 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 20:45:00 +0530 Subject: [PATCH 4/6] review comments incorporated --- lib/parfait/word.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index ecb4f5a6..dc929eb6 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -194,14 +194,12 @@ module Parfait end def start_with(other) return false if other.length > self.length - str=self.to_string - prefix=other.to_string - s = str.size() - temp="" + s = other.length + temp = Word.new(other.length) i=0 while i<=s-1 - temp=temp+str[i] - if temp==prefix + temp.set_char(i,self.get_char(i)) + if temp.compare(other) return true end i=i+1 From 055a4d622ba2d4db5d1f03055bdaf69d8c70a6d5 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 21:34:12 +0530 Subject: [PATCH 5/6] removed comments --- lib/parfait/word.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index dc929eb6..7f002412 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -28,8 +28,6 @@ module Parfait def initialize( len ) super() @char_length = 0 - #@str=str1 - #@prefix=prefix1 raise "Must init with int, not #{len.class}" unless len.kind_of? ::Integer raise "Must init with positive, not #{len}" if len < 0 fill_to( len , 32 ) unless len == 0 #32 being ascii space From 4f8a5f2a78d05cc15e2fde4d4ccd0b5933c740b5 Mon Sep 17 00:00:00 2001 From: chhavikirtani2000 <55941961+chhavikirtani2000@users.noreply.github.com> Date: Sat, 28 Mar 2020 21:42:27 +0530 Subject: [PATCH 6/6] removed temp --- lib/parfait/word.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/parfait/word.rb b/lib/parfait/word.rb index 7f002412..60776549 100644 --- a/lib/parfait/word.rb +++ b/lib/parfait/word.rb @@ -193,16 +193,14 @@ module Parfait def start_with(other) return false if other.length > self.length s = other.length - temp = Word.new(other.length) i=0 while i<=s-1 - temp.set_char(i,self.get_char(i)) - if temp.compare(other) - return true + if other.get_char(i) != self.get_char(i) + return false end i=i+1 end - return false + return true end private def check_length