Last active
January 3, 2016 21:14
-
-
Save fabiolnm/7155e7614800011dd1e7 to your computer and use it in GitHub Desktop.
Five programming problems every software engineer should be able to solve in less than 1 hour
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
# https://www.shiftedup.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
# | |
# 5th problem: | |
# 1 _ 23456789 1 op | |
# 1 _ 2 _ 3456789 2 ops | |
# 1 _ 23 _ 456 _ 789 3 ops | |
# ... | |
# 1 _ 2 _ 3 _ 4 _ 5 _ 6 _ 7 _ 8 _ 9 8 ops | |
# | |
# 1 op combinations: | Op permutations +,- | |
# 1 _ 23456789 (op after 1) | 1 + 23456789 | |
# | 1 - 23456789 | |
# | |
# 12 _ 3456789 (op after 2) | 12 + 3456789 | |
# | 12 - 3456789 | |
# | |
# ... | |
# | |
# 2 op combinations: | Op permutations ++, +-, -+, -- | |
# 1 _ 2 _ 3456789 op after 1 and 2 | 1 + 2 + 3456789 | |
# | 1 + 2 - 3456789 | |
# | 1 - 2 + 3456789 | |
# | 1 - 2 - 3456789 | |
# | |
# 1 _ 23 _ 456789 op after 1 and 3 | 1 + 23 + 456789 | |
# | 1 + 23 - 456789 | |
# | 1 - 23 + 456789 | |
# | 1 - 23 - 456789 | |
# | |
# Left: operation position, Right: what operation goes into each position | |
# | |
def plus_minus | |
(1..8).each do |number_of_ops| | |
positions_combinations = (1..8) .to_a.combination number_of_ops | |
operations_permutations = %w(+ -) .repeated_permutation number_of_ops | |
positions_combinations.each do |position_combination| | |
operations_permutations.each do |operations| | |
expr = "123456789" | |
position_combination.each_with_index do |position, index| | |
expr.gsub! /#{position}/, "#{position}#{operations[index]}" | |
end | |
puts expr if eval(expr) == 100 | |
end | |
end | |
end | |
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
def sum_for(list) | |
sum = 0 | |
for i in 0..(list.size - 1) | |
sum += list[i] | |
end | |
sum | |
end | |
def sum_while(list) | |
sum, i = 0, 0 | |
while i < list.size | |
sum += list[i] | |
i += 1 | |
end | |
sum | |
end | |
def sum_recursive(list) | |
return 0 if list.size == 0 | |
list[0] + sum_recursive(list[1..-1]) | |
end | |
tests = [ | |
[], | |
[1], | |
[1,2], | |
[1,2,3] | |
] | |
tests.each do |list| | |
puts "List #{list}" | |
puts "For #{sum_for list}" | |
puts "While #{sum_while list}" | |
puts "Recursive #{sum_recursive list}" | |
puts | |
end | |
def combine(a, b) | |
a.map.with_index {|x, i| [x, b[i] ] }.flatten | |
end | |
puts "Combine #{combine [:a, :b, :c], [1, 2, 3]}" | |
def fibonacci_100 | |
x = [0, 1] | |
while x.size != 100 | |
x << x[-1] + x[-2] | |
end | |
puts "Fibonacci (#{x.size}): #{x}" | |
end | |
fibonacci_100 | |
def largest_permutation_number(list) | |
max, winner = 0, nil | |
list.permutation(list.size).each do |permut| | |
value = permut.join.to_i | |
max, winner = value, permut if value >= max | |
end | |
[max, winner] | |
end | |
puts "Largest permut number: #{largest_permutation_number [50, 2, 1, 9]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment