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 1 year has passed since last update.

【Python】単調配列

Posted at

作るもの

与えられた配列が単調な増加または単調な減少を持つかどうかを判定するための関数。

実装

main.py
def isMonotonic(array):
    c = True  # 単調増加を示すフラグ
    d = True  # 単調減少を示すフラグ
    for i in range(len(array) - 1):
        # 配列内の要素を順番に比較し、単調増加かどうかを判定
        c = c and array[i] <= array[i + 1]
        # 配列内の要素を順番に比較し、単調減少かどうかを判定
        d = d and array[i] >= array[i + 1]
    # 単調増加または単調減少のいずれかが成り立つ場合、単調であると判断
    return c or d

# テスト
if __name__ == "__main__":
    array1 = [-1, -5, -10, -1100, -1100, -1101]
    array2 = [-1, -5, 10, -1100, -1100, -1101]
    print(isMonotonic(array1))
    print(isMonotonic(array2))

参考

0
1
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
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?