2014-01-09 5 views
0

ordered_vowel_words 메서드와 ordered_vowel_word? 도우미 메서드는 단어를 허용하고 단어의 모음이 (a, e, i, o, u)의 순서이면 단어를 반환합니다.이 기능은 어떻게 작동합니까?

로직을 이해하는 데 문제가 있습니다. 특히 도우미 방법의 마지막 블록 (0...(vowels_arr.length - 1)).all? do...이 어떻게 작동하는지.

누군가가 어떻게 작동하는지 설명해주세요. range에서 all?이 호출되는 방식을 이해할 수 없습니다.

def ordered_vowel_words(str) 
    words = str.split(" ") 

    ordered_vowel_words = words.select do |word| 
    ordered_vowel_word?(word) 
    end 

    ordered_vowel_words.join(" ") 
end 

def ordered_vowel_word?(word) 
    vowels = ["a", "e", "i", "o", "u"] 

    letters_arr = word.split("") 
    vowels_arr = letters_arr.select { |l| vowels.include?(l) } 

    (0...(vowels_arr.length - 1)).all? do |i| 
    vowels_arr[i] <= vowels_arr[i + 1] 
    end 
end 
+0

http://ruby-doc.org/core-2.1.0/Enumerable.html – Beartech

답변

1

나는 약간의 의견 :이 도움이

def ordered_vowel_words(str) 
    # words is a string with words separated by a whitespace. 
    # split generates an array of words from a string 
    words = str.split(" ") 

    # select only the ordered vowel words from the previous array 
    ordered_vowel_words = words.select do |word| 
    ordered_vowel_word?(word) 
    end 

    # join the ordered vowel words in a single string 
    ordered_vowel_words.join(" ") 
end 

def ordered_vowel_word?(word) 
    # THESE ARE THE VOWELS YOU FOOL 
    vowels = ["a", "e", "i", "o", "u"] 

    # transform the word in an array of characters 
    letters_arr = word.split("") 

    # select only the vowels in this array 
    vowels_arr = letters_arr.select { |l| vowels.include?(l) } 

    # generate a range from 0 to the length of the vowels array minus 2: 
    # there is this weird range because we want to iterate from the first vowel 
    # to the second to last; all? when called on a range returns true if... 
    (0...(vowels_arr.length - 1)).all? do |i| 
    # for each number in the range, the current vowel is smaller that the next vowel 
    vowels_arr[i] <= vowels_arr[i + 1] 
    end 
end 

희망을 추가했습니다!

편집 나는 마지막 블록은 매우 루비 틱 생각하지 않는 것을 추가 할 수 있습니다. 나는이 대안 구현을 제안 할 수 있습니다 :

def ordered_vowel_word?(word) 
    vowels = ["a", "e", "i", "o", "u"] 

    # transform the word in an array of characters 
    letters_arr = word.split("") 

    # select only the vowels in this array 
    vowels_arr = letters_arr.select { |l| vowels.include?(l) } 

    # from this array generate each possible consecutive couple of characters 
    vowels_arr.each_cons(2).all? do |first, second| 
    first <= second 
    end 
end 

require 'rspec/autorun' 

describe "#ordered_vowel_word?" do 
    it "tells if word is ordered" do 
    expect(ordered_vowel_word?("aero")).to be_true 
    end 

    it "or not" do 
    expect(ordered_vowel_word?("rolling")).to be_false 
    end 
end 
+0

감사합니다 "바보!" :) - 당신의 의견은 제가 뭔가를 깨닫는데 도움이되었습니다. 나는 irb에 뛰어 들었고 "a"< "e"'를 시도했고 나는 사실을 되찾았습니다. 나는 그것을 잊었다. 이제 나는 (0 ... (vowels_arr.length - 1)) 모두를 이해한다. 할 | i | vowels_arr [i] <= vowels_arr [i + 1]'하고 있습니다. 순서가 올바른지 어떻게 결정할 수 있었는지 파악할 수 없었습니다. 이제 저는'a fyz

+0

테스트를 통과하면서 Ruby-ish 구현을 추가했습니다 :) Ciao bello! –

+0

그 대안은 유쾌합니다. 아주 좋아!고마워, 안녕! – fyz

0

all? 블록은 본질적으로 vowels_arr 반복 배열은 다음 하나의 각 값과 비교된다. 모든 비교가 true을 반환하면 all? 또한 true을 반환합니다. 이는 배열이 주문됨을 의미합니다. 반복 중 하나가 false을 반환하면 all?의 반환 값은 false이 될 수도 있습니다. 이는 컬렉션이 순서가 없음을 의미합니다.

당신은 Rangehttp에 all?를 호출 할 수 //www.ruby-doc.org/core-1.9.3/Range.html 개체를 때문에 Enumerablehttp에서 Range 믹스 : //www.ruby-doc.org/core -1.9.3/Enumerable.html 모듈은 all?을 정의하는 모듈입니다.

당신은 IRB에서 다음을 시도하여 확인할 수 있습니다

Range.included_modules # => => [Enumerable, Kernel] 
0
  1. 첫 번째 부분 (0...(vowels_arr.length - 1)) 단어에 얼마나 많은 모음에 0의 범위를 생성합니다.
  2. all? 그 범위 반복하고 몇 가지 조건이 그렇지 않은 경우는 false에 해당하는 범위의 모든 요소에 대한 모든 경우 true을 반환합니다. vowels_arr[i] <= vowels_arr[i+1]가 참일 경우
  3. do |i|
  4. 1.
  5. 마지막 단계에서 생성 된 범위에있는 각 소자는 상기 조건 범위의 각 인덱스이다 를 나타내는 변수로 i 가진 블록을 도입 지금 i로 표시되는 것이 확인 .
관련 문제