题目
求解字符串数字组合
import itertools
s = input()
n = int(input())
numStr = ''.join(sorted(set(filter(str.isdigit, s)), reverse = True))
for s in itertools.combinations(numStr, n):
print(''.join(list(s)))
求解字符串函数组合
from itertools import combinations
a=input()
b=[]
e=[]
c=set()
d=""
t=0
for i in a:
if i.isdigit():
b.append(eval(i))
for i in b:
c.add(i)
n=eval(input())
if n<=len(c):
for i in combinations(c,n):
i=list(i)
i.sort(reverse=True)
while t<len(i):
d+=str(i[t])
t+=1
e.append(eval(d))
t=0
d=""
e.sort(reverse=True)
for i in e:
print(i)
黑洞
def isHd(n):
num_str= str(n)
max = ''
min = ''
for i in sorted(num_str,reverse = yes):
max += str(i)
for i in sorted(num_str):
min += str(i)
if int(max) - int(min) == n:
return yes
else:
return no
统计单词数-应用
words = ""
while True:
a = input()
if a == "%%%":
break
a = a.lower()
for i in "!.,:*?#012345689":
a = a.replace(i, ' ')
words = words + " " + a
words = words.split()
s = {}
for i in words:
if i in s:
s[i] += 1
else:
s[i] = 1
s = list(s.items())
s.sort(key = lambda x:x[0])
s.sort(key = lambda x:x[1], reverse = True)
print(len(s))
for i in range(6):
word,count = s[i]
print("{}={}".format(word, count))
集合应用2
a = set(input().split())
b = set(input().split())
c = set(input().split())
d = set(input().split())
t1 = (a | b | c | d)
print("Total: {0}, num: {1}".format(sorted(t1), len(t1)))
t2 = (a | b) & c & d
print("Math and jsj: {0}, num: {1}".format(sorted(t2), len(t2)))
t3 = (c | d) & set([chr(x) for x in range(97, 123)])
print("FeMale in race: {0}, num: {1}".format(sorted(t3), len(t3)))
t4 = ((a | b) & c) - d
print("Only Math: {0}".format(sorted(t4)))
学生类
import sys
class Student:
def __init__(self, name, sex, score):
self.name = name
self.sex = sex
self.score = score
def getName(self):
return self.name
def getSex(self):
return self.sex
def getScore(self):
return self.score
students = []
def makestudent(studentstring):
for i in studentstring:
a, b, c = i.split(',')
students.append(Student(a, b, int(c)))
L = sys.stdin.readlines()
makestudent(L)
maxScore = -1
for i in students:
if i.getScore() > maxScore:
maxName = i.getName()
maxSex = i.getSex()
maxScore = i.getScore()
print("the highest score: {0} {1} {2:.1f}".format(maxName, maxSex, maxScore))
print("no pass: ")
for i in students:
if i.getScore() < 60:
print(i.getName())
计算统计值类 (15分)
在python中statistics模块实现常用的统计公式,允许使用python的各种数值类型(int,float,Decimal和Fraction)来完成高效计算。 本题为了加强同学们对列表、类设计,不采用直接调用statistics模块相应方法来完成输入一组数据的基本统计值统计值。 要求:定义一个类计算统计值类CalStatisticsValue,类中方法有:构造方法(用来初始化成员变量)、计算平均值CauMean、计算标准差CauDev、计算中位数CauMedian;成员变量有:Datas(列表类型,用来存储数据)、Mean(平均值)、Dev(标准差)、Median(中位数)。
class