Skip to content

Add Longest Palindromic Substring in Ruby #4051

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

Closed
wants to merge 9 commits into from
Closed
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
32 changes: 32 additions & 0 deletions archive/r/ruby/LongestPalindromicSubstring.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def longest_palindromic_substring(s)
return "Usage: please provide a string that contains at least one palindrome" if s.nil? || s.empty?

longest = ""

# Loop through each character in the string
(0...s.length).each do |i|
# Check for the longest odd-length palindrome centered at `i`
odd_palindrome = expand_from_center(s, i, i)
# Check for the longest even-length palindrome centered between `i` and `i+1`
even_palindrome = expand_from_center(s, i, i + 1)

# Update longest if we found a longer palindrome
longest = [longest, odd_palindrome, even_palindrome].max_by(&:length)
end

longest
end

# Helper function to expand from the center and find a palindrome
def expand_from_center(s, left, right)
# Expand outwards while the characters are the same
while left >= 0 && right < s.length && s[left] == s[right]
left -= 1
right += 1
end
# Return the substring that is a palindrome
s[(left + 1)...right]
end

# Example usage
puts longest_palindromic_substring("paapaapap")
Comment on lines +31 to +32
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is supposed to accept command-line arguments. See the project description for details. In general, all of the projects are list here. Just click on the link for the project you are interested in.

28 changes: 28 additions & 0 deletions archive/r/ruby/LongestWord.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def longest_word_length(input)

# Split the input by spaces
words = input.split(" ")

# Start with 0 as the longest length
max_length = 0

# Go through each word
words.each do |word|
if word.length > max_length
max_length = word.length
end
end

# Return the length of the longest word
max_length
end

# Check if the program has received input from the command line
if ARGV.length > 0
input_string = ARGV.join(" ")
puts longest_word_length(input_string)
else
puts "Please provide an input string."
end


2 changes: 1 addition & 1 deletion archive/r/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Below, you'll find a list of completed code snippets in Ruby. Code snippets prec
- :white_check_mark: [File Input Output in Ruby](https://sampleprograms.io/projects/file-input-output/ruby) [[Requirements](https://sampleprograms.io/projects/file-input-output)]
- :warning: [Fizz Buzz in Ruby](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+fizz+buzz+ruby) [[Requirements](https://sampleprograms.io/projects/fizz-buzz)]
- :warning: [Hello World in Ruby](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+hello+world+ruby) [[Requirements](https://sampleprograms.io/projects/hello-world)]
- :x: [Longest Palindromic Substring](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,longest+palindromic+substring&template=code-snippet-request.md&title=Add+Longest+Palindromic+Substring+in+Ruby) [[Requirements](https://sampleprograms.io/projects/longest-palindromic-substring)]
- :warning: [Palindromic Number in Ruby](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+palindromic+number+ruby) [[Requirements](https://sampleprograms.io/projects/palindromic-number)]
- :warning: [Prime Number in Ruby](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+prime+number+ruby) [[Requirements](https://sampleprograms.io/projects/prime-number)]
- :warning: [Quine in Ruby](https://github.com//TheRenegadeCoder/sample-programs-website/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+quine+ruby) [[Requirements](https://sampleprograms.io/projects/quine)]
Expand All @@ -41,7 +42,6 @@ The following list contains all of the approved programs that are not currently
- :x: [Josephus Problem](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,josephus+problem&template=code-snippet-request.md&title=Add+Josephus+Problem+in+Ruby) [[Requirements](https://sampleprograms.io/projects/josephus-problem)]
- :x: [Linear Search](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,linear+search&template=code-snippet-request.md&title=Add+Linear+Search+in+Ruby) [[Requirements](https://sampleprograms.io/projects/linear-search)]
- :x: [Longest Common Subsequence](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,longest+common+subsequence&template=code-snippet-request.md&title=Add+Longest+Common+Subsequence+in+Ruby) [[Requirements](https://sampleprograms.io/projects/longest-common-subsequence)]
- :x: [Longest Palindromic Substring](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,longest+palindromic+substring&template=code-snippet-request.md&title=Add+Longest+Palindromic+Substring+in+Ruby) [[Requirements](https://sampleprograms.io/projects/longest-palindromic-substring)]
- :x: [Longest Word](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,longest+word&template=code-snippet-request.md&title=Add+Longest+Word+in+Ruby) [[Requirements](https://sampleprograms.io/projects/longest-word)]
- :x: [Maximum Array Rotation](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,maximum+array+rotation&template=code-snippet-request.md&title=Add+Maximum+Array+Rotation+in+Ruby) [[Requirements](https://sampleprograms.io/projects/maximum-array-rotation)]
- :x: [Maximum Subarray](https://github.com/TheRenegadeCoder/sample-programs/issues/new?assignees=&labels=enhancement,maximum+subarray&template=code-snippet-request.md&title=Add+Maximum+Subarray+in+Ruby) [[Requirements](https://sampleprograms.io/projects/maximum-subarray)]
Expand Down
Loading