Skip to content

Instantly share code, notes, and snippets.

@petehuang
Created May 12, 2013 01:43
Show Gist options
  • Save petehuang/5562079 to your computer and use it in GitHub Desktop.
Save petehuang/5562079 to your computer and use it in GitHub Desktop.
Cue Challenge answers
s = File.open("gettysburg.txt").read().downcase
maxrange = s.length
matched = {}
for range in (2..maxrange)
(matched["#{range}"] ||= []) << (0..s.length - 1).find_all { |i| s[i,range] == s[i,range].reverse }
end
matched.delete_if { |key,value| value.last.length < 2 }
#output shows text position 204 with length 7
s[204,7]
require 'prime'
def is_divisor?(x,i)
if x % i == 0
return true
else
return false
end
end
def fib_prime_over(x)
i1,i2 = 1,1
while i1 < x
i1,i2 = i2, i2+i1
end
until i2.prime?
i1,i2 = i2, i2+i1
end
return i2
end
def add_prime_divisors(x)
divisors = []
for i in 2..x
if i.prime? && is_divisor?(x,i)
divisors.push(i)
end
end
divisors.inject(:+)
end
step1 = fib_prime_over(227000)
puts step1
step2 = step1+1
answer = add_prime_divisors(step2)
puts answer
#For an individual subset,
#
#1. Remove elements (number of elements between 0 and size-3)
#2. Set sum equal to the largest number (largest number is the last one in a sorted array).
#3. Add up the rest of the numbers and check to see if it's equal to a sum
#4. If it's equal, put it in an array
#Then add up the number subsets in the array
#
#This code also reads the file in and coverts the numbers, initially strings in an array, to integers in an array
#This is so I can work with it later
#
require 'csv'
subsets = 0
combos_to_remove = []
string_numbers = CSV.read("numbers.csv").last.each { |element| element.strip! }
numbers = []
string_numbers.each do |number|
numbers.push(number.to_i)
end
max_to_remove = numbers.length - 3
for i in 1..max_to_remove
combos_to_remove.push(numbers.combination(i).to_a)
end
combos_to_remove.flatten!(1)
combos_to_remove.each do |combo|
temparray = numbers.dup
temparray = temparray - combo
last_element = temparray.sort.last
temparray.delete(last_element)
if temparray.inject(:+) == last_element
subsets += 1
end
end
puts subsets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment