Skip to content

Instantly share code, notes, and snippets.

@faun
Created December 30, 2014 23:49
Show Gist options
  • Save faun/1e0596e1ca21c3ca1be3 to your computer and use it in GitHub Desktop.
Save faun/1e0596e1ca21c3ca1be3 to your computer and use it in GitHub Desktop.
Pallindromes
class String
def pallindrome?
string = self.downcase.gsub(/[^0-9a-z]/i,'')
return false if string.length <= 1
string.reverse == string
end
def collect_possible_starts_and_end
#{ a: [0, 1], b: [3], c: [1,4,6]}
return if @pallindrome_char_instances if @pallindrome_char_instances
@pallindrome_char_instances = {}
for i in (0..self.length-1)
if @pallindrome_char_instances.has_key?(self[i])
@pallindrome_char_instances[self[i]] << i
else
@pallindrome_char_instances[self[i]] = [i]
end
end
@pallindrome_char_instances
end
def find_pallindromes
possibles = collect_possible_starts_and_end
pallindromes =[]
for key in possibles.keys
indexes = possibles[key]
if indexes.length > 1
for start_i in indexes
for end_i in indexes
pallindromes << self[start_i..end_i] unless !self[start_i..end_i].pallindrome?
end
end
end
end
pallindromes
end
def extract_pallindrome
find_pallindromes.last
end
end
require "rspec"
require_relative "pallindrome"
describe "#pallindrome?" do
context "when given a string containing a pallindrome" do
it "returns true when it is a pallindrome" do
expect("aba".pallindrome?).to be(true)
expect("moom".pallindrome?).to be true
expect("qamoomaq".pallindrome?).to be true
expect("qamoaomaq".pallindrome?).to be true
expect("civic".pallindrome?).to be true
expect("cdr".pallindrome?).to be false
end
it "allows mixed case pallindromes" do
expect("QAmoaomAq".pallindrome?).to be true
expect("qaMOAOMaQ".pallindrome?).to be true
end
it "ignores whitespace" do
expect("A slut nixes sex in Tulsa".pallindrome?).to be true
end
it "ignores punctuation" do
expect("A car, a man, a maraca.".pallindrome?).to be true
expect("Cigar? Toss it in a can. It is so tragic.".pallindrome?).to be true
end
it "it should be case insensititve" do
expect("Aba".pallindrome?).to be(true)
end
it "should not care about non-alphanumerics" do
expect("a b, a".pallindrome?).to be(true)
end
end
context "with a string that is not a pallindrome" do
it "returns false" do
expect("cdr".pallindrome?).to eq(false)
expect("".pallindrome?).to be false
expect(".?.".pallindrome?).to be false
expect(".i.".pallindrome?).to be false
expect(".iaI.".pallindrome?).to be true
end
end
end
describe "#find_pallindromes" do
context "with a string containing at least one pallindrome" do
it "should find pallindromes in a sentence" do
expect("aba hello".find_pallindromes).to include("aba")
end
it "should find pallindromes in a sentence" do
expect("aba hello".find_pallindromes).to include("aba")
end
it "extracts pallindromes from a string" do
expect("a moom".find_pallindromes).to eq ['moom', 'oo']
expect("aba Cain: a maniac.".find_pallindromes).to eq ["aba", "a Ca", "ain: a mania", "a ma", "in: a mani", "n: a man"]
expect("a radar".find_pallindromes).to eq ["a ra", "ada", "radar"]
expect("aba hello".find_pallindromes).to eq ["aba", "ll"]
expect("Amy, must I jujitsu my ma?".find_pallindromes).to eq ["my, m", "my, must I jujitsu my m", "must I jujitsu m", "my m", "y, must I jujitsu my", "ust I jujitsu", "st I jujits", "t I jujit", "juj"]
end
it "should return an empty array when no pallindromes are present" do
expect("no palies".find_pallindromes.length).to eq(0)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment