题目:给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。思路:本题较简单程序:class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [] if numRows <= 0: return result for index1 in range(1, numRows + 1): data = [] if index1 == 1: data.append(1) elif index1 == 2: data.append...
题目:假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 请找出其中最小的元素。 注意数组中可能存在重复的元素。 说明:这道题是 寻找旋转排序数组中的最小值 的延伸题目。允许重复会影响算法的时间复杂度吗?会如何影响,为什么?思路:仍然使用二分法,考虑到会存在重复元素的情况,因此当判断到有重复数字时(nums[middle] == nums[tail])时,使用tail...
题目1:
给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前>后没有空格。 比如: (1) “hello xiao mi”-> “mi xiao hello”输入描述: 输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符) 输出描述: 对于每个测试示例,要求输出句子中单词反转后形成的句子例:输入hello xiao mi输出mi xiao hello‘代码一:测试结果:代码二:需求中要求到多行输入,重新...
要求:随机生成200个12位的序列好序列号由大写字母和数字组成将生成的序列号保存到文件中效果:650) this.width=650;" src="/upload/getfiles/default/2022/11/8/20221108051547341.jpg" title="200.png" />代码:import random
import stringfor num in range(200):
numlist = []
for i in range(12):
numlist.append(random.choice(string.ascii_uppercase+string.digits))
# print(‘‘.join(numlist))
...
学习中,都是从书上找的一些编程小练习。1,编写一个程序,他以球体的半径(浮点数)作为输入,并输出球体的直径、圆周长、表面积、体积。def ball():while True:try:p = 3.14r = float(input("请输入球体半径:"))if r <= 0:print("输入的球体半径不能小于0!")else:print("球体的直径是:%.3f"%(r*2))print("球体的圆周长是:%.3f"%(2*p*r))print("球体的表面积是:%.3f"%(4*p*(r**2)))print("球体的体积是:%.3f"%((4/3)*p*pow(r,3...
# coding=utf-8
import turtle# 画五角星
def drawStar(x):turtle.begin_fill()for i in range(5):turtle.forward(x)turtle.right(144)turtle.end_fill()# 转移位置
def goTo(x, y):turtle.up()turtle.goto(x, y)turtle.down()turtle.setup(864, 576)
turtle.bgcolor("red")
turtle.color("yellow")
turtle.fillcolor("yellow")
turtle.hideturtle()
goTo(-380, 120)
turtle.showturtle()
drawStar(150)for i in range(4):x = 1if ...
1# coding=utf-8 2import urllib3import re4#获取源码 5def gethtml(pg):6 url = ‘https://movie.douban.com/top250?start=%d&filter=‘ % pg7 html = urllib.urlopen(url).read()8return html910#爬取数据
11if__name__ == ‘__main__‘:
12 pat = re.compile(‘<em class="">(.*?)</em>.*?<a href="(.*?)">.*?<img.*?alt="(.*?)" src="(.*?)".*?>‘,re.S)
13for i in range(0,226,25):
14 html = gethtml(i...
今天我们学习了迭代器,其实可以理解为是一个元素容器被遍历的方式,不难理解,看看下面的小例子: 1# 编辑者:闫龙 2#一个简单的迭代器 3 l = [1,2,3,4,5,6,7]#建立一个列表l 4 ite = l.__iter__()#__iter__()代表l是一个可迭代的对象 5print(ite.__next__(),"使用iterator.__next__()方法输出")#代表ite是一个迭代器 6print(next(ite),"使用next(iterator)方法输出")#l两种输出方式均可以 7while True:8try:9print(next(ite))#遍历输...
输入两个整数,如果两个数相减的结果是奇数,输出该结果,如果不是奇数,输出这两个数相减结果是偶数num1 = int(input("请输入第一个数:"))
num2 = int(input("请输入第二个数:"))if (num1 - num2) % 2 != 0:print("两个数相减结果是奇数")
else:print("两个数相减结果是偶数")用for循环输出0到100内所有的奇数i = 0
for i in range(101):if i % 2 == 0:continueprint(i)用while循环输出0到100内所有的偶数j = 0
while j < 101:i...
01:求1+2!+3!+....+20!的和s=0
t=1
for n in range(1,21):t*=ns+=t
print(‘1!+2!+3!+.....+20!=%d‘%s)02:利用递归函数调用方式,将所有输入的5个字符,以相反顺序打印出来def palin(n):next=0if n<=1:next=input()print()print(next)else:next=input()palin(n-1)print(next)
i=5
palin(i)
print()03:给一个不多于5的正整数,要求:一,求它是几位数,二,逆序打印出各位数字num = int(input(‘input a number:‘))a=num//1...
1#!/usr/bin/env python3 2# coding=utf-8 3# Version:python3.6.1 4# Project:practice 5# File:Vehicle.py 6# Data:2020/7/11 15:54 7# Author:LGSP_Harold 8 9"""10编程练习
11自定义一个交通工具类(Vehicle),并根据提示对该类进行进一步封
12装,使其拥有工具类型、速度、体积等属性值。通过自定义实例方法
13实现交通工具的前移、速度设置、获取当前速度、加速行驶、减速行
14驶、实例信息展示、实例类型判别等功能。
1516任...
1.读取文件内容import codecsf=codecs.open(‘1.txt‘,‘rb‘)print (f.read())f.close()2.写入一个新的文件import codecsf=codecs.open(‘2.txt‘,‘wb‘)f.write(‘hello world‘)f.close()3.With的特殊用法import codecswith codecs.open(‘1.txt‘,‘rb‘) as f: print(f.read())print (f.closed)4.Codecs的特殊使用codecs模块中重要的函数之一是lookup,它只有一个参数encoding,指的是编码方式的名称,即utf-8或者gb2312等...
1.执行Python脚本的两种方式(1).交互方式:启动python解释器,执行命令
(2).脚本方式:Python xxx.py 或者 chmod +x && ./xxx.py2.简述位、字节的关系一个二进制位是计算机里最小表示单元。一个字节是计算机里最小存储单元。二进制位=8bits=1Byte=1字节3.简述ascii、unicode、uft-8、gbk的关系美国制定了一套字符编码,对英语字符与二进制位之间的关系做了统一规定,这被称为ASCII码,由1个字节组成,最多支持2**8=256个字符
Unico...
练习内容: 使用Python管理ini文件:实现查询,添加,删除,保存操作。练习目的: 1.掌握文件基本操作 2.认识ini文件 3.了解ConfigParser;ini配置文件格式:节:[session]参数(键=值)[port] port1=3306import ConfigParsercfg=ConfigParser.ConfigParser() vim test.txt[userinfo]name=nyanpwd=password[study]python_base=15python_junior=30linux_base=15cfg.read(‘test.txt‘)cfg.sections()cfg.items f...
运算符:+ - * / // %脚本要求:编写用户登录认证,用户名密码正确则登陆成功,反之则失败并警告重试值user= "patwang"password= "123123"for i in range(3): users = input("You type in the user:") passwd = input("Retype new password password:") if users == user and passwd == password: print("OK") break else: print("you have left%s"%(3-(i+1))) continue 1)设置正确...