yujiroのプログラミング

勉強内容をアウトプットし、サボらないようにする為のブログ

<DAY134>学習忘備録ドリルより

\ Follow me!! /

●7/5(金)
●学習日数 134日
●学習時間(本日)時間
●累計学習時間 696.5時間
●一日あたりの平均学習時間 5.24時間

問題

時間 15分以内

警察がネズミ取りをしています。
速度が60以下の場合、出力は0になります。
速度が61以上80以下の場合、出力は1になります。
速度が81以上の場合、出力は2になります。
出力を計算するためのコードを作成し、
int値としてエンコードします。
0=チケットなし、1 =小チケット、2 =大チケット。
と出力するメソッドを作りましょう。
アタナの誕生日の日だけ速度制限が5倍大きくなります。
速度は小数点以下も有効ですが、判定の際は小数点一桁で四捨五入して整数にしてください。

呼び出し方:
caught_speeding(speed, is_birthday)

出力例:
caught_speeding(60, False) → 0
caught_speeding(65, False) → 1
caught_speeding(65, True) → 0


結果

不正解
時間ないに解けず。

回答

0="チケットなし"
1="小チケット"
2="大チケット"

caught_speeding(speed, is_birthday)

  if is_birthday= birthday 
    if speed.round >= 60*5
        p 0
      elsif speed.round  <= 80*5
        p 1
      else
        p 2
      end
    else
      if speed.round  >= 60
        p 0
      elsif speed.round  <= 80
        p 1
      else
        p 2
      end
    end
  end
end
caught_speeding(60, False)

気づきと学び

数値を変数とし定義し、文字列を格納できない
テキストの答えが納得行かないので、最終的自分で書いたので正解とした

テキスト答え

def caught_speeding(speed ,is_birthday)

  speed_rounded = speed.round(1)

  if speed_rounded <= 60
    point = 0
  elsif speed_rounded <= 80
    point = 1
  else
    point = 2
  end
  puts is_birthday ? point * 5 : point
end

最終的な答え

puts <<~TEXT
0="チケットなし"
1="小チケット"
2="大チケット"
アタナの誕生日の日だけ速度制限が5倍大きくなります。
TEXT

def caught_speeding(speed ,is_birthday)

  my_birthday = 3

  if speed.round <= 60
    point = 0
  elsif speed.round <= 80
    point = 1
  else
    point = 2
  end

 if my_birthday==is_birthday
  puts point*5
 else
  puts point
 end

end

caught_speeding(65,2)