当前位置:网站首页>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!↓↓↓↓↓↓"""
边栏推荐
猜你喜欢
splice随机添加和删除的写法
计算首屏时间
GraphQL背后处理及执行过程是什么
企业虚拟偶像产生了实质性的价值效益
SAP SD module foreground operation
nodejs+express realizes the access to the database mysql and displays the data on the page
特征值与特征向量
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
Example 035: Setting the output color
Web APIs BOM- 操作浏览器:swiper 插件
随机推荐
工程制图平面投影练习
优秀的测试/开发程序员,是怎样修炼的?步步为营地去执行......
工程制图名词解释-重点知识
html select tag assignment database query result
观察者模式
boot issue
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
Summary of GNSS Articles
KunlunBase 1.0 is released!
实例038:矩阵对角线之和
Web APIs BOM- 操作浏览器:swiper 插件
小甲鱼汇编笔记
工程制图复习题
静态/动态代理模式
FeatureNotFound( bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
LDO investigation
Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
nodejs+express realizes the access to the database mysql and displays the data on the page
Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
如何通过API接口从淘宝(或天猫店)复制宝贝到拼多多接口代码对接教程