欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python 求解-黑洞

程序员文章站 2022-07-03 19:28:35
题目求解字符串数字组合from itertools import combinationsa=input()b=[]e=[]c=set()d=""t=0for 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)...

题目

求解字符串数字组合

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 CalStatisticsValue:
   
    def _init_(self):
        self.Datas = Data

    def GetData(self):
        d = input()
        self.Datas = list(eval(d))

    def CauMean(self):
        s = 0.0
        for num in self.Datas:
            s += num
        return s / len(self.Datas)
        
    def CauDev(self):
        sdev = 0.0
        for num in self.Datas:
            sdev = sdev + (num - self.CauMean()) ** 2
        return pow(sdev / (len(self.Datas) - 1), 0.5)
    
    def CauMedian(self):
        ls = sorted(self.Datas)
        size = len(ls)
        if size % 2 == 0:
            med = (ls[size // 2 - 1] + ls[size // 2]) / 2
        else:
            med = ls[size // 2]
        return med


求圆面积
import math

radius = float(input())
print("{0:.2f}".format(radius ** 2 * math.pi))


身份证校验_python (10)
中国居民身份证校验码算法如下:

将身份证号码前面的17位数分别乘以不同的系数。从第一位到第十七位的系数分别为:7910584216379105842。 将这17位数字和系数相乘的结果相加。

a = list(input().split())
coefficient = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
lastNum = ['1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2']
sum = 0
for i in range(0,17):
    sum += int(a[i]) * coefficient[i]
sum %= 11
print(lastNum[sum])

学生成绩统计


class CourseScore:
    def __init__(self, Name):
        self.Name = Name
        self.Scorelist = []
        
    def InputScore(self):
        Sc = input().split(',')
        self.Scorelist = list(map(int, Sc))
        
    def GetStatistics(self):
        grade = ['满分', '优秀', '良好', '中等', '及格', '不及格']
        sclist = [0] * 6
        for sc in self.Scorelist:
            if sc < 60:
                sclist[5] += 1
            else:
                sclist[10 - sc // 10] += 1
        return dict(zip(grade, sclist))
    
    def GetAverage(self):
        s = 0.0
        for num in self.Scorelist:
            s = s + float(num)
        return s / len(self.Scorelist)
    
    def Show(self):
        print("课程:{0}".format(self.Name))
        print("共有{0}人参加考试".format(len(self.Scorelist)))
        print("最高分:{0},最低高分:{1}".format(max(self.Scorelist), min(self.Scorelist)))
        print("平均分:{:.2f}".format(self.GetAverage()))
        print(self.GetStatistics())

身份证号判断性别与出生日期

import time

def isVaildDate(date):
    try:
        time.strptime(date, "%Y%m%d")
        return True
    except:
        return False

Id = input()
if len(Id) == 18:
    Date = Id[6:14]
    xb = Id[-2]
elif len(Id) == 15:
    Date = '19' + Id[6:12]
    xb = Id[-1]
if not isVaildDate(Date):
    print("Error,日期不合法")
else:
    if eval(xb) % 2 == 0:
        print('女', end = ',')
    else:
        print('男', end = ',')
    print(Date[:4] + '年' + Date[4:6] + '月' + Date[6:] + '日')

本文地址:https://blog.csdn.net/mju_Hy/article/details/111938405

相关标签: python