当前位置:首页 » 《随便一记》 » 正文

Python_8_小心星的博客

27 人参与  2022年05月02日 14:33  分类 : 《随便一记》  评论

点击全文阅读


Python_8

  • 写在前面
  • 7-1 jmu-Java&Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出 (10 分)
  • 7-2 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (25 分)
  • 7-3 获奖 (10 分)
  • 7-4 字典的应用-找出出现次数最多的字符串(高教社,《Python编程基础及应用》习题7-6) (3 分)
  • 7-5 sdut-查验身份证 (10 分)
  • 7-6 sdut-查字典 (10 分)
  • 7-7 词频统计 - 实验11 字典操作及应用 - 《Python编程实验》 (10 分)
  • 7-8 使用for循环和字典功能,统计字符出现在一个英文句子中的次数 (5 分)
  • 7-9 字典查询1--查询省会 (30 分)
  • 7-10 字典应用--用户登录 (30 分)
  • 写在最后

写在前面

其实发布Python的博客,刚开始是仅供宿舍内部使用 但是有很多人也看到了本人的博客,来完成作业
本来我自己写了就行了,不用发布在博客里 但是想来发布在博客里,自己可以获取粉丝,收获一些赞 我就想收货一些赞而已
我自己是 可以看到粉丝通过了什么方式来关注我的
但是就有那么些人,复制作业完成,写完了,就直接取关
对于这些**(自行体会),我只想说gun吧

本身写作业,并且还写文章就很浪费我的时间和精力
每个人的时间都应该是用在自己的身上
你们只需Ctrl+C和Ctrl+V一下就完成了作业,给你们节省了写作业的时间
节省下的时间,可以自己打发

而我也能学习其他课程,做其他事
我付出了时间和精力,又收到了什么,而你们呢? 连赞都不想点,这公平吗? 世界上哪有那么好的事
但是没人点赞,所以没办法,只能开启付费了
这篇文章本来应该是付费的
都已经弄好了付费资源了

Python_8.md


但是感觉太突兀了
所以就又发了这篇文章
具体再看后面

这世上本没有白嫖,白嫖的人多了,也就有了白嫖。
任何只想白嫖的人都不可能真正拿到结果。

7-1 jmu-Java&Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出 (10 分)


7-1 jmu-Java&Python-统计一段文字中的单词个数并按单词的字母顺序排序后输出 (10)
现需要统计若干段文字(英文)中的不同单词数量。
如果不同的单词数量不超过10个,则将所有单词输出(按字母顺序),否则输出前10个单词。

注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。
注3:单词大小写敏感,即'word''WORD'是两个不同的单词 。

输入说明
若干行英文,最后以!!!!!为结束。

输出说明
不同单词数量。 然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。

输入样例
Failure is probably the fortification in your pole
It is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta
when you Are wondering whether new money it has laid
background Because of you, then at the heart of the
most lax alert and most low awareness and left it
godsend failed
!!!!!
输出样例
49
Are
Because
Failure
It
a
alert
and
are
as
at

仅供参考

inputThings = []
a = input()
while a != '!!!!!':
    inputThings += [' ']
    inputThings += a
    a = input()
wordCount = []
inputThingsCount = len(inputThings)
inputThingsCountMinusOne = int(inputThingsCount) - 1
wordStartSpace = 0
wordEndPlace = 0
inputThings.insert(0, " ")
inputThings.insert(len(inputThings), " ")
flag = 0
for whichLetter in range(len(inputThings)):
    if inputThings[whichLetter - 1] == ' ' and inputThings[whichLetter] != ' ':
        flag += 1
        wordStartSpace = whichLetter
    if inputThings[whichLetter] != ' ' and inputThings[whichLetter + 1] == ' ':
        flag += 1
        wordEndPlace = whichLetter
    if flag == 2:
        sub = inputThings[wordStartSpace:wordEndPlace + 1]
        sub = "".join(sub)
        flag = 0
        if sub not in wordCount:
            wordCount += [sub]
