Skip to content

Instantly share code, notes, and snippets.

@hayksaakian
Created May 9, 2015 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayksaakian/d07776fc755d8df312c4 to your computer and use it in GitHub Desktop.
Save hayksaakian/d07776fc755d8df312c4 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# started at 2:29
# 1
test_list = [42, 512, 7, 3, 11]
# use builtin summing function
expected_value = test_list.reduce(:+)
def sum_for(list)
total = 0
for i in list
total = total + i
end
return total
end
def sum_while(list)
total = 0
while list.any?
total = total + list.pop
end
return total
end
def sum_recursion(list)
if list.length == 1
return list.pop
else
return list.pop + sum_recursion(list)
end
end
puts "test sum_for, passing?"
puts sum_for(test_list.clone) == expected_value
puts "test sum_while, passing?"
puts sum_while(test_list.clone) == expected_value
puts "test sum_recursion, passing?"
puts sum_recursion(test_list.clone) == expected_value
# 2
test_list_b = ["a", "b", "c", "d"]
expected_combined = [42, "a", 512, "b", 7, "c", 3, "d", 11]
def alternating_merge(a, b)
combined = []
while a.any? or b.any?
combined << a.shift if a.any?
combined << b.shift if b.any?
end
return combined
end
puts "test alternating_merge, passing?"
# puts 'expected'
# puts expected_combined
# puts 'actual'
# puts alternating_merge(test_list.clone, test_list_b.clone)
puts expected_combined == alternating_merge(test_list.clone, test_list_b.clone)
# 3
fib_10 = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
def fib_at(n)
if n <= 1
return 0
elsif n == 2
return 1
elsif n == 3
return 1
else
return fib_at(n-1)+fib_at(n-2)
end
end
def fib(n)
return (1..n).to_a.map{|x| fib_at(x)}
end
puts "test fib, passing?"
test = fib_10 == fib(10)
puts test
unless test
puts 'expected'
puts fib_10
puts 'actual'
puts fib(10)
end
# 4
list_4 = [50, 2, 1, 9]
def largest_combined(list)
slist = list.map{|i| i.to_s}.sort.reverse
return slist.join('').to_i
end
puts "test largest_combined, passing?"
puts 95021 == largest_combined(list_4)
# 5
def p5(possibilities, limit)
# blanks = (list.length-1).times.map{|x| :_}
# pos_map = alternating_merge(list, blanks)
new_possibilities = []
to_remove = []
possibilities.each do |l|
last_i = l.last
new_possibilities << l.clone.push(:+).push(last_i+1)
new_possibilities << l.clone.push(:-).push(last_i+1)
new_possibilities << l.clone.push(:j).push(last_i+1)
end
if new_possibilities.last.length < limit
return p5(new_possibilities, limit)
else
return compute_possibilities(new_possibilities)
end
end
def compute_possibilities(raw_os)
# resolve joins
os = raw_os.map{|o|
rp = o.clone
while rp.include?(:j)
j = rp.index(:j)
nj = (rp[j-1].to_s + rp[j+1].to_s).to_i
rp[j-1] = nil
rp[j] = nj
rp[j+1] = nil
rp.delete(nil)
end
rp
}
correct_os = []
os.each_with_index do |o, oi|
sum = o[0]
o.each_with_index do |v, idx|
next unless v.is_a?(Symbol)
sum = sum.send(v, o[idx+1])
end
if sum == 100
correct_os << raw_os[oi]
puts "100 = " + o.join(" ")
puts raw_os[oi].join(" ")
end
end
return correct_os
end
puts "searching for #5 answers"
answers = p5([[1]], 17)
# distractions caused
# a paused at
# 3:12 back at 3:26
# 14 minutes
# 3:36 back at 3:38
# 4 minutes
# 3:41 back at 3:57
# 16 minutes
# 4:01 back at 4:09
# 8 minutes
# 4:16 back at 4:42
# 26 minutes
# 4:47 back at 4:52
# 5 minutes
# done at 5:05
# total time = 2:29 to 5:05
# 2 hours and 36 minutes
# minus
# 73 = 14+4+16+8+26+5
# 1 hour 13 minutes of distractions
# finished in 1 hour and 23 minutes of work
@hayksaakian
Copy link
Author

apparently my #4 was wrong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment