0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python3で時間のゾロ目判定

Last updated at Posted at 2018-11-15

はじめに

コーディングテストしたらあまりに出来なくて悲しかった…
コーディングテストの練習の備忘録として…

追記
本記事のコメントに3行で実装できるコードも載っております.
初心者(私)と賢い方のコードの違いが見れて面白いですよ!!

問題

「時 分 秒」(h m s)を入力とし,
全ての数字が同じなら「Yes」,一つでも違うなら「No」と出力する.

条件

python3

0 ≦ h < 24 , 0 ≦ m < 60 , 0 ≦ s < 60

入出力例

入力「11 1 11」
出力「Yes」

入力「12 11 1」
出力「No」

コード

def main():
    # 「h m s」をinputし半角スペースでsplit
    time = input().split()

    # h,m,sが条件であることの確認用に格納
    h = int(time[0])
    m = int(time[1])
    s = int(time[2])

    # 16時なら['1', '6']のように分けて格納.今の段階では文字列(str型)
    hList = list(time[0])
    mList = list(time[1])
    sList = list(time[2])

    # hList,mList,sListを繋げた配列の各要素をint型にして格納
    hmsList = list(map(int, hList + mList + sList))

    # h,m,sが条件であることの確認
    if (0 <= h < 24 and 0 <= m < 60 and 0 <= s < 60):
        pass    # 大丈夫なら以下を行う
    else:
        return  # 大丈夫でないならreturn

    # mdList内が全て同一ならばTrue,一つでも違うならFalseを格納
    # hmsListの先頭([0])が全てと同一かどうかをチェック
    judge = all((x == hmsList[0] for x in hmsList))

    if (judge == True):
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

最後に

プログラミング初心者のへぼコードです.
ミスがあっても知ったこっちゃありません.

0
1
3

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?