Skip to content

Instantly share code, notes, and snippets.

@teknocat
Created June 9, 2015 14:05
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 teknocat/c3aabda13990b788a74c to your computer and use it in GitHub Desktop.
Save teknocat/c3aabda13990b788a74c to your computer and use it in GitHub Desktop.
# http://www.softantenna.com/wp/software/5-programming-problems/
# https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
#
# 問題4
# 正の整数のリストを与えられたとき、数を並び替えて可能な最大数を返す関数を記述せよ。
# 例えば、[50, 2, 1, 9]が与えられた時、95021が答えとなる(解答例)。
#@list = [50, 2, 1, 9]
@list = [50, 2, 1, 9, 95, 83]
@result_all = []
# @param [Array] list
def func1(list)
create_all_combinations(list, [])
max = -1
@result_all.each do |candidate|
val_string = candidate.reduce('') {|result, num|
result + "#{num}"
}
val = val_string.to_i
max = val if val > max
end
max
end
def create_all_combinations(list, result)
# p "list = #{list}"
# p "result = #{result}"
if list.empty?
@result_all.push(result)
return
end
list.each do |val|
create_all_combinations(list.reject {|v| v == val}, [result, val].flatten)
end
end
max = func1(@list.dup)
p "result = #{@result_all}"
p "max = #{max}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment