2011-06-10 2 views
1

내가 (예를 위해) N 숫자의 더 큰 2 개 세트를 찾고 있어요 말해 나는이 알고리즘했다 :변수를 2 개의 값 중 큰 값으로 설정하는 지름길이 있습니까?

def maxofarrays set1 set2 
    greater_array = [] 
    set1.each_index do |index| 
     if set1[index] > set2[index] then greater_array << set1[index] 
     else greater_array << set2[index] 
    end 
    greater_array 
end 

코드의 두 안쪽 라인에 대한 바로 가기가 거기를? 아니면 입력해야합니까?

+0

Google에서 "루비 기능 프로그래밍"을 검색하는 것이 좋습니다. – tokland

답변

6
a = [347, 163, 436, 234, 113] 
b = [213, 566, 124, 212, 963] 
c = a.zip(b).map(&:max) 
#=> [347, 566, 436, 234, 963] 
+0

[Array # zip] (http://ruby-doc.org/core/classes/Array.html#M000260)은 수신기 및 인수의 값을 인터리빙하고 [Enumerable # max] (http : // ruby-doc.org/core/)는 Enumerable에있는 모든 값의 최대 값을 반환합니다. – Phrogz