当前位置:网站首页>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!↓↓↓↓↓↓"""边栏推荐
- v-model
- 持续投入商品研发,叮咚买菜赢在了供应链投入上
- 贴纸拼词 —— 记忆化搜索 / 状压DP
- 简单的线性表的顺序表示实现,以及线性表的链式表示和实现、带头节点的单向链表,C语言简单实现一些基本功能
- Use nodejs switch version (no need to uninstall and reinstall)
- Sticker Spelling - Memory Search / Shape Pressure DP
- C语言力扣第54题之螺旋矩阵。模拟旋转
- 企业虚拟偶像产生了实质性的价值效益
- Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment
- Simple sorting (summer vacation daily question 14)
猜你喜欢

谁说程序员不懂浪漫,表白代码来啦~

HBuilderX的下载安装和创建/运行项目

数组_滑动窗口 | leecode刷题笔记

JS 从零教你手写节流throttle

【虚拟户生态平台】虚拟化平台安装时遇到的坑
一个项目的整体测试流程有哪几个阶段?测试方法有哪些?

快速入门EasyX图形编程

Continuing to invest in product research and development, Dingdong Maicai wins in supply chain investment

Continuing to pour money into commodities research and development, the ding-dong buy vegetables in win into the supply chain
通用的测试用例编写大全(登录测试/web测试等)
随机推荐
Web APIs BOM- 操作浏览器:swiper 插件
OpenCV如何实现Sobel边缘检测
Observability:你所需要知道的关于 Syslog 的一些知识
SAP SD module foreground operation
贴纸拼词 —— 记忆化搜索 / 状压DP
Multithreading JUC Learning Chapter 1 Steps to Create Multithreading
Flask Framework Beginner-06-Add, Delete, Modify and Check the Database
idea中diagram使用
Apache DolphinScheduler actual combat task scheduling platform - a new generation of distributed workflow
FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
一篇文章看懂JS闭包,从执行上下文角度解析有趣的闭包
C程序编译和预定义详解
通用的测试用例编写大全(登录测试/web测试等)
特征值与特征向量
GNSS【0】- 专题
appium软件自动化测试框架
ThreadLocal
工程制图复习题
esp32 releases robot battery voltage to ros2 (micro-ros+CoCube)
【虚拟户生态平台】虚拟化平台安装时遇到的坑