print(len(wordCount))
wordCount.sort() 
if len(wordCount) > 10:
    for whichOne in range(10):
        print(wordCount[whichOne])
else:
    for whichOne in range(len(wordCount)):
        print(wordCount[whichOne])

7-2 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (25 分)

7-2 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (25)
现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数。

注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。

基本版:
统计时,区分字母大小写,且不删除指定标点符号。

进阶版:

统计前,需要从文字中删除指定标点符号!.,:*?。 注意:所谓的删除,就是用1个空格替换掉相应字符。
统计单词时需要忽略单词的大小写。
输入说明
若干行英文,最后以!!!!!为结束。

输出说明
单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例1
failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta

when you are wondering whether new money it has laid
background because of you then at the heart of the

most lax alert and most low awareness and left it

godsend failed
!!!!!
输出样例1
46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2
结尾无空行
输入样例2
Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!!!
结尾无空行
输出样例2
54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2
结尾无空行

仅供参考

def HandleBlock(list):
    new_list =[]
    for item in list:
        if item != "":
            new_list.append(item)
    return new_list

removechars = "!.,:*?"
total_list = []
line = ""
while line != "!!!!!":
    line = input()
    if len(line) == 0:
        pass
    elif line != "!!!!!":
        
        for char in removechars:
            line = line.replace(char,' ')
        
        line = line.split(" ")
        line = HandleBlock(line)
        
        for item in line:
            total_list.append(item.lower())
    else:
        pass


statistical_table = {}
for item in total_list:
    statistical_table[item] = 0

for item in total_list:
    statistical_table[item] += 1

temp_table = list(zip(statistical_table.values(),statistical_table.keys()))
ordered_table = sorted(temp_table,reverse=True)


final_ordered_table = []
cut_list = []
cur_times = ordered_table[0][0]
for item in ordered_table:
    if item[0] == cur_times:
        cut_list.append((item[1],item[0]))
    else:
        ordered_cut_list = sorted(cut_list)
        final_ordered_table += ordered_cut_list
        
        cur_times = item[0]
        cut_list = []
        cut_list.append((item[1], item[0]))
ordered_cut_list = sorted(cut_list)
final_ordered_table += ordered_cut_list

numb = len(final_ordered_table)
print(numb)
for i in range(10):
    print(f"{final_ordered_table[i][0]}={final_ordered_table[i][1]}")

7-3 获奖 (10 分)

7-3 获奖 (10)
在某次竞赛中,判题规则是按解题数从多到少排序,在解题数相同的情况下,按总成绩(保证各不相同)从高到低排序,取排名前60%的参赛队(四舍五入取整)获奖,请确定某个队能否获奖。

输入格式:
首先输入一个正整数T,表示测试数据的组数,然后是T组测试数据。每组测试的第一行输入1个整数n(1≤n≤15)和1个字符串ms(长度小于10且不含空格),分别表示参赛队伍总数和想确定是否能获奖的某个队名;接下来的n行输入n个队的解题信息,每行一个1个字符串s(长度小于10且不含空格)和2个整数m,g(0≤m≤100≤g≤100),分别表示一个队的队名、解题数、成绩。当然,n个队名中肯定包含ms。

输出格式:
对于每组测试,若某队能获奖,则输出“YES”,否则输出“NO”。引号不必输出。

输入样例:
1
3 team001
team001 2 27
team002 2 28
team003 0 7
输出样例:
YES

仅供参考

T=int(input())
for z in range(T):
    n,ms=input().split()
    n=eval(n)
    canAward=int(n*0.6+0.5)
    lstS=[]
    lstM=[]
    lstG=[]
    lst=[]
    for i in range(n):
        s,m,g=input().split()
        m,g=int(m),int(g)
        lstS.append(s)
        lstM.append(m)
        lstG.append(g)
    nameIndex=lstS.index(ms)
    grade=lstG[nameIndex]
    lstG.sort(reverse=True)
    sortedIndex=lstG.index(grade)
    if sortedIndex<= canAward-1:
        print("YES")
    else:
        print("NO")

