Last active
December 25, 2015 08:09
-
-
Save israelb/6945029 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
def braces(expresion) | |
if expresion.length % 2 != 0 | |
puts '0' | |
return | |
end | |
opening = [ '(', '[', '{'] | |
closing = [ ')', ']', '}'] | |
stack = [] | |
for i in 0..expresion.length - 1 | |
if opening.include? expresion[i] | |
stack << expresion[i] | |
elsif closing.include? expresion[i] | |
if stack.empty? or not pair?(stack.last, expresion[i]) | |
puts '0' | |
break | |
else | |
stack.pop | |
end | |
end | |
end | |
puts '1' if stack.empty? | |
end | |
def pair?(open_exp, last_exp) | |
opening = { '(' => 1, '[' => 2, '{' => 3 } | |
closing = { ')' => 1, ']' => 2, '}' => 3 } | |
index = opening[open_exp] | |
last_exp == closing.key(index) | |
end | |
def analize(expresions) | |
expresions.each do |exp| | |
braces(exp) | |
end | |
end | |
#analize([')(){}']) | |
#analize(['{()()}', '()']) | |
analize([ ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]', ')(){}', '[]({})', '([])', '{(){}}', '([)]' ]) | |
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
def palindrome words | |
words.each do |word_separate| | |
valido = false | |
permutations = word_separate.split(//).permutation.to_a | |
permutations.each do |word| | |
the_word = word.join | |
if the_word == the_word.reverse | |
puts the_word | |
valido = true | |
break # cuando lo encuentra | |
end | |
end | |
puts -1 if valido == false | |
end | |
end | |
words = ["cecarar", "civic", "agwewq","falso","chicharito","penal","tirado"] | |
palindrome words |
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
def palindrome words | |
words.each do |word_separate| | |
valido = false | |
word_separate.split(//).permutation.to_a.collect do |p| | |
if p.join == p.join.reverse | |
puts p.join | |
valido = true | |
break # cuando lo encuentra | |
end | |
end | |
puts -1 if valido == false | |
end | |
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
def palindrome words | |
return false if words.length >= 1000 # max 1000 word in the list | |
words.each do |word| | |
if canRearrangeToPalindrome(word) | |
compare = word.split(//) | |
if compare == compare.reverse | |
puts compare.join | |
else | |
rearrange(word) | |
end | |
else | |
puts -1 | |
end | |
end | |
return | |
end | |
def rearrange(word) | |
for i in 0..word.length - 1 | |
if i == word.length - 1 | |
# busqueda en el centro | |
if word[0] == word[word.length - 1] | |
#puts "BUSQUEDA CENTRAL" | |
for j in 0..word.length - 2 | |
mid_temp = word[j+1] | |
word[j+1] = word[j+2] | |
word[j+2] = mid_temp | |
compare = word.split(//) | |
if compare == compare.reverse | |
puts compare.join | |
return true | |
end | |
end | |
else | |
return true if rearrange(word) | |
end | |
end | |
tmp = word[i] | |
word[i] = word[i + 1] | |
word[i + 1] = tmp | |
compare = word.split(//) | |
if compare == compare.reverse | |
puts compare.join | |
return true | |
end | |
end | |
return false | |
end | |
def canRearrangeToPalindrome(str) | |
return false if str.length >= 1000 # max 1000 letters in a word | |
letter_counts = {} | |
palindromo_sum = 0 | |
for i in 0..str.length - 1 | |
letter = str[i] | |
letter_counts[letter] = letter_counts[letter] || 0 | |
letter_counts[letter] += 1 | |
end | |
letter_counts.each_key do |key| | |
palindromo_sum += letter_counts[key] % 2 | |
end | |
return palindromo_sum < 2; | |
end | |
palindrome [ "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb", "bb", "dbd", "ivcci", "oyotta", "cecarar", "bbb", "babbb"] | |
# http://codereview.stackexchange.com/questions/77511/check-if-string-can-be-rearranged-into-a-palindrome |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Challenge 1: Snake
Given a square matrix of integer numbers
Your task is to
write a function that prints the matrix to the standard output (stdout) on a single line starting in the upper-left corner and continuing clockwise, in circles, from exterior towards interior.
Note that your function will receive the following arguments:
matrix
which is an array of integers giving the matrix as follows. The first width elements in the array represent the first line of the matrix, the next width elements represent the second line of the matrix and so on.
width
which is an integer value giving the width of the square matrix
Data constraints
the width value will not exceed 250
all the elements in the matrix are integer numbers in the following range [1, 231 -1
Efficiency constraints
your function is expected to print the result in less than 2 seconds