Created
October 9, 2010 02:40
-
-
Save arnab/617823 to your computer and use it in GitHub Desktop.
in reply to http://challenge.greplin.com
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
require "pp" | |
class String | |
def all_substrings | |
substrs = [] | |
start_index, end_index = [0, self.length - 1] | |
start_index.upto end_index do |i| | |
i.upto end_index do |j| | |
# puts "#{self}[#{i}, #{j}] => #{ss}" | |
substrs << self[i..j] | |
end | |
end | |
substrs.uniq! | |
end | |
end | |
def find_reversed_substring(word) | |
# pp "#{word} => #{word.all_substrings.join(',')}" | |
count = 0 | |
@data = Hash.new do |h, sub| | |
h[sub] = [] | |
end | |
word.all_substrings.each do |substr| | |
count += 1 | |
next if substr.length <= 1 | |
# puts "Testing #{substr} [count: #{count}]" # if count < 100 | |
next unless palindrome? substr | |
@data[substr.length] << substr | |
end | |
pp @data | |
end | |
def palindrome?(string) | |
string == string.reverse | |
end | |
text = <<EOF | |
FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth | |
EOF | |
# test | |
# text = <<EOF | |
# broughtracecarforthmalayalamtest | |
# EOF | |
words = text.gsub(/\s+/, ' ').gsub(/[^\w\s]/, '').split(' ') | |
words.each do |w| | |
puts "Checking word #{w}" | |
find_reversed_substring w | |
end |
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
require "set" | |
class Array | |
def sum | |
inject { |sum, n| sum += n } | |
end | |
end | |
def find_sub_arrays(nums) | |
selected_sub_arrays = Set.new | |
nums.sort! | |
puts "Whole thing: #{nums.inspect}" | |
0.upto(nums.size - 1) do |i| | |
(i).upto(nums.size - 1) do |j| | |
nums.combination(j).each do |sub_arr| | |
next if sub_arr.empty? or selected_sub_arrays.member? sub_arr | |
if candidate_array? sub_arr | |
puts "#{sub_arr.inspect} works! (current count: #{selected_sub_arrays.size + 1})" | |
selected_sub_arrays << sub_arr | |
end | |
end | |
end | |
end | |
selected_sub_arrays | |
end | |
def candidate_array?(arr) | |
# the array is already sorted | |
array = arr.dup | |
largest = array.pop | |
array.sum == largest ? true : false | |
end | |
nums = [ 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99 ] | |
# nums = [ 1, 2, 3, 4, 6 ] | |
puts "answer: #{find_sub_arrays(nums).size}" |
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
require "pp" | |
def next_fibonacci(start_num, end_num) | |
start_num + end_num | |
end | |
def find_fibonacci_after(prev_num, next_num, lower_bound) | |
next_fib = next_fibonacci(prev_num, next_num) | |
if ( next_fib >= lower_bound ) && ( prime? next_fib ) | |
puts " ==========> found! #{next_fib} <============" | |
else | |
puts "Looking for the next fib after: #{next_fib}" | |
find_fibonacci_after(next_num, next_fib, lower_bound) | |
end | |
end | |
@known_primes = [2] | |
def prime?(num) | |
# rand(100) > 80 ? true : false | |
(2..num/2).each do |i| | |
return false if num % i == 0 | |
end | |
true | |
end | |
lower_bound = 217000 | |
find_fibonacci_after(1, 2, lower_bound) | |
# 514229 | |
# 2 + 3 + 5 + 61 + 281 = 352 | |
# 2 * 3 * 5 * 61 * 281 = 514230 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment