资源经验分享Python 练习100题---No.(81-98)---附其他题目解答链接

Python 练习100题---No.(81-98)---附其他题目解答链接

2019-12-23 | |  99 |   0

原标题:Python 练习100题---No.(81-98)---附其他题目解答链接

原文来自:CSDN      原文链接:https://blog.csdn.net/weixin_41744624/article/details/103646520


github展示python100题
链接如下:
https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises.txt
以下为博主翻译后题目及解答,答案代码分为两个,第一条为博主个人解答(Python3),第二条为题目所提供答案(Python2)
………………………………………………………………………………
题目1-20链接:https://blog.csdn.net/weixin_41744624/article/details/103426225
题目21-40链接:https://blog.csdn.net/weixin_41744624/article/details/103511139
题目41-60链接:https://blog.csdn.net/weixin_41744624/article/details/103575741
题目61-80链接:
https://blog.csdn.net/weixin_41744624/article/details/103607992
本部分为题目81-98,难度1-3不定序~

经检测题库去除重复只有98题啦(欢迎评论添加好题目)~
………………………………………………………………………………
81、问题:
请编写一个程序来压缩和解压缩字符串
“hello world!hello world!hello world!hello world!”.
(使用zlib.compress()和zlib.decompress()压缩和解压缩字符串,Python3记得转换字符存储方式)

import zlib
a='hello world!hello world!hello world!hello world!'
s=a.encode("utf-8")
zlib_s=zlib.compress(s)
print(zlib_s)
zlib_ds=zlib.decompress(zlib_s)
print(zlib_ds)
import zlib
s = 'hello world!hello world!hello world!hello world!'
t = zlib.compress(s)
print tprint zlib.decompress(t)

82、问题:
请编写程序打印“1+1”执行100次的运行时间

import time
start = time.clock()
for i in range(0,100):
    print("1+1")   
end = time.clock()             
print (end-start)
from timeit import Timer
t = Timer("for i in range(100):1+1")
print t.timeit()

83、问题:
请编写一个程序来重新排列并打印列表[3,6,7,8]

import random
ls = [3,6,7,8]
ls2=random.shuffle(ls)
print (ls)1234
from random import shuffle
li = [3,6,7,8]
shuffle(li)print li

84、问题:
请编写一个程序,生成主语在“I”、“You”中,动词在“Play”、“Love”中,宾语在“Hockey”、“Football”中的所有句子。

for i in ("I","You"):
    for j in ("Play","Love"):
        for k in ("Hockey","Football"):
            print(i+j+k)
subjects=["I", "You"]
verbs=["Play", "Love"]
objects=["Hockey","Football"]
for i in range(len(subjects)):
    for j in range(len(verbs)):
        for k in range(len(objects)):
            sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k])
            print sentence

85、问题:
打印[5,6,77,45,22,12,24]中的删除偶数后的列表,请编写程序打印列表。

ls = [5,6,77,45,22,12,24]
ls2 = []
for i in ls:
    if i%2 !=0:
        ls2.append(i)
print (ls2)
li = [5,6,77,45,22,12,24]
li = [x for x in li if x%2!=0]
print li123

86、问题:
使用列表的理解,删除[12,24,35,70,88,120,155]中可被5和7整除的,删除数后,请编写程序打印列表。

ls = [12,24,35,70,88,120,155]
ls2 = []
for i in ls:
    if (i%7 !=0 and i%5 !=0):
        ls2.append(i)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for x in li if x%5!=0 and x%7!=0]
print li

87、问题:
使用列表理解功能,删除[12,24,35,70,88,120,155].中的第0、2、4、6个数字后,请编写程序打印列表。

ls = [12,24,35,70,88,120,155]
ls2 = []
for i in range(0,len(ls)):
    if  i in (0,2,4,6):
        a=ls[i]
        ls2.append(a)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i%2!=0]
print li

88、问题:
通过使用列表理解,请编写一个程序生成一个358的三维数组,其每个元素为0。

num_list=[[[0]*3 for i in range(5)] for i in range (8)]
print (num_list)
array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)]
print array

89、问题:
使用列表理解,请在删除[12,24,35,70,88,120,155]中的第0、4、5个数字后编写程序打印列表。

ls = [12,24,35,70,88,120,155]
ls2 = []
for i  in range(0,len(ls)):
    if  i not in (0,4,5):
        a=ls[i]
        ls2.append(a)
print (ls2)
li = [12,24,35,70,88,120,155]
li = [x for (i,x) in enumerate(li) if i not in (0,4,5)]
print li

90、问题:
使用列表理解,请在删除[12,24,35,24,88,120,155]中的值24后,编写程序打印列表。

ls = [12,24,35,24,88,120,155]ls2 = []for i  in ls:
    if  int(i) == 24:
        pass
    else:
        ls2.append(i)print (ls2)
li = [12,24,35,24,88,120,155]
li = [x for x in li if x!=24]
print li

91、问题:
使用两个给定列表[1,3,6,78,35,55]和[12,24,35,24,88,120,155],编写一个程序来生成一个元素与上述列表相交的列表。

ls = [1,3,6,78,35,55]
ls2 = [12,24,35,24,88,120,155]
ls3 = []
for i  in ls:
    for j in ls2:
        if i==j:
         ls3.append(i)
print (ls3)
set1=set([1,3,6,78,35,55])
set2=set([12,24,35,24,88,120,155])
set1 &= set2
li=list(set1)
print li

92、问题:
对于给定的列表[12,24,35,24,88,120,155,88,120,155],在删除保留原始顺序的所有重复值后,编写程序打印此列表。

ls = [12,24,35,24,88,120,155,88,120,155]
ls2 = set(ls)
ls3 = sorted(ls2,key=ls.index)
print (ls3)
def removeDuplicate( li ):
    newli=[]
    seen = set()
    for item in li:
        if item not in seen:
            seen.add( item )
            newli.append(item)
    return newli
li=[12,24,35,24,88,120,155,88,120,155]
print removeDuplicate(li)

93、问题:
定义一个类人及其两个子类:男性和女性。所有班级都有一个“getGender”方法,可以为男性类打印“男性”,为女性类打印“女性”。

class person():
    def getGender(self):
        pass
class male(person):
    def getGender(self):
        print ("male")
class female(person):
    def getGender(self):
        print ("female")
x=person()     
x.getGender   
a=male()
b=female()
a.getGender()
b.getGender()
class Person(object):
    def getGender( self ):
        return "Unknown"

class Male( Person ):
    def getGender( self ):
        return "Male"

class Female( Person ):
    def getGender( self ):
        return "Female"
aMale = Male()
aFemale= Female()
print aMale.getGender()
print aFemale.getGender()

94、问题:
请编写一个程序,计算并打印控制台输入的字符串中每个字符的个数。

例子:
如果将以下字符串作为程序的输入:
abcdefgabc
那么,程序的输出应该是:
a,2
c,2
b,2
e,1
d,1
g,1
f,1
(大致同22题)

ls = input ("please input:")
digit = []
digitchar = []
alpha = []
for i in ls:
    if i.isalpha():            
        alpha.append(i)
    elif i.isdigit():
        digit.append(i)
    else:
        digitchar.append(i)
x = sorted(set (alpha))
for z in x:
    print (str(z)+":"+str(alpha.count(z)))
dic = {}s=raw_input()for s in s:
    dic[s] = dic.get(s,0)+1
print 'n'.join(['%s,%s' % (k, v) for k, v in dic.items()])

95、问题:
请编写一个从控制台接收字符串并按相反顺序打印的程序

ls = input ("please input:")
ls2 = []n = 0for i in range(0,len(ls)):
    ls2.append(ls[len(ls)-i-1])
print (ls2)
s=raw_input()
s = s[::-1]
print s

96、问题:
请编写一个从控制台接受字符串的程序,并打印具有偶数索引的字符。

例子:
如果将以下字符串作为程序的输入:
H1e2l3l4o5w6o7r8l9d
那么,程序的输出应该是:
Helloworld

ls = input ("please input:")
ls2 = []for i in ls:
    if ls.index(i)%2 == 0:
        ls2.append(i)
print (ls2)
s=raw_input()
s = s[::2]
print s

97、问题:
请编写一个程序,打印[1,2,3]的所有排列组合
(使用itertools.permutations()获取列表的排列)

import itertools
ls =[1,2,3]
ls2=itertools.permutations(ls)
for item in ls2:    
print (item)
import itertools
print list(itertools.permutations([1,2,3]))

98、问题:
编写一个程序来解决一个经典的中国古代难题:
我们在一个农场的鸡和兔子中间数了35头94腿。我们有多少只兔子和多少只鸡?

x=35y=94for i in range(x):
        if (i*4+(x-i)*2 == y):
            print ("鸡:"+str(x-i)+"兔:"+str(i))
def solve(numheads,numlegs):
    ns='No solutions!'
    for i in range(numheads+1):
        j=numheads-i
        if 2*i+4*j==numlegs:
            return i,j
    return ns,ns

numheads=35
numlegs=94
solutions=solve(numheads,numlegs)
print (solutions)


免责声明:本文来自互联网新闻客户端自媒体,不代表本网的观点和立场。

合作及投稿邮箱:E-mail:editor@tusaishared.com


上一篇:Python 练习100题---No.(61-80)---附其他题目解答链接

下一篇:MTSP遗传算法解决

用户评价
全部评价

热门资源

  • Python 爬虫(二)...

    所谓爬虫就是模拟客户端发送网络请求,获取网络响...

  • TensorFlow从1到2...

    原文第四篇中,我们介绍了官方的入门案例MNIST,功...

  • TensorFlow从1到2...

    “回归”这个词,既是Regression算法的名称,也代表...

  • TensorFlow2.0(10...

    前面的博客中我们说过,在加载数据和预处理数据时...

  • 机器学习中的熵、...

    熵 (entropy) 这一词最初来源于热力学。1948年,克...