0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codewars 7 kyu The Coupon Code

0
Posted at

7 kyu The Coupon Code

Task

Task
Your mission:
Write a function called checkCoupon which verifies that a coupon code is valid and not expired.
A coupon is no more valid on the day AFTER the expiration date. All dates will be passed as strings in this format: "MONTH DATE, YEAR"

Verbalization

Step1 クーポンコードの確認
1. entered_codeと correct_codeが同じだったら、Trueを返す
2.entered_codeとcorrect_codeが違っていたらFalseを返す

Step2 日付の確認
1.current_dateがexpiration_dateより前または同じだったら、Trueを返す
2.current_dateがexpiration_dateより後だったら、Falseをかえす。

Code

from datetime import datetime

def check_coupon(entered_code, correct_code, current_date, expiration_date):
    if entered_code != correct_code:
        return False
    
    current = datetime.strptime(current_date, "%B %d, %Y")
    expire = datetime.strptime(expiration_date, "%B %d, %Y")
    
    return current <= expire

Reference

datetime:日付オブジェクトに変換

current = datetime.strptime(current_date, "%B %d, %Y")
expire = datetime.strptime(expiration_date, "%B %d, %Y")

strptime とは?
strptime は "string parse time"(文字列を日付へ変換)
日付のフォーマットを指定しないと変換できない
%B → 月名(January, February, ...)
%d → 日
%Y → 年(4桁)

"June 15, 2020" → datetime(2020, 6, 15)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?