Created
June 9, 2015 14:04
-
-
Save teknocat/12ee1d1a260a13fa7c9f to your computer and use it in GitHub Desktop.
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
# 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 | |
# | |
# 問題2 | |
# 交互に要素を取ることで、2つのリストを結合する関数を記述せよ。例えば [a, b, c]と[1, 2, 3]という2つのリストを与えると、関数は [a, 1, b, 2, c, 3]を返す。 | |
#@list1 = %w(a b c).freeze | |
@list1 = %w(a b c d e f).freeze | |
#@list2 = %w(1 2 3).freeze | |
@list2 = %w(1 2 3 4 5).freeze | |
# @param [Array] list1 | |
# @param [Array] list2 | |
def func1(list1, list2) | |
result = [] | |
list1.each do |val| | |
result.push(val) | |
list2.empty? ? result.push(nil) : result.push(list2.shift) | |
end | |
unless list2.empty? | |
list2.each do |val| | |
result.push(nil) | |
result.push(val) | |
end | |
end | |
result | |
end | |
p "func1 = #{func1(@list1.dup, @list2.dup)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment