Hatena::Grouprubyist

てーげーRuby

2008-06-26[たのしいRuby] 練習問題 配列(前半)

(1) 1から100までの整数を含む配列を作ってください

Array(1..100)
irb(main):118:0> 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]


(2) (1)の配列の要素をすべて100倍した要素を含む、新しい配列を作って

ください。また、新しい配列を作成せずに、すべての要素を100倍した

ものに置き換えてください。

irb(main):120:0> ary.map do |elem|
irb(main):121:1*  elem * 100
irb(main):122:1> end
=> [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900, 7000, 7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900, 8000, 8100, 8200, 8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000, 9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000]
irb(main):123:0> ary.map! do |elem|
irb(main):124:1*  elem * 100
irb(main):125:1> end
=> [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, 5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, 6800, 6900, 7000, 7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900, 8000, 8100, 8200, 8300, 8400, 8500, 8600, 8700, 8800, 8900, 9000, 9100, 9200, 9300, 9400, 9500, 9600, 9700, 9800, 9900, 10000]


(3) (1)の配列から3の倍数だけを取り出して、新しい配列を作ってください。

また、新しい配列を作成せずに、3の倍数以外の数を削除してください。

irb(main):149:0> ary.reject do |e|
irb(main):150:1*   e % 3 == 0
irb(main):151:1> end
=> [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 100]
irb(main):152:0> ary.reject! do |e|
irb(main):153:1*   e % 3 == 0
irb(main):154:1> end
=> [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80, 82, 83, 85, 86, 88, 89, 91, 92, 94, 95, 97, 98, 100]

(4) (1)の配列を次の3つの方法で逆順に並べ換えてください。

(a) Array#reverse

(b) Array#sort

(c) Array#sort_by

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

(5) (1)の配列に含まれる整数の和を次の2つの方法で求めてください。

(a) Array#each

(b) Array#inject

irb(main):182:0> sum = 0
=> 0
irb(main):183:0> ary.each do |e|
irb(main):184:1*  sum += e
irb(main):185:1> end
=> [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):186:0> sum
=> 5050
irb(main):188:0> ary.inject(0) do |r, e|
irb(main):189:1*  r + e
irb(main):190:1> end
=> 5050


(6) randメソッドを利用して(1)の配列をでたらめな順番に並べ換えてください。

irb(main):001:0> def detarame
irb(main):002:1>   from_ary = Array(1..100)
irb(main):003:1>   to_ary = Array(1..100)
irb(main):004:1>   to_ary.each{|e| e = nil}
irb(main):005:1>
irb(main):006:1*   from_ary.each do |e|
irb(main):007:2*     begin
irb(main):008:3*       index = rand(to_ary.size + 1)
irb(main):009:3>     end while(!to_ary[index])
irb(main):010:2>     to_ary[index] = e
irb(main):011:2>   end
irb(main):012:1>
irb(main):013:1*   to_ary
irb(main):014:1> end
=> nil
irb(main):015:0> detarame
=> [1, 93, 94, 41, 5, 52, 7, 36, 9, 87, 11, 50, 13, 14, 60, 85, 80, 70, 19, 56, 72, 22, 23, 24, 25, 1, 27, 89, 68, 2, 31, 32, 77, 73, 29, 36, 37, 38, 39, 40, 57, 55, 43, 17, 86, 58, 47, 71, 22, 96, 51, 47, 25, 44, 55, 56, 97, 43, 59, 60, 61, 34, 48, 64, 90, 69, 99, 68, 21, 27, 75, 35, 95, 74, 75, 88, 91, 83, 79, 80, 59, 37, 83, 84, 85, 86, 92, 84, 63, 98, 74, 49, 67, 94, 28, 82, 39, 98, 99, 100]
irb(main):016:0> detarame
=> [99, 2, 59, 22, 16, 6, 26, 94, 9, 10, 11, 60, 23, 14, 62, 21, 93, 29, 45, 69, 43, 22, 23, 77, 52, 26, 27, 92, 29, 17, 91, 100, 33, 73, 87, 86, 48, 66, 76, 40, 41, 42, 85, 65, 33, 70, 2, 28, 54, 3, 96, 42, 53, 54, 64, 56, 57, 81, 59, 60, 13, 68, 63, 64, 83, 67, 63, 68, 69, 97, 71, 56, 73, 74, 75, 98, 77, 51, 79, 32, 41, 82, 25, 95, 79, 38, 87, 88, 84, 88, 78, 92, 93, 90, 95, 96, 97, 40, 99, 100]
irb(main):017:0> detarame.size
=> 100
irb(main):018:0> detarame
=> [93, 2, 3, 4, 5, 45, 7, 8, 9, 71, 44, 66, 13, 57, 96, 75, 97, 18, 22, 20, 13, 86, 80, 72, 25, 19, 27, 28, 29, 30, 31, 58, 46, 34, 33, 87, 85, 62, 92, 79, 20, 50, 43, 44, 4, 30, 47, 99, 51, 8, 51, 52, 74, 25, 23, 59, 90, 91, 60, 60, 61, 62, 43, 53, 65, 2, 67, 68, 69, 84, 39, 72, 73, 74, 95, 94, 88, 52, 81, 98, 70, 67, 89, 47, 77, 86, 78, 73, 89, 76, 91, 100, 29, 1, 64, 68, 97, 98, 37, 36]

お手本はこれ

puts array.sort_by{|i| rand }

これはちょっと自力で書けない。。


(3) randメソッドを用いて、1~6までの数字をランダムに返すメソッドdice

定義してください。

irb(main):050:0> def dice
irb(main):051:1>  rand(6)+1
irb(main):052:1> end
=> nil
irb(main):053:0> dice
=> 5
irb(main):054:0> dice
=> 3
irb(main):055:0> dice
=> 5
irb(main):056:0> dice
=> 4

(4) 整数numが素数であるかどうかを調べるメソッドprime?を定義してください。

def prime?(num)
  prime = true
  2.upto(num - 1) do |n|
    if num % n == 0
      prime = false
    end
  end
  prime
end