【備忘録】#基本的なプログラミング-１ 

自分用の復習メモ。

「以下のコードの実行結果を表してください」シリーズです。
問いの後に、答えを記載しています。

##問1

```
a = 1
b = 2
c = a + b
c = c + 1

puts c
```

4

##問2

```
a = 8
b = 2
c = a - b
puts c

c = c / b
puts c
```

3

##問3
```
a = 6
b = 2
c = a * b
puts c

c = c + 1
puts c
```
13

##問4

```
a = 6
b = 2
c = a * b
puts c

c = c / a
puts c
```
2

##問5

```
age = 0
price = 0

if age == 0
  price = 100
else
  price = 500
end

puts price
```
100

##問6

```
age = 1
price = 0

if age == 0
  price = 100
elsif age == 1
  price = 300
else
  price = 500
end

puts price
```

300

##問7

```
age = 2
birth = 2000
price = 0

if age == 2 && birth == 2001
  price = 100
else
  price = 500
end

puts price
```

500

##問8

```
age = 2
birth = 2000
price = 0

if age == 2 || birth == 2001
  price = 100
else
  price = 500
end

puts price
```

100

##問9

```
age = 2
birth = 2000
price = 0

if age == 2
  if birth == 2001
    price = 100
  else
    price = 300
  end
else
  price = 500
end

puts price
```
300

##問10

```
money = 1000
money = money * 1.1
money = money * 1.1
money = money * 1.1

puts money.to_i
```

1331

##問11

```
money = 1000
1.upto(3) do
  money = money * 1.1
end
puts money.to_i
```

1331

「money = money * 1.1」を1から3回まで（1, 2, 3）繰り返して整数で表してね、ということなので、やっている計算は問10と同じ。


##問12

```
money = 1000
3.times do
  money = money * 1.1
end
puts money.to_i
```

1331

「money = money * 1.1」を3回繰り返して整数で表してね、ということなので、こちらも問10、11と計算式は同じ。

##問13

```
money = 1000
rate = 1.1
2001.upto(2004) do
  money = money * rate
end
puts money.to_i
```
繰り返しは、
2001回目
2002回目
2003回目
2004回目
→の計4回分

「money = money * rate」＝「1000 * 1.1」を4回繰り返した整数を表示してね、ということなので、実行結果は1464。

##問14

```
2001.upto(2004) do
  puts "hello"
end
```

hello　　#2001回目
hello　　#2002回目
hello　　#2003回目
hello　　#2004回目

putsなので改行ありで、helloが縦に4つ並ぶのが正解。

##問15

```
money = 1000
rate = 1.1
(2001..2010).step(2) do
  money = money * rate
end
puts money.to_i
```

「money = money * rate」を、2001から2010まで1つ飛ばした分だけ繰り返して整数で表示してくださいね、ということなので
2001
2003
2005
2007
2009
→の計5回、1000 * 1.1を繰り返す。実行結果は1610。

##問16

```
money = 1000
(1..10).each do
  (1..10).each do
    money = money + 1
  end
end

puts money
```

1100

1回目...「money = money + 1」つまり「1000 + 1」を10回繰り返す　→ 1010
2〜10回目・・・同じことを1から10まで（10回）繰り返すので、実行結果は1100。

##問17

```
val = "cebu"
puts "Hello #{val} welcome"
```

Hello cebu welcome

##問18

```
val = 1
puts "Hello #{1 * 10} welcome"
```
Hello 10 welcome

##問19

```
money = 1000
rate = 1.1
2001.upto(2004) do |year|
  money = money * rate
  puts "I got #{money.to_i} in #{year}."
end
```

「money = money * rate
　puts "I got #{money.to_i} in #{year}."」
この部分を、2001〜2004回目分（＝計4回）繰り返すので、

I got 1100 in 2001.
I got 1210 in 2002.
I got 1331 in 2003.
I got 1464 in 2004.

##問20

```
(1..10).each do |i|
  (1..10).each do |v|
    puts "i = #{i}" if i % 2 == 0
    puts "v = #{v}" if v % 2 == 1
  end
end
```

「1から10までを１つずつ順番に取り出して i という変数に格納していく間に、2で割り切れる（＝偶数）があった場合は、"i = #{i}"と表示してね」のループ1回目。その中に「1から10までを１つずつ順番に取り出して v という変数に格納していく間に、2で割り切れない（＝奇数）があった場合は、"v = #{v}"と表示してね」という指示あり。

i = 1
v = 1から10

→ iは奇数なので、表示なし。
→ vを1から10までの奇数を表示

v = 1
v = 3
v = 5
v = 7
v = 9

i = 2の時も、同じ条件でループ。

i = 2
v = 1
i = 2
　　#ココにv=2が入っているが、奇数でないので表示されていないだけ。
i = 2
v = 3
i = 2
　　#ココにv=4が入っているが、奇数でないので表示されていないだけ。
i = 2
v = 5
i = 2
　　#ココにv=6が入っているが、奇数でないので表示されていないだけ。
i = 2
v = 7
i = 2
　　#ココにv=8が入っているが、奇数でないので表示されていないだけ。
i = 2
v = 9
i = 2
　　#ココにv=10が入っているが、奇数でないので表示されていないだけ。

i = 3の時も、同じ条件でループ。iが偶数でないので、vのみ表示。

v = 1
v = 3
v = 5
v = 7
v = 9

i = 4の時も、同じ条件でループ。

i = 4
v = 1
i = 4
i = 4
v = 3
i = 4
i = 4..........

のように進むので、実行結果は

v = 1
v = 3
v = 5
v = 7
v = 9
i = 2
v = 1
i = 2
i = 2
v = 3
i = 2
i = 2
v = 5
i = 2
i = 2
v = 7
i = 2
i = 2
v = 9
i = 2
v = 1
v = 3
v = 5
v = 7
v = 9
i = 4
v = 1
i = 4
i = 4
v = 3
i = 4
i = 4
v = 5
i = 4
i = 4
v = 7
i = 4
i = 4
v = 9
i = 4
v = 1
v = 3
v = 5
v = 7
v = 9
i = 6
v = 1
i = 6
i = 6
v = 3
i = 6
i = 6
v = 5
i = 6
i = 6
v = 7
i = 6
i = 6
v = 9
i = 6
v = 1
v = 3
v = 5
v = 7
v = 9
i = 8
v = 1
i = 8
i = 8
v = 3
i = 8
i = 8
v = 5
i = 8
i = 8
v = 7
i = 8
i = 8
v = 9
i = 8
v = 1
v = 3
v = 5
v = 7
v = 9
i = 10
v = 1
i = 10
i = 10
v = 3
i = 10
i = 10
v = 5
i = 10
i = 10
v = 7
i = 10
i = 10
v = 9
i = 10


##問21

```
(1..10).each do |i|
  (1..10).each do |v|
    puts "i = #{i}"
    puts "v = #{v}"
  end
end
```
i = 1
v = 1
i = 1
v = 2
i = 1
v = 3
i = 1
v = 4
i = 1
v = 5
i = 1
v = 6
i = 1
v = 7
i = 1
v = 8
i = 1
v = 9
i = 1
v = 10
i = 2
v = 1
i = 2
v = 2
i = 2
v = 3
i = 2
v = 4
i = 2
v = 5
i = 2
v = 6
i = 2
v = 7
i = 2
v = 8
i = 2
v = 9
i = 2
v = 10
i = 3
v = 1
i = 3
v = 2
i = 3
v = 3
i = 3
v = 4
i = 3
v = 5
i = 3
v = 6
i = 3
v = 7
i = 3
v = 8
i = 3
v = 9
i = 3
v = 10
i = 4
v = 1
i = 4
v = 2
i = 4
v = 3
i = 4
v = 4
i = 4
v = 5
i = 4
v = 6
i = 4
v = 7
i = 4
v = 8
i = 4
v = 9
i = 4
v = 10
i = 5
v = 1
i = 5
v = 2
i = 5
v = 3
i = 5
v = 4
i = 5
v = 5
i = 5
v = 6
i = 5
v = 7
i = 5
v = 8
i = 5
v = 9
i = 5
v = 10
i = 6
v = 1
i = 6
v = 2
i = 6
v = 3
i = 6
v = 4
i = 6
v = 5
i = 6
v = 6
i = 6
v = 7
i = 6
v = 8
i = 6
v = 9
i = 6
v = 10
i = 7
v = 1
i = 7
v = 2
i = 7
v = 3
i = 7
v = 4
i = 7
v = 5
i = 7
v = 6
i = 7
v = 7
i = 7
v = 8
i = 7
v = 9
i = 7
v = 10
i = 8
v = 1
i = 8
v = 2
i = 8
v = 3
i = 8
v = 4
i = 8
v = 5
i = 8
v = 6
i = 8
v = 7
i = 8
v = 8
i = 8
v = 9
i = 8
v = 10
i = 9
v = 1
i = 9
v = 2
i = 9
v = 3
i = 9
v = 4
i = 9
v = 5
i = 9
v = 6
i = 9
v = 7
i = 9
v = 8
i = 9
v = 9
i = 9
v = 10
i = 10
v = 1
i = 10
v = 2
i = 10
v = 3
i = 10
v = 4
i = 10
v = 5
i = 10
v = 6
i = 10
v = 7
i = 10
v = 8
i = 10
v = 9
i = 10
v = 10

##問22

```
(1..10).each do |i|
  puts "i = #{i}"
  (1..10).each do |v|
    puts "v = #{v}"
  end
end
```

i = 2
v = 1
v = 3
v = 5
v = 7
v = 9
i = 4
v = 1
v = 3
v = 5
v = 7
v = 9
i = 6
v = 1
v = 3
v = 5
v = 7
v = 9
i = 8
v = 1
v = 3
v = 5
v = 7
v = 9
i = 10
v = 1
v = 3
v = 5
v = 7
v = 9

##問23

```
def print_hello(v)
  puts "hello, #{v}"
end

(1..10).each do |i|
  print_hello(i)
end
```

hello, 1
hello, 2
hello, 3
hello, 4
hello, 5
hello, 6
hello, 7
hello, 8
hello, 9
hello, 10

##問24

```
a = 0
b = 0

(1..10).each do
  if a > b
    b = b + 1
    puts "1; a = #{a}, b = #{b}"
  else
    a = a + 1
    puts "2; a = #{a}, b = #{b}"
  end
end
```

2; a = 1, b = 0
1; a = 1, b = 1
2; a = 2, b = 1
1; a = 2, b = 2
2; a = 3, b = 2
1; a = 3, b = 3
2; a = 4, b = 3
1; a = 4, b = 4
2; a = 5, b = 4
1; a = 5, b = 5

##問25

```
def display_price(price)
  case price
  when 100 then
    size = "small"
  when 200 then
    size = "mid"
  when 300 then
    size = "large"
  else
    size = "LL"
  end
  
  print "size is #{size}"
end

display_price(100)
display_price(200)
display_price(300)
display_price(400)
```
size is small
size is mid
size is large
size is LL

##問26

```
def display_price(price)
  size = case price
    when 100 then
      "small"
    when 200 then
      "mid"
    when 300 then
      "large"
    else
      "LL"
   end
  
  print "size is #{size}"
end

display_price(100)
display_price(200)
display_price(300)
display_price(400)
```

size is small
size is mid
size is large
size is LL

##問27

```
def cal_bmi(height, weight)
  if height > 180
    bmi = weight / (height * 0.01) ** 2
    puts "You are tall. This is your bmi: #{bmi}"
  elsif weight > 80
    bmi = weight / (height * 0.01) ** 2
    puts "You are big. This is your bmi: #{bmi}"
  elsif height < 160 && weight < 50
    bmi = weight / (height * 0.01) ** 2
    puts "You are small. This is your bmi: #{bmi}"
  else
    bmi = weight / (height * 0.01) ** 2
    puts "You are normal. This is your bmi: #{bmi}"
  end  
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)
```

You are tall. This is your bmi: 18.587892133
You are big. This is your bmi: 27.7777777778
You are small. This is your bmi: 19.3821446936
You are normal. This is your bmi: 23.7332384004

##問28

```
def cal_bmi(height, weight)
  bmi = weight / (height * 0.01) ** 2
  message = 
    if height > 180
      "You are tall. This is your bmi: #{bmi}"
    elsif weight > 80
      "You are big. This is your bmi: #{bmi}"
    elsif height < 160 && weight < 50
      "You are small. This is your bmi: #{bmi}"
    else
      "You are normal. This is your bmi: #{bmi}"
    end
  puts message
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)
```

You are tall. This is your bmi: 18.587892133
You are big. This is your bmi: 27.7777777778
You are small. This is your bmi: 19.3821446936
You are normal. This is your bmi: 23.7332384004

##問29

```
def cal_bmi(height, weight)
  bmi = weight / (height * 0.01) ** 2
  message = 
    if height > 180
      "tall"
    elsif weight > 80
      "big"
    elsif height < 160 && weight < 50
      "small"
    else
      "normal"
    end
  puts "You are #{message}. This is your bmi: #{bmi}"
end

cal_bmi(187, 65)
cal_bmi(180, 90)
cal_bmi(159, 49)
cal_bmi(159, 60)
```

You are tall. This is your bmi: 18.587892133
You are big. This is your bmi: 27.7777777778
You are small. This is your bmi: 19.3821446936
You are normal. This is your bmi: 23.7332384004

##Don't Repeat Yourself
Rubyには、「繰り返し同じコードを書くな」という<b>DRY原則</b>があります。DRYとは、「Don't Repeat Yourself」の頭文字をとったものです。

例えば、問27〜29の実行結果は同じですが、コードの書き方が異なります。

実行結果の文字列を記述するとき、問27では、<b>"This is your bmi: #{bmi}"という文字列を、tall、big、small、normalの4回分も繰り返している</b>一方、問29では、<b>"You are #{message}. This is your bmi: #{bmi}"の一行</b>で収まっています。

変数に格納されている値が文字列の中に展開されるので、わざわざ重複している部分の表記を繰り返さなくて良いためです。

実際にコードを書くときは、DRY原則を念頭に置いておくのが望ましいそうです。