1、给定一个包含n+1个整数的数组nums,其数字在1到n之间(包含1和n),可知至少存在一个重复的整数,假设只有一个重复的整数,请找出这个重复的数
def chongfu(ls):ls.sort()for i in range(0,len(ls)):if ls[i] == ls[i - 1]:print(ls[i])chongfu([1,3,5,7,1,2,4,6])
2、找出10000以内能被5或6整除,但不能被两者同时整除的数(函数)
def zhengchu():ls = []for i in range(0,10001):if (i % 5 == 0 or i % 6 == 0) and i % 30 != 0:ls.append(i)print(ls)zhengchu()
3、写一个方法,计算列表所有偶数下标元素的和(注意返回值)
def xiabiao(ls):x = 0for i in range(0,len(ls)):if ls[i] % 2 == 0:x += iprint(x)xiabiao([0,1,2,3,4,5])
4、根据完整的路径从路径中分离文件路径、文件名及扩展名
import ospath = '/root/user/python/1.py'x , y = os.path.split(path)
i , j = os.path.splitext(y)print(f"dir path:{x}\nfilename:{i}\nextname:{j}")
5、根据标点符号对字符串进行分行
def fenhang(text):lines = text.split(".") lines = [line.strip() + "." for line in lines if line.strip()]return linestext = "Nice to meet you.Nice to meet you too."
lines = fenhang(text)
for line in lines:print(line)
6、去掉字符串数组中每个字符串的空
ls = ['How are you' , 'Nice to meet you' , 'hello world']for i in range(len(ls)):ls[i] = ls[i].replace(' ','')
print(ls)
7、随意输入你心中想到的一个书名,然后输出它的字符串长度。 (len()属性:可以得字符串的长度)
ls = input('please enter a book name:')print(len(ls))
8、两个学员输入各自最喜欢的游戏名称,判断是否一致,如果相等,则输出你们俩喜欢相同的游戏;如果不相同,则输出你们俩喜欢不相同的游戏。
a = input("enter favourite game name:")
b = input("enter favourite game name:")if a == b:print(f"your favourite game is {a}")
else:print("you love different game")
9、上题中两位同学输入 lol和 LOL代表同一游戏,怎么办?
a = input("enter favourite game name:")
b = input("enter favourite game name:")if a.lower() == b.lower():print(f"your favourite game is {a}")
else:print("you love different game")
10、让用户输入一个日期格式如“2008/08/08”,将 输入的日期格式转换为“2008年-8月-8日”。
def sdate():x = input("enter a date(yyyy/mm/dd):")y , m , d = x.split("/")y , m , d = int(y) , int(m) , int(d)print(f"{y}年-{m}月-{d}日")sdate()
11、接收用户输入的字符串,将其中的字符进行排序(升序),并以逆序的顺序输出,“cabed”→"abcde"→“edcba”。
x = input("please enter a string:")
y = ''.join(sorted(x))
z = y[::-1]
print(y,z)
12、接收用户输入的一句英文,将其中的单词以反序输出,“hello c sharp”→“sharp c hello”。
x = str(input("请输入一个字符串:"))
y = x.split(" ")
z = y[::-1]
a = " "
for i in range(len(z)):a += z[i] + " "
print(a)
13、从请求地址中提取出用户名和域名
http://www.163.com?userName=admin&pwd=123456
x = input("请输入网址:")
y = x[:x.index("?")]
z = x[x.index("=") + 1:x.index("&")]
print(f"域名为:{y},用户名为:{z}")
14、让用户输入一句话,找出所有"呵"的位置。
x = input("输入一句话:")
a = []
for i in range(len(x)):if x[i] == "呵":a.append(i)
print(a)
15、让用户输入一句话,找出所有"呵呵"的位置。
x = input("请输入一句话:")
y = 0
while True:y = x.find("呵呵", y)if y == -1:breakprint("呵呵在:", y)y += 2
16、让用户输入一句话,判断这句话中有没有邪恶,如果有邪恶就替换成这种形式然后输出,如:“老牛很邪恶”,输出后变成”老牛很**”;
x = input("输入一句话:")
if "邪恶" in x:y = x.replace("邪恶","**")print(y)
17、如何判断一个字符串是否为另一个字符串的子串
x = input("请输入一个字符串:")
y = input("请输入一个字符串:")if x >= y:if y in x:print(f"{y}是{x}的子串")else:print(f"{y}不是{x}的子串")
else:if x in y:print(f"{x}是{y}的子串")else:print(f"{x}不是{y}的子串")