当前位置:网站首页>Example 039: Inserting elements into an ordered list
Example 039: Inserting elements into an ordered list
2022-08-04 01:42:00 【lazily】
Subject: There is a sorted array.Now enter a number and ask it to be inserted into the array according to the original law.
Program analysis: First judge whether the number is greater than the last number, and then consider the case of inserting the middle number. After the insertion, the number after this element is moved back one position in turn.
Code:
Method one:
lis = [1, 10, 100, 1000, 10000, 100000]n = int(input('insert a number: '))lis.append(n)for i in range(len(lis) - 1):if lis[i] >= n:for j in range(i, len(lis)):lis[j], lis[-1] = lis[-1], lis[j]breakprint(lis)Method Two:
lis = [1, 10, 100, 1000, 10000, 100000]n = int(input('insert a number: '))lis.append(n)print(sorted(lis))Results:

"""Problems encountered during study and no one answered?The editor has created a QQ group for Python learning and communication, and you can enter the officially recommended group by scanning the code below.Looking for like-minded friends to help each other, there are also good video learning tutorials and PDF e-books in the group!↓↓↓↓↓↓"""边栏推荐
- FeatureNotFound( bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requested:
- SAP SD module foreground operation
- 即席查询——Presto
- 阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
- splice随机添加和删除的写法
- 【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中
- appium软件自动化测试框架
- Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
猜你喜欢
随机推荐
工程制图复习题
实例040:逆序列表
The idea of the diagram
nodejs+express实现数据库mysql的访问,并展示数据到页面上
持续投入商品研发,叮咚买菜赢在了供应链投入上
观察者模式
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
Example 040: Reverse List
GNSS【0】- 专题
2022 中国算力大会发布“创新先锋”优秀成果
小甲鱼汇编笔记
简单的线性表的顺序表示实现,以及线性表的链式表示和实现、带头节点的单向链表,C语言简单实现一些基本功能
2022 China Computing Power Conference released the excellent results of "Innovation Pioneer"
【正则表达式】笔记
this巩固训练,从两道执行题加深理解闭包与箭头函数中的this
C语言力扣第54题之螺旋矩阵。模拟旋转
实例039:有序列表插入元素
ASP.NET 获取数据库的数据并写入到excel表格中
持续投入商品研发,叮咚买菜赢在了供应链投入上








