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

icp备案网站负责人赢展网站建设

icp备案网站负责人,赢展网站建设,南京网站网站建设公司,WordPress来必力有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么z轴的值是会累增的。同理,逆向考虑,递减就是它的对立面。 下面是查找总结到…

有时候需要对某一组数组的数据进行判断是否 递增 的场景,比如我在开发一些体育动作场景下,某些肢体动作是需要持续朝着垂直方向向上变化,那么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.lakalapos1.cn/news/178/

相关文章:

  • 来宾住房和城乡建设网站做网站用什么虚拟服务器
  • 你学做网站学了多久山西项目网
  • 湛江网站建设方案推广前程无忧官网
  • 苏州集团网站制作公司免费个人微网站模板
  • 虚拟网站多少钱黄岛外贸网站建设
  • 门户网站建设需求1 建设网站目的
  • 海口市住房与城乡建设厅网站软件开发过程模型
  • 网站标题收录一级a做爰片付费网站
  • 网站建设进度安排企业贷款政策最新消息2022
  • 企业网站 设网站改版需要怎么做
  • 阿里巴巴网站首页怎么制作网站建设中建站广告html单页面
  • 深圳网站建设如何制作运动健身型网站开发
  • 网站快速排名技巧给网站做视频怎么赚钱
  • 建设电影网站论文网站项目在哪接
  • wordpress站群是什么网站报价明细
  • 企业网站小程序源码别墅设计 网站模板
  • 要接入广告做啥网站做网站公司的商标需要注册吗
  • 静态网站分页wordpress 时间不对
  • 做网站认证对网站有什么好处wordpress应用市场主体
  • 中国建设网官方网站企业网银wordpress手机版受钱吗
  • 我要学习做网站怎么办网站
  • 商城网站建设所必备的四大功能是哪些wordpress调取页脚文件
  • 深圳市网站建设哪家好wordpress 增加页面
  • 兰州网站建设开发百度指数行业排行
  • 怎么做卖辅助网站如何制作网址
  • 网站建设的经费预算报告网站建设好卖吗
  • 做淘客的网站名称网站建设与管理专业就业
  • 旅游网站制作代码信息分类网站好建吗
  • 有什么可以在线做数学题的网站江苏省企业年报网上申报入口
  • 在西安市建设工程交易中心网站上江海区建设局网站