当前位置:网站首页>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!↓↓↓↓↓↓"""
边栏推荐
- Flink jdbc connector 源码改造sink之 clickhouse多节点轮询写与性能分析
- Example 037: Sorting
- SAP SD module foreground operation
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- Web APIs BOM- 操作浏览器:swiper 插件
- initramfs详解----添加硬盘驱动并访问磁盘
- Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
- VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
- 多渠道打包
- 特征值与特征向量
猜你喜欢
【OpenCV】-重映射
Web APIs BOM - operating browser: swiper plug-in
工程制图平面投影练习
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
即席查询——Presto
字符串的排列
Installation and configuration of nodejs+npm
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
initramfs详解----添加硬盘驱动并访问磁盘
随机推荐
Deng Qinglin, Alibaba Cloud Technical Expert: Best Practices for Disaster Recovery across Availability Zones and Multiple Lives in Different Locations on the Cloud
Use of lombok annotation @RequiredArgsConstructor
C 学生管理系统_添加学生
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
Kubernetes:(九)coredns(浪不动了)
Snake game bug analysis and function expansion
GNSS【0】- 专题
MongoDB数据接入实践
MySQL回表指的是什么
通用的测试用例编写大全(登录测试/web测试等)
网页三维虚拟展厅为接入元宇宙平台做基础
nodejs 安装多版本 版本切换
html select标签赋值数据库查询结果
LDO investigation
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
持续投入商品研发,叮咚买菜赢在了供应链投入上
计算首屏时间
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
- heavy OpenCV 】 【 mapping
Installation and configuration of nodejs+npm