Python基础100题打卡Day23

题目九十五

给出你的大学体育日参与者的分数表,你需要找到亚军的分数。给你分数。
把它们保存在一个列表中,找出亚军的得分。
例子:输入:
5 2 3 6 6 5
输出为:
5

提示:
使分数唯一,然后找到第二好的数字。

代码实现

方法一:

score = int(input("请输入分数:"))
l_score = []
while True:
    l_score.append(score)
    score = int(input("请输入另一个分数:"))
    if score == 0:
        break

l_score1 = list(set(l_score))
l_score2 = sorted(l_score1)
print(l_score2)
print("季军分数是:",l_score2[-2])

方法二:

arr = map(int, input("请输入分数:").split())
arr = list(set(arr))
arr.sort()
print(arr[-2])

运行结果

请输入分数:5 2 3 6 6 5
季军分数是: 5

题目九十六

你将得到一个字符串S和宽度W。任务是将字符串包装成一个宽度段。
如果给出以下字符串作为程序的输入:
ABCDEFGHIJKLIMNOQRSTUVWXYZ 4
输出:
ABCD EFGH IJKL IMNO QRST UVWX YZ

代码实现

方法一:

def wrap(lst_string, maxwith):
    a = len(lst_string) // maxwith
    b = len(lst_string) % maxwith
    i = 1
    while i <= a:
        for j in range((i-1)*maxwith,i*maxwith):
            print(lst_string[j],end="")
        print("\n",end="")
        i +=1

    for i in range(len(lst_string)-b,len(lst_string)):
        print(lst_string[i],end="")


lst, maxwidth = input("请输入字符:"), int(input("请输入字符串宽度:"))
wrap(lst, maxwidth)

方法二:

import textwrap


def wrap(string, max_width):
    string = textwrap.wrap(string, max_width)
    string = "\n".join(string)
    return string


if __name__ == "__main__":
    string, max_width = input(), int(input())
    result = wrap(string, max_width)
    print(result)

方法三:

import textwrap

string = input()
width = int(input())

print(textwrap.fill(string, width))

运行结果

ABCDEFGHIJKLIMNOQRSTUVWXYZ
4
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ

题目九十七

给你一个整数,你的任务是打印一个大小为N的字母表。(Rangoli是一种基于图案创作的印度民间艺术。)
不同大小的字母表如下:
size=3
----c----

–c-b-c–

c-b-a-b-c

–c-b-c–

----c----

size=5
--------e--------

------e-d-e------

----e-d-c-d-e----

–e-d-c-b-c-d-e–

e-d-c-b-a-b-c-d-e

–e-d-c-b-c-d-e–

----e-d-c-d-e----

------e-d-e------

--------e--------
提示:
首先以给定的方式打印出Rangoli的一半,并将每一行保存在一个列表中。
然后按反向顺序打印列表,以得到其余的列表。

代码实现

import string


def print_rangoli(size):
    n = size
    alph = string.ascii_lowercase
    width = 4 * n - 3

    ans = []
    for i in range(n):
        left = "-".join(alph[n - i - 1 : n])
        mid = left[-1:0:-1] + left
        final = mid.center(width, "-")
        ans.append(final)

    if len(ans) > 1:
        for i in ans[n - 2 :: -1]:
            ans.append(i)
    ans = "\n".join(ans)
    print(ans)


if __name__ == "__main__":
    n = int(input("请输入数字:"))
    print_rangoli(n)

运行结果

请输入数字:5
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------

题目九十八

给你个日期。你的任务是找出那天是周几。
输入:
包含空格的单行输入分别以MM DD YYYY格式分隔月份、日和年。
输出:
WEDNESDAY

提示:
使用日历模块的工作日功能

代码实现

import calendar

month, day, year = map(int, input("请按照:MM DD YYYY格式输入日期:").split())

dayId = calendar.weekday(year, month, day)
print(calendar.day_name[dayId].upper())

运行结果

请按照:MM DD YYYY格式输入日期:06 20 2021
SUNDAY

题目九十九

给定2组整数,M和N,按升序打印它们的对称差分。
对称差一词表示那些存在于M或N中但两者都不存在的值。

输入:
A:4 2 4 5 9
B:4 2 4 11 12
输出:
5 9 11 12

代码实现

方法一:

n = int(input())
set1 = set(map(int, input().split()))

m = int(input())
set2 = set(map(int, input().split()))

ans = list(set1 ^ set2)
ans.sort()
for i in ans:
    print(i)

方法二:

A_lst = list(map(int , input("请输入第一个数列").split()))
B_lst = list(map(int , input("请输入第二个数列").split()))
C_lst = []
for i in A_lst:
    if i not in B_lst:
        C_lst.append(i)

for i in B_lst:
    if i not in A_lst:
        C_lst.append(i)

print(C_lst)

运行结果

请输入第一个数列4 2 4 5 9
请输入第二个数列4 2 4 11 12
[5, 9, 11, 12]
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值