2016-06-24 4 views
2

배열이있는 경우 : array = ["ruby", "code", "library"]. 일치하는/^ library $/요소를 처음으로 어떻게 이동시킬 수 있습니까? 배열은 다음과 같이 보입니다 : array = [ "library", "ruby", "code"]루비 정렬 배열 - 일치하는 요소를 처음으로 이동

+0

Sean의 답변이 더 좋다고 생각하지만'array.sort {| str | str.match (/^library $ /)? 0 : 1}' –

답변

5

여러 가지 방법으로 수행 할 수 있습니다. 이것은 단지 호기심 하나

array = ["ruby", "code", "library"] 
array.partition { |element| element.match /^library$/ }.flatten 
+1

[Enumerable #partition] (http://ruby-doc.org/core-2.2.0/Enumerable.html#method-partition)은 주문을 보존 할 수 있지만 해당 문서는 음소거되어 있습니다. –

2

이다

[:select, :reject].map do |m| 
    ["ruby", "code", "library"].public_send(m, &(/^library/.method(:=~))) 
end.reduce :| 
+0

그것은 그것에 대해 아주 흥미로운 방법입니다. 좋은 생각의 훈련 :) – Sean

+0

@Sean 예, 이것은 실제 코드보다 루비 골프 농담에 관한 것입니다. 그러나 운동으로 그것은 괜찮습니다. 수정 해주십시오. 업데이트했습니다. – mudasobwa

+0

'reduce : |'나를 웃게했다. "meh"연산자라고 부를 수 있을까요? –

1
def move_to_front(arr, pattern) 
    mi = matching_indices(arr, pattern) 
    return arr unless mi 
    a = arr.dup 
    mi.reverse_each.with_object([]) { |i,b| b.unshift(a.delete_at(i)) }.concat(a) 
end 

def matching_indices(arr, pattern) 
    arr.each_index.select do |i| 
    case pattern 
    when Regexp then arr[i] =~ pattern 
    when Proc then pattern[arr[i]] 
    else    (arr[i] == pattern) 
    end 
    end 
end 

move_to_front ["ruby", "code", "library"], /\Alibrary\z/ 
    #=> ["library", "ruby", "code"] 
move_to_front ["ruby", "library", "code", "library"], "library" 
    #=> ["library", "library", "ruby", "code"] 
move_to_front ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/ 
    #=> ["libraries", "library", "ruby", "code"] 
move_to_front ["ruby", "libraries", "code", "library"], /\Alibrar/ 
    #=> ["libraries", "library", "ruby", "code"] 
move_to_front ["ruby", "libraries", "code", "library"], 
    ->(str) { str =~ /librar(?:ies|y)/ } 
    #=> ["libraries", "library", "ruby", "code"] 
move_to_front ("1".."9").to_a, /[13579]/ 
    #=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"] 
move_to_front ("1".."9").to_a, ->(n) { n.to_i.odd? } 
    #=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"] 
move_to_front ("1".."9").to_a, ->(n) { false } 
    #=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"] 
move_to_front ("1".."9").to_a, ->(n) { true } 
    #=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"] 

참고

matching_indices ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/ 
    #=> [1, 3] 
있어서 move_to_front는 이동하는 요소의 순서를 유지

되고 그들 움직이지 않았습니다.

0

1 센트 당 3 개. 당신이 할 수있는이

같은 두 배열 변수를 사용하는 싫어하면

array.inject([]){|a,e| e[/^library/] ? a.unshift(e) : a<<e} 

및 사례 배열에서

array & ["library"] | array 

검색 요소를 포함 여러 번이

array.find_all{ |e| e[/^library/] } + array.reject{ |e| e[/^library/] } 

된다

[array].map{|a| a & ["library"] | a}.flatten 

Th 마지막 하나 : grep 사용하기

array.grep(/library/) + array.grep(/^(?!library)/) 
+0

마지막 하나는 당신이 생각하는대로하지 않습니다. '/ [^ library] /'는'l','i','b' 등 이외의 문자를 포함하는 문자열과 일치 할 것입니다. 아마도'/^(?! library) /'를 의미할까요? –

+0

@Jordan, 나는 그걸 알고 있었고/^ (?! library)/더 좋을 것이라고 생각했지만, 그것은 "라이브러리"가 아닌 다른 문자열을 전달할 수 있기 때문에 효과가있다. – peter

+0

확실하지만 일치 할 것이다. ''lliibbrraarryy''와''yrarbil ''을 사용합니다. –