『情報科学入門 Rubyを使って学ぶ』の練習問題の解答例の他、授業スライドの練習問題の解答例や、プログラミングのポイント等を掲載します。他クラスの方の利用歓迎。改良・訂正箇所ありましたらご指摘ください。

2010/11/09

[教]練習問題(第三章)

 教科書「第三章・条件分岐」の練習問題の解答例です。



練習3.1 (絶対値)
def abs(x)
 if x<0
  -x
 else
  x
 end
end


練習3.2 (繰り返しによる画像の作成)
load("./distance.rb") ←二点間距離を出す関数が定義されているファイル
load(“./make2d.rb”)
def b(s,x,y)
 r=s/2
 if d(x,y,r,r)>r
  1
 else
  (r-d(x,y,r,r))/(1.0*r)
 end
end
def sphere(s)
 image=make2d(s,s)
 for y in 0..(s-1)
  for x in o..(s-1)
   image[y][x]=b(s,x,y)
  end
 end
 image
end
show(sphere(20))


練習3.3 (条件分岐)
a) def d(a,b,c)
     b**2-4*a*c
   end
   の記述を前提とする。
   def solutions(a,b,c)
     if a==0 || d(a,b,c)==0
      1
    else
      if d(a,b,c)>0
        2
      else
        0
      end
     end
   end

b) def solve1(a,b,c)
     if a==0
      -1.0*c/b
    else
      if d(a,b,c)>=0
        (-b+sqrt(d(a,b,c)))/(2.0*a)
      else
        “no answer”
      end
     end
    end

c) def median(x,y,z)
      if (y<x && x<z) || (z<x && x<y)
       x
     else
       if (x<y && y<z) || (z<y && y<x)
         y
      else
         z
      end
     end
   end

d) def income_tax(income)
     a=195*0.05
     b=(330-195)*0.1
     c=(695-330)*0.2
     d=(900-695)*0.23
     e=(1800-900)*0.33
     if income<=195
       income*0.05
     else
       if income<=330
         a+(income-195)*0.2
       else
         if income<=695
           a+b+(income-330)*0.23
         else
           if income<=900
             a+b+c+(income-695)
           else
             if income<=1800
               a+b+c+d+(income-900)
             else
               a+b+c+d+e+(income-1800)
             end
           end
         end
       end
     end
    end
    income_tax(1000) 結果:176.4(万円)

e) def days_of_february(year)
     if year%400==0 || (year%4==0 && year%100!=0)
       29
     else
       28
     end
    end

f) def days_of_month(year,month)
     if month==2
       days_of_february(year)
     else
       if month==4 || month==6 || month==9 || month==11
         30
       else
         31
       end
     end
   end


練習3.4 (条件式の組み合わせ)
a) def median1(x,y,z)
     if x<y
       if x>z
         x
       else
         if y<z
           y
         else
            z
         end
       end
     else
       if y>z
         y
       else
         if x<z
           x
         else
           z
         end
       end
     end
    end

b) def median2(x,y,z)   ※3.3cと一緒です
     if (y<x && x<z) || (z<x && x<y)
       x
     else
       if (x<y && y<z) || (z<y && y<x)
         y
       else
         z
       end
     end
    end


練習3.5 (真偽値を求める関数)
a) def divisible(x,y)
     x%y==0
   end

b) def ascending(x,y,z)
     x<y && y<z
    end

c) def leap_year(y)
     year%400==0 || (year%4==0 && year%100!=0)
    end

d) def within_range(a,i)
     i>=0 && i<a.length()
   end

e) def within_image(img,x,y)
     within_range(img,y) && within_range(img[0],x)
   end


練習3.6 (論理関数)
def xor(x,y)
  x!=y
 end

def implies(x,y)
  !(x==true && y==false)
end


練習3.7 (文字列)
a) def longer(s,t)
     if s.length()==t.length()
       “same”
     else
        if s.length()>t.length()
          s
       else
         t
       end
     end
   end

b) def trim(s)
     s[1..s.length()-2]
   end

c) def upsidedown(s)
     if s.length()%2==0
       s[s.length()/2 ..s.length()-1] + s[0..s.length()/2-1]
     else
       s[s.length()/2+1 ..s.length()-1] + s[s.length()/2)..s.length()/2]
            +s[0..s.length()/2-1]
     end
    end


練習3.8 (条件分岐を使った配列の操作)
a) def length3(a,x)
     if x>0 && x<a.length-1
       3
     else
       if x==0 && a.length()==1
         1
       else
         2
       end
     end
    end

b) def image_average(a,x,y)
     if x==0
       if y==0
         (a[0][0]+a[0][1]+a[1][1]+a[1][0])/4.0
       else
         if y==a.length()-1
          (a[y][0]+a[y-1][0]+a[y-1][1]+a[y][1])/4.0
         else
           (a[y][0]+a[y-1][0]+a[y-1][1]+a[y][1]+a[y+1][1]+a[y+1][0])/6.0
         end
       end
     else
       if x==a[0].length()-1
         if y==0
           (a[0][x]+a[0][x-1]+a[1][x-1]+a[1][x])/4.0
         else
           if y==a.length()-1
             (a[y][x]+a[y-1][x]+a[y-1][x-1]+a[y][x-1])/4.0
           else
              (a[y][x]+a[y-1][x]+a[y-1][x-1]+a[y][x-1]
                  +a[y+1][x-1]+a[y+1][x])/6.0
            end
         end
       else
         (a[y][x]+a[y-1][x-1]+a[y][x-1]+a[y+1][x-1]+a[y+1][x]
                 +a[y+1][x+1]+a[y][x+1]+a[y-1][x+1]+a[y-1][x])/9.0
       end
     end
   end

   def array_average(a,x)
     if x==0
       (a[0]+a[1])/2.0
     else
       if x==a.length()-1
         (a[x-1]+a[x])/2.0
       else
         (a[x-1]+a[x]+a[x+1])/3.0
       end
     end
   end

   def sum(a,x)
     if length3(a,x)==3
       (a[x-1]+a[x]+a[x+1])/3.0
     else
       if length3(a,x)==1
         a[x]
       else
         if x==0
           (a[0]+a[1])/2.0
         else
           (a[x-1]+a[x])/2.0
         end
       end
     end
   end


練習3.9 (数列を作る)
load(“./make1d.rb”)
def gradation(n)
  image=make1d(n)
  for i in 0..n-1
    image[i]=1.0*(i+1)/n
  end
  image
end


練習3.10 (いろいろな図形)  ※make2d.rbの呼び出しを前提とする
a) def assist_a(n,x,y)
     if x>y
       1
     else
       1.0*y/n
     end
   end
   def image_a(n)
     a=make2d(n,n)
     for y in 0..n-1
       for x in 0..n-1
         a[y][x]=assist_a(n,x,y)
       end
     end
     a
   end

b) def assist_b(n,x,y)
     if (x+y)%2==0
       1
     else
       0
     end
   end
   def image_b(n)
     b=make2d(n,n)
     for y in 0..n-1
       for x in 0..n-1
         b[y][x]=assist_b(n,x,y)
       end
     end
   end

c) def image_c(n)
     c=make2d(n,n)
     for y in 0..n-1
       for x in 0..n-1
         c[y][x]=(x+y)/(2.0*n)
       end
     end
     c
   end

d) include(Math)
   def assist_d(n,x,y)
     if 2.0*y/n>sin(4*3.141592*x/n)+1
       1
     else
       1.0*y/n
     end
   end
   def image_d(n)
      d=make2d(n,n)
       for y in 0..n-1
        for x in 0..n-1
         d[y][x]=assist_d(n,x,y)
       end
     end
     d
   end

e) include(Math)
   def image_e(n)
     e=make2d(n,n)
     for y in 0..n-1
       for x in 0..n-1
         e[y][x]=(sin(4*3.141592*x/n)+1)/2.0
       end
     end
     e
   end

f) include(Math)
   def d(x,y,n)
     sqrt((x-n/2.0)**2+(y-n/2.0)**2)
   end
   def image_f(n)
     f=make2d(n,n)
     for y in 0..n-1
       for x in 0..n-1
         f[y][x]=(cos(12*3.141592*d(x,y,n)/n)+1)/2.0
       end
     end
     f
   end


練習3.11 (画像の加工)  ※make2d.rbの呼び出しを前提とする
a) def brighter(img)
     s=make2d(img.length(),img[0].length())
     for y in 0..img.length-1
       for x in 0..img[0].length()-1
         s[y][x]=(img[y][x]+1)*0.5
       end
     end
     s
   end

b) def blend(img1,img2)
     s=make2d(img1.length(),img1[0].length())
     for y in 0..img1.length-1
       for x in 0..img1[0].length()-1
         s[y][x]=(img1[y][x]+img2[y][x])/2.0
       end
     end
     s
   end

c) load(“./image_average.rb”)
   def blur(img)
     s=make2d(img.length(),img[0].length())
     for y in 1..img.length()-2
       for x in 1..img[0].length()-2
         s[y][x]=image_average9(img,x,y)
       end
     end
     s
   end


練習3.12 (色付きの画像)

0 件のコメント:

コメントを投稿