Last active
August 29, 2015 14:16
-
-
Save urielhdz/5b7c60911ddd386a209f to your computer and use it in GitHub Desktop.
#RetasProgramacion 1
This file contains 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
# Evaluar archivo por cada 3 líneas | |
# La primera linea representa la cantidad de casos | |
# Evaluar el crédito C primera linea | |
# Evaluar la cantidad de elementos l | |
# Almacenar en un arreglo la cantidad de elementos | |
def execute_solution lines | |
lines.size == 3 ? inner_execution(lines) : false | |
end | |
def inner_execution lines | |
c = lines[0] | |
l = lines[1] | |
elements = parseSpaceToArray(lines[2]) | |
find_elements c,l,elements | |
end | |
def parseSpaceToArray line | |
line.split(" ") | |
end | |
def find_elements c,l,elements | |
sorted_minimum_to_maximum = elements.sort | |
sorted_maximum_to_minimum = elements.sort.reverse | |
firs_element = 0 | |
last_element = 0 | |
sorted_minimum_to_maximum.each_with_index do |i_string,index_i| | |
bandera = true | |
i = i_string.to_i | |
sorted_maximum_to_minimum.each_with_index do |j_string, index_j| | |
j = j_string.to_i | |
next if i == j and not is_repited? sorted_minimum_to_maximum, i | |
return find_and_return_indexes(i_string,j_string, elements, c) if i + j == c.to_i | |
bandera = false if i + j < c.to_i | |
end | |
next unless bandera | |
end | |
end | |
def is_repited? elements, i | |
count = 0 | |
unless elements.nil? | |
elements.each do |el| | |
count += 1 if i == el.to_i | |
return false if el.to_i > i | |
return true if count > 1 | |
end | |
end | |
end | |
def find_and_return_indexes index_j, index_i,elements,c | |
solutions = [] | |
elements.each_with_index do |el,index| | |
solutions.push(index) if el == index_j or el == index_i | |
break if solutions.size == 2 | |
end | |
solutions.sort | |
end | |
lines_per_case = 3 | |
readed_lines = 0 | |
lines = [] | |
solutions = [] | |
solucion = 1 | |
file = File.open("input.txt").each do |line| | |
if readed_lines < lines_per_case | |
lines.push line | |
if lines.size == 3 | |
numbers= execute_solution lines | |
elements = lines[2].split(" ") | |
puts "Case ##{solucion}: #{numbers[0]+1} #{numbers[1]+1}" | |
solucion += 1 | |
end | |
else | |
lines = [] | |
readed_lines = 0 | |
lines.push line | |
end | |
readed_lines += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment