Last active
August 29, 2015 14:21
-
-
Save TikiTDO/e8b7d3f718205d819602 to your computer and use it in GitHub Desktop.
My solutions to the "Five programming problems every Software Engineer should be able to solve in less than 1 hour"
This file contains hidden or 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://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour | |
def sum_a(list) | |
sum = 0 | |
for item in list | |
sum += item | |
end | |
return sum | |
end | |
def sum_b(list) | |
sum = 0 | |
while !list.empty? | |
sum += list.pop | |
end | |
return sum | |
end | |
def sum_c(list) | |
return 0 if list.empty? | |
sum = list.pop | |
return sum + sum_c(list) | |
end | |
puts sum_a([1, 4, 6, 9, 2]) | |
puts sum_b([1, 4, 6, 9, 2]) | |
puts sum_c([1, 4, 6, 9, 2]) | |
def comb(list_a, list_b) | |
ret = [] | |
max = [list_a.length, list_b.length].max | |
max.times do |i| | |
ret.push(list_a[i]) if list_a.length > i | |
ret.push(list_b[i]) if list_b.length > i | |
end | |
puts ret.to_s | |
end | |
comb([1, 2, 3, 0, 0], [5, 6, 7]) | |
def fib | |
fib = [0, 1] | |
10.times do |i| | |
fib.push(fib[i] + fib[i + 1]) | |
end | |
puts fib.to_s | |
end | |
fib | |
def max_num(list) | |
out = "" | |
out_data = {} | |
list.sort! do |x,y| # In-place sort, with (X, Y) compare function | |
x_s = x.to_s # X as a string | |
y_s = y.to_s # Y as a string | |
x_l = x_s.length # X string length | |
y_l = y_s.length # Y string length | |
max = [x_l, y_l].max # Longest string | |
ret = 0 # Return initialize | |
max.times do |i| # for(i = 0; i < max; i++) | |
x_i = (x_l > i ? i : 0) # Compare either current number, or the first number if past string limit | |
y_i = (y_l > i ? i : 0) # Ditto | |
if x_s[x_i] > y_s[y_i] # Handle X > Y case | |
ret = -1 | |
break | |
elsif x_s[x_i] < y_s[y_i] # Handle Y > X case | |
ret = +1 | |
break | |
end # Default to equal | |
end | |
ret # Return (X, Y) comparison | |
end | |
puts list.join(":") | |
end | |
max_num([51, 5, 2]) | |
max_num([554, 57, 5]) | |
max_num([2, 5, 51]) | |
max_num([420, 42, 423]) | |
def solve_100 | |
nums = "1%s2%s3%s4%s5%s6%s7%s8%s9" | |
try = [""] * 8 | |
final = ["-"] * 8 | |
while try != final | |
carry = false | |
(0..7).each do |i| | |
cur = try[i] | |
if cur == "" && (carry || i == 0) | |
try[i] = "+" | |
carry = false | |
elsif cur == "+" && (carry || i == 0) | |
try[i] = "-" | |
carry = false | |
elsif (carry || i == 0) | |
try[i] = "" | |
carry = true | |
end | |
end | |
to_eval = nums % try | |
if eval(to_eval) == 100 | |
puts to_eval | |
end | |
end | |
end | |
solve_100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment