python经典编程题-Python入门经典编程题2

1、从键盘输入整数n(1-9之间),对于1-100之间的整数删除包括n并且能被n整除的数,例如如何n为6,则要删除包含6的如6,16这样的数及时6的倍数如12、18这样的数,输出所有满足条件的数,要求每满10个数换行。

如:输入6

输入数字:6

结果:

1,2,3,4,5,7,8,9,10,11

13,14,15,17,19,20,21,22,23,25

27,28,29,31,32,33,34,35,37,38

39,40,41,43,44,45,47,49,50,51

52,53,55,57,58,59,70,71,73,74

75,77,79,80,81,82,83,85,87,88

89,91,92,93,94,95,97,98,99,100

代码实现如下n=int(input("输入数字:"))

count=0

new_str=''

print("结果:")

for i in range(101):

s=str(i)

if i % n!=0 and s.find(str(n))==-1:

new_str=new_str + s + ','

count+=1

if count % 10==0:

print(new_str[:-1])

new_str=''

if len(new_str)>0:

print(new_str[-1])

2、随机函数产生500行1-100之间的随机整数存入文件random.txt中,编程寻找这些整数的众数并输出。import random

with open('random.txt','w+') as fp:

for i in range(500):

fp.write(str(random.randint(1,100)))

fp.write(' ')

fp.seek(0)

nums=fp.readlines()

nums=[num.strip() for num in nums]

setNums=set(nums)

lst = [0]*101

for num in setNums:

c=nums.count(num)

lst[int(num)]=c

for i in range(len(lst)):

if lst[i] == max(lst):

print(i)

3、在文件article.txt中存放一篇英文文章,假设文章中的标点符号仅包括",”、".”、"!”、"?”和"...”,通过Python找出最长的单词并输出。with open('article.txt') as fp:

data=fp.read()

words = data.split()

lst = []

for word in words:

if word[-3:] =='...':

word=word[:-3]

lst.append(word)

if word[-1] in ',.?!':

word=word[:-1]

lst.append(word)

result =sorted(lst,key=len,reverse=True)

print(result[0])

m=len(result[0])

for word in result[1:]:

n=len(word)

if n==m:

print(word)

else:

break

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值