Hatena::Grouprubyist

てーげーRuby

2008-06-30

練習問題 配列(後半)

| 17:06

(7) 1から100までの要素を含む配列aryから、1~10、11~20、21~30と

いうように10個の要素を含む配列を10個取り出します。取り出した

すべての配列を、順に別の配列resultに格納するとき、以下の???の

部分に当てはまる式を考えてください。

ary = [1~100の要素を含む配列]

result = Array.new

10.times { |i|

result << ary[???]

}

irb(main):174:0> ary = Array(1..100)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
irb(main):175:0> result = Array.new
=> []
irb(main):176:0> 10.times{|i| result << ary[i*10, 10]}
=> 10
irb(main):177:0> result
=> [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26, 27, 28, 29, 30], [31, 32, 33, 34, 35, 36, 37, 38, 39, 40], [41, 42, 43, 44, 45, 46, 47, 48, 49, 50], [51, 52, 53, 54, 55, 56, 57, 58, 59, 60], [61, 62, 63, 64, 65, 66, 67, 68, 69, 70], [71, 72, 73, 74, 75, 76, 77, 78, 79, 80], [81, 82, 83, 84, 85, 86, 87, 88, 89, 90], [91, 92, 93, 94, 95, 96, 97, 98, 99, 100]]

(8) 数値からなる配列nums1とnums2に対して、それらの個々の要素を足し合わせた

要素からなる配列を返すメソッドsum_arrayを定義してください。

irb(main):190:0> def sum_array(nums1, nums2)
irb(main):191:1>   nums1.each_index{|k| nums1[k] += nums2[k] }
irb(main):192:1> end
=> nil
irb(main):193:0> nums1 = Array(1..100)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
irb(main):194:0> nums2 = Array(1..100)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
irb(main):195:0> sum_array(nums1,nums2)
=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200]

Array#zipに足し算するブロックを渡して配列に詰める方がいい

irb(main):339:0> num1 = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
irb(main):340:0>  num2 = [1,2,3]
=> [1, 2, 3]
irb(main):341:0> num1.zip(num2)
=> [[1, 1], [2, 2], [3, 3], [4, nil], [5, nil]]
irb(main):342:0>  num1.zip(num2) do |a, b|
irb(main):343:1*    a ||= 0
irb(main):344:1>    b ||= 0
irb(main):345:1>  result << a + b
irb(main):346:1> end
=> nil
irb(main):347:0> result
=> [2, 4, 6, 4, 5]

(9) (、)、{、}という4つの文字を要素とした配列があります。この配列に

対して、カッコが正しく対応していうかどうかを調べるメソッド、

balanced?を定義してください。なお、「カッコが正しく対応している」とは

・(と)の数が同じ

・{と}の数が同じ

・「()」の対応と「{}」の対応が交差することがない

といった状態のことです。

def balanced?(ary)
  return false if ary.select{|e| e == '('}.size != ary.select{|e| e == ')'}.size
  return false if ary.select{|e| e == '{'}.size != ary.select{|e| e == '}'}.size

  chk_ary = []
  ary_reverse = ary.reverse
  ary.each_index do |idx|
    chk_ary << ary[idx] if ary[idx] == '(' && ary_reverse[idx] == ')'
    chk_ary << ary[idx] if ary[idx] == '{' && ary_reverse[idx] == '}'
    chk_ary << ary[idx] if ary[idx] == ')' && ary_reverse[idx] == '('
    chk_ary << ary[idx] if ary[idx] == '}' && ary_reverse[idx] == '{'
  end
  return false if chk_ary.size != ary.size
  true
end
irb(main):308:0> balanced?("{((({()})))}".scan(/./))
=> true
irb(main):311:0>  balanced?(["(", "{", "}", ")"])
=> true
irb(main):312:0> balanced?(["(", ")", "{"])
=> false
irb(main):313:0> balanced?(["{", "}", "("])
=> false
irb(main):314:0> balanced?(["(", "{", ")", "}"])
=> false

ほんとはこういう対応モノ(HTMLのタグとか)はスタックでやるものですね