7-4 字典的应用-找出出现次数最多的字符串(高教社,《Python编程基础及应用》习题7-6) (3 分)

7-4 字典的应用-找出出现次数最多的字符串(高教社,《Python编程基础及应用》习题7-6(3)
编写一个程序,从键盘读取未指定个数的字符串,一行一个,以字符串"q"为输入结束标志("q"不列入统计范围)。使用字典找出其中出现次数最多的字符串,打印该字符串及其出现次数。 
注意:本题的测试用例中将保证只有一个字符串出现次数最多。

输入格式:
字符串1
字符串2
...
字符串n
q

输出格式:
字符串 出现次数

输入样例:
abc
abc
bcd
xxx
q
结尾无空行
输出样例:
abc 2
结尾无空行

仅供参考

word_dict = {}

while True:
    word=input()
    if word=='q':
        break
    if word in word_dict:
        word_dict[word] += 1
    else:
        word_dict[word] = 1

# print(word_dict)
mx=max(word_dict.values())
for key in word_dict:
    if word_dict[key]==mx:
        print(key,mx,sep=' ',end='')
        break

7-5 sdut-查验身份证 (10 分)

7-5 sdut-查验身份证 (10)
一个合法的身份证号码由17位地区、日期编号和顺序编号加1位校验码组成。校验码的计算规则如下:

首先对前17位数字加权求和,权重分配为: {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};然后将计算的和对11取模得到值Z;最后按照以下关系对应Z值与校验码M的值:

Z:0 1 2 3 4 5 6 7 8 9 10
M:1 0 X 9 8 7 6 5 4 3 2
现在给定一些身份证号码,请你验证校验码的有效性,并输出有问题的号码。

验证身份证合法性的规则:(1)前17位是否全为数字;(2)最后1位校验码计算准确。

输入格式:
输入第一行给出正整数N(≤100)表示:输入的身份证号码的个数。

随后N行,每行给出118位身份证号码。

输出格式:
按照输入的顺序每行输出1个有问题的身份证号码。

如果所有号码都正常,则输出All passed。

输入样例1:
4
320124198808240056
12010X198901011234
110108196711301866
37070419881216001X
结尾无空行
输出样例1:
12010X198901011234
110108196711301866
37070419881216001X
结尾无空行
输入样例2:
2
320124198808240056
110108196711301862
结尾无空行
输出样例2:
All passed
结尾无空行

仅供参考

def suum(numList):

    weight=[7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
    sum=0
    for i in range(17):
        sum+=numList[i]*weight[i]

    Z=str(sum%11)
    return Z
Z=['0', '1', '2', '3' ,'4' ,'5', '6' ,'7', '8' ,'9' ,'10']
M=['1', '0' ,'X' ,'9', '8', '7', '6', '5' ,'4' ,'3' ,'2']
ZM=dict(zip(Z,M))
# print(ZM)

N=int(input())
idList=[]
for z in range(N):
    idStr=input()
    idList.append(idStr)
wrong=[]
for id in idList:
    num=[]
    if len(id)!=18:
        wrong.append(id)
        continue
    else:
        ch=id[17]
        idl=list(id)
        idl.pop()
        id17=''.join(idl)
        if not id17.isdigit():
            wrong.append(id)
            continue
        for i in range(17):
            num.append(int(id[i]))

        Z=suum(num)
        if ZM[Z]!=ch:
            wrong.append(id)
# print(wrong)
if len(wrong)==0:
    print('All passed')
else:
    first=1
    for i in wrong:
        if first!=1:
            print()
        print(i,end='')
        first=0

7-6 sdut-查字典 (10 分)

7-6 sdut-查字典 (10)
遇到单词不认识怎么办? 查字典啊!

已知字典中有n个单词,现有m个不认识的单词,询问这m个单词是否出现在字典中,如果在字典中就输出其含义,如果不在字典中,就输出:Not found!

输入格式:
含有多组测试用例。

第一行输入n,m (n>=0&&n<=100000&&m>=0&&m<=100000)分别是字典中存在的n个单词,和要查询的m个单词.

紧跟着n行,代表字典中存在的单词及其解释(含义);

然后m行,要查询的m个单词。

若n=0&&m=0 程序结束

输出格式:
对于待查询的单词,若在字典中存在则输出其解释(含义),不存在输出Not found!

输入样例:
3 2
red: having the colour of blood or fire
green:having the colour of grass or the leaves of most plants and trees
blue:having the colour of a clear sky or the sea/ocean on a clear day
blue
abcded
0 0
结尾无空行
输出样例:
having the colour of a clear sky or the sea/ocean on a clear day
Not found!
结尾无空行

仅供参考

m,n=map(int,input().split())
while m!=0 and n!=0:
    dict={}
    for i in range(m):
        k,v=map(str,input().split(':'))
        dict[k]=v
    first=1
    for i in range(n):
        word=input()
        if first==1:
            print(dict.get(word,'Not found!'),end='')
            first=0
        else:
            print()
            print(dict.get(word,'Not found!'),end='')

    m,n=map(int,input().split())

7-7 词频统计 - 实验11 字典操作及应用 - 《Python编程实验》 (10 分)

7-7 词频统计 - 实验11 字典操作及应用 - 《Python编程实验》 (10)
从键盘读入由仅由英文构成的多行文本,借助于字典统计其中每个单词出现的次数。然后按字典递增序按格式输出每个单次出现的次数。


要求:
1. 所有单词不区分大小写,输出时按小写格式输出;
2. 需要排除! , : ?等英文符号,即这些符号不应作为单词的构成部分; 
3. 需要排除012... 9等数字,即这些数字不应作为单词的构成部分。
输入格式:
行数n
第1行内容
第2行内容
....
第n行内容

输出格式:
单词1 单词1出现次数
单词2 单词2出现次数
...
单词k 单词k出现次数

注意: 单词12.... k 按字典递增序。

输入样例:
4
Hello! baby! Jupyter 4 class 
class ultimately.
class
jupyter
结尾无空行
输出样例:
baby 1
class 3
hello 1
jupyter 2
ultimately 1
结尾无空行

仅供参考

n = int(input())
word = ""
dict = []
dict1 = []
while n:
    n -= 1
    st = 0
    str = input()
    for i in str:
        if (ord(i)>=ord('A') and ord(i)<=ord('Z')) or (ord(i)>=ord('a') and ord(i)<=ord('z')):
            word+=i
            st = 1
        elif st==1:
            st = 0
            #if word not in dict:
            dict.append(word.lower())
            word = ""
    if st==1:
        st = 0
        word.lower()
        #if word not in dict:
        dict.append(word.lower())
        word = ""
dict.sort()
cnt = 1
for i in dict:
    if i not in dict1:
        dict1.append(i)
        print("%s %d"%(i,dict.count(i)))

7-8 使用for循环和字典功能,统计字符出现在一个英文句子中的次数 (5 分)

7-8 使用for循环和字典功能,统计字符出现在一个英文句子中的次数 (5)
本题目要求写出一段代码实现输入一个英文句子后,统计句子中各个字符(不区分大小写,包含空格和标点符号)出现的次数。

输入格式:
请例如:输入 Life is short,we need Python. 。

输出格式:
对每一个字符输出对应的出现次数,{'l':1,'i':2,'f':3'......}。

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

Life is short,we need Python.
结尾无空行
输出样例:
在这里给出相应的输出。例如:

{'l':1,'i':2,'f':1,'e':4,' ':4','s':2,'h':2,'o':2,'r':1,'t':2,',':1,'w':1,'n':2,'d':1,'p':1,'y':1,'.':1}
结尾无空行

仅供参考

str=input()
str=str.lower()
list=list(str)
dict={}
for c in list:
    if c in dict:
        dict[c]+=1
    else:
        dict[c]=1
print(dict,end='')

7-9 字典查询1–查询省会 (30 分)

7-9 字典查询1--查询省会 (30)
总理有诗云:两湖两广两河山,五江云贵福吉安,四西二宁青甘陕,还有内台北上天。 中国各省、直辖市、自治区和特别行政区的字典数据如下:

capitals = {'湖南':'长沙','湖北':'武汉','广东':'广州','广西':'南宁','河北':'石家庄','河南':'郑州','山东':'济南','山西':'太原','江苏':'南京','浙江':'杭州','江西':'南昌','黑龙江':'哈尔滨','新疆':'乌鲁木齐','云南':'昆明','贵州':'贵阳','福建':'福州','吉林':'长春','安徽':'合肥','四川':'成都','西藏':'拉萨','宁夏':'银川','辽宁':'沈阳','青海':'西宁','海南':'海口','甘肃':'兰州','陕西':'西安','内蒙古':'呼和浩特','台湾':'台北','北京':'北京','上海':'上海','天津':'天津','重庆':'重庆','香港':'香港','澳门':'澳门'}

设计程序,接收用户输入的省、直辖市、自治区和特别行政区名称,输出对应的省会名称,当输入错误时输出“输入错误”。

输入格式:
第一行输入一个整数n

接下来输入n行,每行输入一个省、直辖市、自治区或特别行政区名称‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬

输出格式:
输出对应的省会名称

输入样例1:
2
新疆
北京
输出样例1:
乌鲁木齐
北京
输入样例1:
1
美国
输出样例1:
输入错误

仅供参考

capitals = {'湖南':'长沙','湖北':'武汉','广东':'广州',
            '广西':'南宁','河北':'石家庄','河南':'郑州','山东':'济南',
            '山西':'太原','江苏':'南京','浙江':'杭州','江西':'南昌','黑龙江':'哈尔滨',
            '新疆':'乌鲁木齐','云南':'昆明','贵州':'贵阳','福建':'福州','吉林':'长春',
            '安徽':'合肥','四川':'成都','西藏':'拉萨','宁夏':'银川','辽宁':'沈阳',
            '青海':'西宁','海南':'海口','甘肃':'兰州','陕西':'西安','内蒙古':'呼和浩特',
            '台湾':'台北','北京':'北京','上海':'上海','天津':'天津','重庆':'重庆','香港':'香港',
            '澳门':'澳门'}
n=int(input())
for i in range(n):
    provice=input()
    print(capitals.get(provice,'输入错误'))



7-10 字典应用–用户登录 (30 分)

7-10 字典应用--用户登录 (30)
有字典如下: dic = {'admin':'123456','administrator':'12345678','root':'password'} 实现用户输入用户名和密码,当用户名与密码和字典中的键值对匹配时,显示“登录成功”,否则显示“登录失败”,登录失败时允许重复输入三次。

输入格式:
在两行中分别输入用户名和密码

输出格式:
"登录成功""登录失败"

输入样例:
admin
12345678
admin
123456
结尾无空行
输出样例:
登录失败
登录成功
结尾无空行

仅供参考

dic = {'admin': '123456', 'administrator': '12345678', 'root': 'password'}
logining = True
while logining:
    try:   
        for i in range(3):
            username = input()
            password = input()
            if username in dic.keys():
                if dic[username] == password:
                    print("登录成功", end='')
                    logining = False
                    break
                else:
                    print("登录失败")
            else:
                print("登录失败")
        logining = False
    except EOFError:
        pass

写在最后

如果这篇文章收不到100赞,下次我就开启付费
这要求不过分吧

本文也可能是最后一篇

我这个人就是心中怀慈悲,手中提利剑
只是有时太怜悯了罢了
但是我说到做到,从来不开玩笑

估计有些人心里会bb
对于喷子
我只呵呵一笑

点击全文阅读


本文链接:http://zhangshiyu.com/post/39339.html

单词  输出  输入  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1