Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Karis - Edges - Palindrom Check #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/palindrome_check.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
# A method to check if the input string is a palindrome.
# Return true if the string is a palindrome. Return false otherwise.
def palindrome_check(my_phrase)
raise NotImplementedError
return false if !my_phrase
return true if my_phrase == ""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could combine lines 5 and 6 to be: return true if my_phrase.length < 2

return true if my_phrase.length == 1

min = 0
max = my_phrase.length - 1
check = false

# first check if string is empty or not
# if not empty then compare the pair
# if at least one of them is empty then move onto next index until it is not empty
# then compare
# if match continue moving up the index until min > max
# if at least one pair that does not match then return false for good


until min > max
# unless my_phrase[min] == " " || my_phrase[max] == " "
until my_phrase[min] != " "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the two nested inner loops, continue to check and confirm that min is less than max before comparing the character at the index min (or max) with a white space.

min += 1
end

until my_phrase[max] != " "
max -= 1
end

if my_phrase[min] == my_phrase[max]
check = true
else
return false
end
# end

min += 1
max -= 1
# require 'pry'; binding.pry
end

return check

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the code reaches this point, you'll always be returning true. So you could get rid of the variable check and simply return true here. Accordingly, you could simplify lines 30 through 34.

end
8 changes: 8 additions & 0 deletions specs/palindrome_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
end

it "palindrome input with even character count" do

test_string = "redder"

palindrome_check(test_string).must_equal true
end

it "not a palindrome" do

test_string = "empty"

palindrome_check(test_string).must_equal false
Expand All @@ -27,37 +29,43 @@
describe "edge cases" do
# if it's a string parameter, check for empty
it "empty string reads the same backwards or forwards" do

test_string = ""

palindrome_check(test_string).must_equal true
end

# if the parameter is an object, check for nil
it "nil object is not a string" do

test_string = nil

palindrome_check(test_string).must_equal false
end

it "palindrome with spaces to ignore" do

test_string = "nurses run"

palindrome_check(test_string).must_equal true
end

it "palindrome with multiple spaces to ignore" do

test_string = " pull up "

palindrome_check(test_string).must_equal true
end

it "not a palindrome, with spaces" do

test_string = " not in "

palindrome_check(test_string).must_equal false
end

it "single character is a palindrome" do

test_string = "A"

palindrome_check(test_string).must_equal true
Expand Down