当前位置: 首页 > news >正文

递增数组的判断【python实现】

有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么z轴的值是会累增的。同理,逆向考虑,递减就是它的对立面。

下面是查找总结到的所有方式,如有补充可以评论区提出。

资料参考来源: Check if list is strictly increasing

1. zip() and all()

  • Code:
test_list = [1, 4, 5, 7, 8, 10]
# Using zip() and all() to
# Check for strictly increasing list
res = all(i < j for i, j in zip(test_list, test_list[1:]))
print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

2. reduce and lambda

  • Code:
import functoolstest_list = [1, 4, 5, 7, 8, 10]
res = bool((lambda list_demo: functools.reduce(lambda i, j: j ifi < j else 9999, list_demo) != 9999)(test_list))print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

3. itertools.starmap() + zip() + all()

  • Code:
import itertoolstest_list = [1, 4, 5, 7, 8, 10]
res = all(itertools.starmap(operator.le, zip(test_list, test_list[1:])))print(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(n), n是数组的长度。

4. sort() and extend()

  • Code:
test_list = [1, 4, 5, 7, 8, 10]
res = False
new_list = []
new_list.extend(test_list)
test_list.sort()if new_list == test_list:res = Trueprint(f"Is list strictly increasing ? : {res}")
  • Output:
Is list strictly increasing ? : True

时间复杂度: O(nlogn), 这里是sort()的时间复杂度

5. Use stacks

栈是一种后进先出的数据结构(Last in, first out)。

  • Code:
def is_strictly_increasing(lst):stack = []for i in lst:if stack and i <= stack[-1]:return Falsestack.append(i)return Truetest_list = [1, 4, 5, 7, 8, 10]
print(is_strictly_increasing(test_list))  # Truetest_list = [1, 4, 5, 7, 7, 10]
print(is_strictly_increasing(test_list))  # False

时间复杂度: O(n),原数组被遍历了一遍
空间复杂度: O(n),栈可能要存储全部的n个原数组元素

6. numpy()

  • Code:
import numpy as npdef is_increasing(lst):# Converting input list to a numpy arrayarr = np.array(lst)# calculate the difference between adjacent elements of the arraydiff = np.diff(arr)# check if all differences are positive# using the np.all() functionis_increasing = np.all(diff > 0)# return the resultreturn is_increasing# Input list
test_list = [1, 4, 5, 7, 8, 10]# Printing original lists
print("Original list : " + str(test_list))result = is_increasing(test_list)print(result)
# True

时间复杂度: O(n)

7. itertools.pairwise() and all()

这里面就等于使用 pairwise() 替代了之前的 zip(list, list[1:])

  • Code:
from itertools import pairwise# Function
def is_strictly_increasing(my_list):# using pairwise method to iterate through the list and# create pairs of adjacent elements.# all() method checks if all pairs of adjacent elements# satisfy the condition i < j, where i and j# are the two elements in the pair.if all(a < b for a, b in pairwise(my_list)):return Trueelse:return False# Initializing list
test_list = [1, 4, 5, 7, 8, 10]# Printing original lists
print("Original list : " + str(test_list))# Checking for strictly increasing list
# using itertools pairwise() and all() method
res = is_strictly_increasing(test_list)# Printing the result
print("Is list strictly increasing ? : " + str(res))
  • Output:
Original list : [1, 4, 5, 7, 8, 10]
Is list strictly increasing ? : True

时间复杂度: O(n)

http://www.lryc.cn/news/70032.html

相关文章:

  • 在自定义数据上训练 YOLOv8 实例分割
  • 洛谷密钥被破解:加密安全面临新挑战
  • 02 Android开机启动之BootLoader及kernel的启动
  • 代码随想录算法训练营 Day 49 | 121.买卖股票的最佳时机,122.买卖股票的最佳时机 II
  • 精炼计算机网络——数据链路层(一)
  • 猿创征文|Spring系列框架之面向切面编程AOP
  • IoT架构设计
  • EasyRecovery16电脑硬盘数据恢复软件功能讲解
  • 信道通信基础 - 传输介质(双绞线、光纤)
  • 黑马Redis原理篇
  • Sql Server增加字段、修改字段、修改类型、修改默认值
  • 计算机网络第一章(谢希仁第8版学习)
  • Java反射机制
  • New:dbForge Edge 2023 4in1 Enterprise Edition Crack
  • 基于高效率IP路由查找的内容
  • 【VLAN如何划分?6个方法帮你解决!好用又简单!】
  • 网络通信概述 -了解网络编程,什么是ip和端口,url
  • 【Windows线程开发】Windows线程同步技术
  • 【数据结构与算法】- 期末考试
  • 股票?看我用python采集数据制作成交量图表
  • C++中的多态,以及多态的实现、以及实现多态的两个特例。
  • ESP32-C2系列开发板简介
  • AI在狂飙,ChatGPT-4可直接在iPhone上使用啦
  • 【计算机操作系统学习资源汇总】
  • 吴恩达 x OpenAI Prompt Engineering教程中文笔记
  • Everypixel: AI图片搜索引擎
  • IOS新建应用
  • CMake学习笔记:搜索第三方库及头文件路径 find_package()命令
  • Vue3-黑马(一)
  • [组合数学]母函数与递推关系