基本問題(5)
[ edit ]
Contents
概要
[ edit ]
曜日に対応する文字列を簡潔に求める
ヒント
この課題で使うPythonの機能 (学習のヒント)
[ edit ]
この課題の解き方 (問題解決のヒント)
[ edit ]
- この課題の解き方 (問題解決のヒント) ....
- 文字列の配列の要素を,添え字で指定すれば,簡潔に記述できます
- 以下のような配列でよさそうですね.
- [ "月","火","水","木","金","土","日" ]
- 以下のような配列でよさそうですね.
- 文字列の配列の要素を,添え字で指定すれば,簡潔に記述できます
実行例
*
プログラム例: 本質的な部分 (授業中に順次公開します)
[ edit ]
- 解答例の核心部分,下記3行です.
# ===== 【関数定義】 0~9の整数を対応する文字列(曜日を意味する)へ変換する =====
def day_of_week_str( dow ):
dow_str = [ "月","火","水","木","金","土","日" ]
return( dow_str[ dow ] )
プログラム例: 配布コード (授業中に順次公開します)
[ edit ]
- 以下の解答例を配布します.
- 本質部分は替えませんが,コメントや,メインプログラム部分の実行例を,後々,分かり易く書き換える可能性はあります.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==============================================================================
# * Copyright (c) 2018 IIJIMA, Tadashi
# * (IIJIMA Laboratory, Dept. of Science and Technology, Keio University).
# ==============================================================================
# ソフトウェア工学[03] 基本課題[03]-(005a) BP_03_005a_day_of_week.py
# BP(Basic Problem) 03-005a: 曜日に対応する文字列を簡潔に求める
# 2018-10-17 飯島 正 (iijima@ae.keio.ac.jp)
# ==============================================================================
# ----- 日付時刻を扱うためのdatetimeモジュールをインポートする -----
import datetime
# ==============================================================================
# ===== 【関数定義】 =====
def today():
return( datetime.date.today() )
# ==============================================================================
# ===== 【関数定義】 =====
def day_of_week_today():
date_of_today = today()
dow = date_of_today.weekday()
return( day_of_week_str(dow) )
# ==============================================================================
# ===== 【関数定義】 =====
def day_of_week( year, month, day ):
dow = datetime.date( year, month, day ).weekday()
return( day_of_week_str(dow) )
# ==============================================================================
# ===== 【関数定義】 0~9の整数を対応する文字列(曜日を意味する)へ変換する =====
def day_of_week_str( dow ):
dow_str = [ "月","火","水","木","金","土","日" ]
return( dow_str[ dow ] )
# ==============================================================================
# ===== 【メイン・プログラム】 =====
# ----- オープニングメッセージ -----
print( "標準モジュールdatetimeを使って,年月日からその日の曜日を求めます: " )
# ----- 実行結果の表示 -----
print( "今日の曜日は", day_of_week_today(), "曜日です. ", sep="" )
# -----
print( "2018年10月 3日の曜日は", day_of_week( 2018, 10, 3 ), "曜日です. ", sep="" )
print( "2018年10月10日の曜日は", day_of_week( 2018, 10, 10 ), "曜日です. ", sep="" )
print( "2018年12月31日の曜日は", day_of_week( 2018, 12, 31 ), "曜日です. ", sep="" )
print( "2019年 1月 1日の曜日は", day_of_week( 2019, 1, 1 ), "曜日です. ", sep="" )
# ==============================================================================