【python】习题 第10周

简介: 【python】习题 第10周

22秋季Python第10周作业(类与对象)

7-01 图书价格汇总

输入样例:

Java程序设计:34 ;Web程序设计: 56;JSP程序设计:20

输出样例:

Java程序设计:34 
Web程序设计: 56
JSP程序设计:20
总价格为110

代码:

方法一

# 1
sum = 0
num = 0
x = input()
for i in x:
    if i == ';':
        print()
        sum += num
        num = 0
    else:
        print(i, end="")
    if i >= '0' and i <= '9':
        num = num * 10 + ord(i) - ord('0')
print()
sum += num
print("总价格为%d" % sum)

方法二

# 2
str = input().split(';')
jiage = ''
sum = 0
for i in str:
    print(i)
    for y in i:
        if y.isdigit() == True:
            jiage += y
    sum += eval(jiage)
    jiage = ''
print('总价格为{}'.format(sum))

方法三

# 3
import re
class price:
    def __init__(self,project):
        self.project=project
    def bookprice(self):
        ans=[]
        for i in self.project:
            print(i)
            ans.append(int(re.sub('\D','',i)))
        print("总价格为",sum(ans),sep='')
s=input().split(';')
ans=price(s)
ans.bookprice()

7-02 类的定义和使用

输入样例:

2006 3 5

输出样例:

64

(2006年3月5日是该年的第64天)

代码:

方法一

class Date:
    def __init__(self,year,month,day):
        self.year=int(year)
        self.month=int(month)
        self.day=int(day)
        
    def mycheck(self):
        if (self.year%4==0 and self.year%100!=0) or self.year%400==0:
            return True
        return False
    
    def mysolve(self):
        d=[0,31,0,31,30,31,30,31,31,30,31,30,31]
        if self.mycheck():
            d[2]=29
        else:
            d[2]=28
        ans=0
        for i in range(1,self.month):
            ans+=d[i]
        ans+=self.day
        print(ans)
        
year,month,day=input().split()
d=Date(year,month,day)
d.mysolve()

方法二

import calendar
class Date():
    def __init__(self,nian,yue,ri):
        self.nian=nian
        self.yue=yue
        self.ri=ri
    def tianshu(self,nian,yue,ri):
        sum = ri
        for i in range(1,yue):
            a,b = calendar.monthrange(nian,i)
            sum += b
        return(sum)
a,b,c = list(map(int,input().split()))
zhang = Date(a,b,c)
print(zhang.tianshu(a,b,c))

7-03 立方体类的实现

代码:

方法一

a=int(input())
print("{} {}".format(a*a*a,a*a*6))

方法二

class Box:
    def __init__(self,ab):
        self.ab=ab
    def myV(self):
        if self.ab==int(self.ab):
            return int(self.ab**3)
        return self.ab**3
    def myS(self):
        if self.ab==int(self.ab):
            return int((self.ab**2)*6)
        return (self.ab**2)*6
ab=float(input())
a=Box(ab)
print(a.myV(),a.myS(),sep=' ')

7-04 字符串处理

输入样例:

he11ll00o w0or8ld!

输出样例:

!dlrow olleh

代码:

方法一

string = input()
newstring = ''.join([i for i in string if not i.isdigit()])
print(newstring[::-1])

方法二

class str:
    def __init__(self,s):
        self.s=s
    def mydeal(self):
        ans=""
        for i in range(len(s)-1,-1,-1):
            if s[i]<'0' or s[i]>'9':
                ans+=s[i]
        print(ans)
s=input()
a=str(s)
a.mydeal()

7-05 数组元素交换

输入样例:

2 9 0 10

输出样例:

2
9
0
10
10
9
2
0

代码:

方法一

arr = input()
num = [int(n) for n in arr.split()]
for i in num:
    print(i)
num.sort(reverse=True)
for i in num:
    print(i)

方法二

class array:
    def __init__(self,s):
        self.s=s
    def myprint(self):
        for i in self.s:
            print(i)
    def myexchange(self):
        max_index=self.s.index(max(self.s))
        min_index=self.s.index(min(self.s))
        self.s[0],self.s[max_index]=self.s[max_index],self.s[0]
        self.s[-1],self.s[min_index]=self.s[min_index],self.s[-1]
s=list(map(int,input().split()))
a=array(s)
a.myprint()
a.myexchange()
a.myprint()

7-06 查找单价最高和最低的书籍

输入样例:

3 (n=3)
Programming in C
21.5
Programming in VB
18.5
Programming in Delphi
25

输出样例:

highest price: 25.0, Programming in Delphi 
lowest price: 18.5, Programming in VB

代码:

class Book:
    def __init__(self, name, price):
        self.name = name
        self.price = price
    def ans(self):
        max_index = self.price.index(max(self.price))
        min_index = self.price.index(min(self.price))
        print("highest price: ", self.price[max_index], ", ", self.name[max_index], sep='')
        print("lowest price: ", self.price[min_index], ", ", self.name[min_index], sep='')
n = int(input())
Book_name = list()
Book_price = list()
for i in range(n):
    s = input()
    Book_name.append(s)
    p = float(input())
    Book_price.append(p)
a = Book(Book_name, Book_price)
a.ans()

7-07 学生排序

输入样例:

3 (n=3)
1000 85
1001 90
1002 75

输出样例:

1002 75
1000 85
1001 90

代码:

class student:
    def __init__(self, project):
        self.project = project
    def mysort(self):
        s = sorted(self.project.items(), key=lambda d: d[1], reverse=False)
        for key, value in s:
            print(key, value, sep=' ')
n = int(input())
student_project = dict()
for i in range(n):
    id, score = input().split()
    score = int(score)
    student_project[id] = score
a = student(student_project)
a.mysort()

7-08 使用公历类GregorianCalendar

代码:

def isLeap(year):
    if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
        return True
    return False
class GregorianCalendar:
    def __init__(self, day):
        self.day = day
    def setTimeInMillis(self):
        n = self.day
        year = 0
        for i in range(1970, 3000):
            if n < 365 or (n < 366 and isLeap(i)):
                year = i
                break
            if isLeap(i):
                n -= 366
            else:
                n -= 365
        d = [0, 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if isLeap(year):
            d[2] = 29
        else:
            d[2] = 28
        month = 1
        for i in range(1, 13):
            if n >= d[i]:
                n -= d[i]
                month += 1
            else:
                break
        day = max(1, n)
        print(year, month - 1, day, sep='-')
n = int(input())
n //= (1000 * 60 * 60 * 24)
n += 1
a = GregorianCalendar(n)
a.setTimeInMillis()

7-08 2908 算日期

输入样例:

10/01
06/01

输出样例:

136
14

代码:

import calendar
class Date():
    def __init__(self, nian, yue, ri):
        self.nian = nian
        self.yue = yue
        self.ri = ri
    def tianshu(self, nian, yue, ri):
        sum = ri
        for i in range(1, yue):
            a, b = calendar.monthrange(nian, i)
            sum += b
        return (sum)
while True:
    try:
        b, c = list(map(int, input().split("/")))
        day1 = Date(2024, 5, 18)
        day2 = Date(2024, b, c)
        print(abs(day1.tianshu(2024, 5, 18) - day2.tianshu(2024, b, c)))
    except:
        break

7-09 计算年龄

输入样例:

在这里给出一组输入。例如:

1995
12
23

输出样例:

在这里给出相应的输出。例如:

age=22

代码:

方法一

year = int(input())
month = int(input())
day = int(input())
ans = 0
if month == 12 and day >= 25:
    ans = 2017 - year + 1
else:
    ans = 2017 - year
print("age={}".format(ans))

方法二

class Birthday:
    def __init__(self,year,month,day):
        self.year=year
        self.month=month
        self.day=day
    def getAge(self):
        age=2017-self.year
        if self.month==12 and self.day>=25:
            age+=1
        print("age=",age,sep='')
year=int(input())
month=int(input())
day=int(input())
a=Birthday(year,month,day)
a.getAge()

7-10 类的定义与对象使用

输入样例:

3
20174042001 zhangsan 20
20174042030 lisi 2
20174042050 wangwu 17

输出样例:

zhangsan 20174042001 20
bad
wangwu 20174042050 17

代码:

方法一

n = int(input())
for i in range(0, n):
    a, b, c = input().split()
    c = int(c)
    # print(a,b,c)
    if 7 <= c <= 60:
        print(b, a, c)
    else:
        print("bad")

方法二

class Stu:
    def __init__(self,num,ID,name,age):
        self.num=num
        self.id=ID
        self.name=name
        self.age=age
    def myprint(self):
        for i in range(self.num):
            if 7<=self.age[i]<=60:
                print(self.name[i],self.id[i],self.age[i],sep=' ')
            else:
                print("bad")
n=int(input())
Stu_ID=[]
Stu_name=[]
Stu_age=[]
for i in range(n):
    ID,name,age=input().split()
    Stu_ID.append(ID)
    Stu_name.append(name)
    Stu_age.append(int(age))
a=Stu(n,Stu_ID,Stu_name,Stu_age)
a.myprint()

7-11 sdust-Java-学生成绩读取与排序

输入样例:

小明,2001,Java,88
小刚,2002,Java,78
小丁,2003,Java,56
小宏,2004,Java,85
小明,2001,Python,84
小刚,2002,Python,98
小丁,2003,JavaWeb,66
小宏,2004,Algorithm,87
exit

输出样例:

No1:2002,小刚
No2:2001,小明
No3:2004,小宏
No4:2003,小丁

代码:

class Student:
    def __init__(self, name, ID, score):
        self.name = name
        self.ID = ID
        self.score = score
    def Score_sort(self):
        for i in range(1, len(self.name) + 1):
            max_index = self.score.index(max(self.score))
            print("No", i, ":", self.ID[max_index], ",", self.name[max_index], sep='')
            del self.score[max_index]
            del self.ID[max_index]
            del self.name[max_index]
Student_name = []
Student_id = []
Student_score = []
while True:
    s = input()
    if s == "exit":
        break
    s = s.split(',')
    if Student_name.count(s[0]) == 0:
        Student_name.append(s[0])
        Student_id.append(s[1])
        Student_score.append(int(s[3]))
    else:
        index = Student_name.index(s[0])
        Student_score[index] += int(s[3])
        Student_score[index] /= 2
ans = Student(Student_name, Student_id, Student_score)
ans.Score_sort()

7-12 课程设计排名统计

输入样例:

5
xiaozhao zhangwuji zhaomin zhouzhiruo 2 3 1 5 4
huangrong guojing guofu guoxiang 2 3 5 1 4
yangguo xiaolongyu limochou laowantong 3 2 1 4 5
yangkang monianci ouyangxiu zhoubotong 2 3 1 5 4
yuanchengzhi qingqing wenyi gongzhu 3 2 1 4 5

输出样例:

1 huangrong guojing guofu guoxiang
2 yangguo xiaolongyu limochou laowantong
3 xiaozhao zhangwuji zhaomin zhouzhiruo
4 yuanchengzhi qingqing wenyi gongzhu
5 yangkang monianci ouyangxiu zhoubotong

代码:

class score:
    def __init__(self, n, d, name):
        self.n = n
        self.d = d
        self.name = name
    def Group_sort(self):
        ans = dict()
        for i in range(self.n):
            ans[self.name[i]] = self.d[i + 1]
        ans = dict(sorted(ans.items(), key=lambda d: d[1], reverse=False))
        count = 1
        for key, value in ans.items():
            print(count, key, sep='')
            count += 1
n = int(input())
d = dict()
for i in range(n):
    d[i + 1] = 0
name = list()
for i in range(n):
    s = input().split()
    ss = ""
    for j in range(4):
        ss += ' '
        ss += s[0]
        del s[0]
    name.append(ss)
    for k in range(n):
        d[int(s[k])] += (k + 1)
ans = score(n, d, name)
ans.Group_sort()

7-13 领装备

输入样例:

在这里给出一组输入。例如:

4
10120150912233 2 4
10120150912119 4 1
10120150912126 1 3
10120150912002 3 2
2
3 4

输出样例:

在这里给出相应的输出。例如:

10120150912002 2
10120150912119 1

代码:

class game:
    def __init__(self, d, s):
        self.d = d
        self.s = s
    def gift(self):
        for i in self.s:
            print(self.d[i])
d = dict()
n = int(input())
for i in range(n):
    s = input().split()  # 玩家号 号码牌 装备星级
    d[s[1]] = (s[0] + " " + s[2])
m = int(input())
s = input().split()
ans = game(d, s)
ans.gift()

7-14 宿舍谁最高?

输入样例:

7
000000 Tom 175 120
000001 Jack 180 130
000001 Hale 160 140
000000 Marry 160 120
000000 Jerry 165 110
000003 ETAF 183 145
000001 Mickey 170 115

输出样例:

000000 Tom 175 120
000001 Jack 180 130
000003 ETAF 183 145

代码:

class Student:
    def __init__(self, d, ID, name, height, weight):
        self.d = d
        self.ID = ID
        self.name = name
        self.height = height
        self.weight = weight
    def home_sort(self):
        for (key, value) in self.d.items():
            max_height = 0
            max_index = 0
            cnt = self.ID.count(key)
            index = -1
            while cnt > 0:
                cnt -= 1
                index = self.ID.index(key, index + 1)
                if self.height[index] > max_height:
                    max_height = self.height[index]
                    max_index = index
            if len(self.ID[max_index]) < 6:
                self.ID[max_index] = self.ID[max_index].zfill(6)
            print(self.ID[max_index], self.name[max_index], self.height[max_index], self.weight[max_index], sep=' ')
n = int(input())
d = dict()
Student_ID = []
Student_name = []
Student_height = []
Student_weight = []
for i in range(n):
    s = input().split()
    Student_ID.append(s[0])
    if d.get(s[0], -1) == -1:
        d[s[0]] = 1
    Student_name.append(s[1])
    Student_height.append(int(s[2]))
    Student_weight.append(int(s[3]))
d = dict(sorted(d.items(), key=lambda d: d[0], reverse=False))
ans = Student(d, Student_ID, Student_name, Student_height, Student_weight)
ans.home_sort()


相关文章
|
17天前
|
物联网 Python
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
|
22天前
|
存储 Python
【python】习题第10周题解
【python】习题第10周题解
|
22天前
|
Python
【python】习题第9周
【python】习题第9周
|
22天前
|
数据安全/隐私保护 Python
【python】习题第8周
【python】习题第8周
|
22天前
|
Python
【python】习题第7周(下)
【python】习题第7周(下)
|
22天前
|
自然语言处理 Python
【python】习题第7周(上)
【python】习题第7周(上)
|
22天前
|
Python
【python】习题 6-10周(下)
【python】习题 6-10周(下)
|
22天前
|
自然语言处理 数据安全/隐私保护 Python
【python】习题 6-10周(中)
【python】习题 6-10周(中)
|
22天前
|
Python
【python】习题 6-10周(上)
【python】习题 6-10周(上)
|
22天前
|
Python
【python】习题 1-5周(下)
【python】习题 1-5周(下)