Last active
March 11, 2019 11:25
-
-
Save volonterx/c304f100a9899ad6fa844a1430a57e81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Andres | |
def get_combinations(chars) | |
return [[]] if chars.empty? | |
(1..chars.length).map do |n| | |
get_combinations(chars.drop(n)).map{|ys| [chars.first(n).join] + ys} | |
end.flatten(1) | |
end | |
def get_polindromes(str) | |
combinations = get_combinations(str.chars) | |
combinations.map{|c| c.join(" ") if c == c.reverse}.compact | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Andrey | |
string = "geeks" | |
def is_it_a_palindrome?(s) | |
s.reverse == s | |
end | |
def all_palindromes_that_start_from_the_first_letter_of(s) | |
palindromes = [] | |
for length in 1..s.length | |
palindrome_candindate = s[0,length] | |
palindromes << palindrome_candindate if is_it_a_palindrome?(palindrome_candindate) | |
end | |
palindromes | |
end | |
def arrays_with_separetad_palindromes_from(s) | |
start_palindromes = all_palindromes_that_start_from_the_first_letter_of(s) | |
palindromes_collection = start_palindromes.map do |start_palindrome| | |
[start_palindrome, s[start_palindrome.length..-1]].reject{|e| e.empty?} | |
end | |
end | |
def replace_with_next_level_of_separated_palindromes(array) | |
variants = [] | |
array.each do |variant| | |
variants += arrays_with_separetad_palindromes_from(variant[-1]).map{|sp| variant[0...-1] + sp} | |
end | |
variants | |
end | |
def remove_non_polindrome_variants_from(arrays) | |
arrays.keep_if{|array_of_variants| is_it_a_palindrome?(array_of_variants[-1])} | |
end | |
def palindromes_list_from(s) | |
palindromes_list = [] | |
next_level_palindromes_list = arrays_with_separetad_palindromes_from(s) | |
until palindromes_list == next_level_palindromes_list do | |
palindromes_list = next_level_palindromes_list | |
next_level_palindromes_list = replace_with_next_level_of_separated_palindromes(palindromes_list).uniq | |
end | |
remove_non_polindrome_variants_from(next_level_palindromes_list) | |
end | |
puts palindromes_list_from(string).inspect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Input: "nitin" | |
Output: [["n", "i", "t", "i", "n"], ["n", "iti", "n"], ["nitin"]] | |
Input: "geeks" | |
Output: [["g", "e", "e", "k", "s"], ["g", "ee", "k", "s"]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment