你必須很努力

Day18 - Codewars 刷題

2019/09/27
字數統計: 218閱讀時間: 1 min

今天整個忙翻
擠出時間刷一題簡單的題目
示範如果對 Ruby 不熟時
可以如何下關鍵字、測試方法是否正確
Codewars LV8 小品一下


題目(Century From Year)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Introduction
The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc.

Task :
Given a year, return the century it is in.

Input , Output Examples ::
centuryFromYear(1705) returns (18)
centuryFromYear(1900) returns (19)
centuryFromYear(1601) returns (17)
centuryFromYear(2000) returns (20)
Hope you enjoy it .. Awaiting for Best Practice Codes

Enjoy Learning !!!

1
2
3
4
5
6
7
8
9
10
11
12
13
def century(year)
# Your solution goes here, warrior
end

describe "Solution" do
it "should return the proper century for each year" do
Test.assert_equals(century(1705), 18, 'Testing for year 1705');
Test.assert_equals(century(1900), 19, 'Testing for year 1900');
Test.assert_equals(century(1601), 17, 'Testing for year 1601');
Test.assert_equals(century(2000), 20, 'Testing for year 2000');
Test.assert_equals(century(89), 1, 'Testing for year 89');
end
end


影片解題: Yes


答案:

1
2
3
4
# Century From Year
def century(year)
year.ceil(-2)/100
end

本文同步發布於 小菜的 Blog https://riverye.com/

原文連結:https://riverye.com/2019/09/27/Day18-Codewars-刷題/

發表日期:2019-09-27

更新日期:2019-10-09

CATALOG