diff --git a/archive/r/ruby/LongestPalindromicSubstring.rb b/archive/r/ruby/LongestPalindromicSubstring.rb new file mode 100644 index 000000000..7d8c6783f --- /dev/null +++ b/archive/r/ruby/LongestPalindromicSubstring.rb @@ -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") diff --git a/archive/r/ruby/LongestWord.rb b/archive/r/ruby/LongestWord.rb new file mode 100644 index 000000000..869969f9e --- /dev/null +++ b/archive/r/ruby/LongestWord.rb @@ -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 + + \ No newline at end of file diff --git a/archive/r/ruby/README.md b/archive/r/ruby/README.md index e2d71500e..12c7b04b5 100644 --- a/archive/r/ruby/README.md +++ b/archive/r/ruby/README.md @@ -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)] @@ -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)]