Skip to content

7 efficient ways to check if a list/array is monotonic in Python (non-strict & strict). Includes edge cases, performance comparison, floating-point handling, interview tips for LeetCode 896, and real-world examples. O(n) solutions + NumPy/pairwise variants.

License

Notifications You must be signed in to change notification settings

Emmimal/monotonic-sequence-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

monotonic-sequence-python

7 efficient ways to check if a list/array is monotonic in Python (non-strict & strict). Includes edge cases, performance comparison, floating-point handling, interview tips for LeetCode 896, and real-world examples. O(n) solutions + NumPy/pairwise variants.

Monotonic Sequence Check in Python

Python License: MIT

7 efficient methods to check if a list/array is monotonic (non-decreasing or non-increasing) in Python — including edge cases, floating-point safety, performance analysis, and interview tips for LeetCode 896 (Monotonic Array).

Perfect for:

  • Data validation pipelines
  • Time-series analysis
  • Machine learning loss monitoring
  • Technical interview preparation

→ Original in-depth article: https://emitechlogic.com/monotonic-sequence-in-python/

Features

  • All methods are O(n) time (except naive sorted version)
  • Handles strict vs non-strict monotonicity
  • Floating-point tolerance with math.isclose
  • Early exit optimizations
  • NumPy vectorized version for large arrays
  • itertools.pairwise (Python 3.10+) — clean & zero-copy

Quick Start

git clone https://github.com/Emmimal/monotonic-sequence-python.git
cd python-monotonic-check
pip install -r requirements.txt          # only if using NumPy
python -m pytest tests/                   # run tests

About

7 efficient ways to check if a list/array is monotonic in Python (non-strict & strict). Includes edge cases, performance comparison, floating-point handling, interview tips for LeetCode 896, and real-world examples. O(n) solutions + NumPy/pairwise variants.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages