【Computer Level 2】Comprehensive Topics

Computer level two python real questions



insert image description here


1. "Two Questions about University MOOCs"

The file data.txt in the attachment is the source file of an HTML page on the Chinese University MOOC platform of the Ministry of Education's Love Course Network, which contains a list of universities or institutions in my country participating in the construction of MOOC.

This question answers question 1

Question 1: Please write a program to extract the list of university or institution names from data.txt, write the result into the file univ.txt, one university or institution name per line, and output the universities or institutions in the order in which they appear in data.txt, as follows:

……

Beijing Institute of Technology

……

Beijing Normal University

……

Tip: All university names exist in the form of alt="Beijing Institute of Technology" in the data.txt file.

code:

with open("data.txt","r",encoding="utf-8") as f:
    lines = f.readlines()

f = open("univ.txt", "w")
for line in lines:
    if "alt=" in line:
    # 判断是否有alt,若有则用'alt'分割,分割后再用'"'分割
        d = line.split("alt=")[-1].split('"')[1]    
        f.write("{}\n".format(d))
f.close()

This question answers question 2:

Please write a program to extract the name of the university from the univ.txt file. The name of the university is based on the words "university" or "college" as a reference, but does not include the words "university student". Output all the names of universities on the screen. There is no blank line between each line of the university. Finally, the number of names containing "university" and "college" is given, and there are universities and colleges for university processing. The sample is as follows (the number in the sample is not the real result):

……

Beijing Institute of Technology

……

Changsha Normal University

……

The number of names containing universities is 10

The number of names containing colleges is 10

code:

n = 0  # 记录大学数量的计数器n
m = 0  # 记录学院数量的计数器m
with  open("univ.txt", "r", encoding="utf-8") as f:  # 以读的方式打开文件univ.txt
    lines = f.readlines()  # 返回一个列表,列表中每一个元素对于文件中每一行

for line in lines:  # 遍历列表中每一个元素
    line = line.replace("\n", "")  # 去除元素中的换行符
    if '大学生' in line:  # 如果列表元素中包含"大学生"字符串,不做计数
        continue
    elif '大学' in line:  # 基于列表元素中不包含"学院"字符串,而包含"大学"字符串。
        # 同时有大学和学院做大学处理,例如 中国社会科学院大学 (考点)
        print('{}'.format(line))  # 计数为大学+1,例如南京大学
        n += 1
    elif '学院' in line:  # 如果列表元素中包含"学院"字符串,则默认计数为学院+1
        print('{}'.format(line))  # 例如:江苏理工学院,归属于学院
        m += 1
print("包含大学的名称数量是{}".format(n))  # 输出大学计数
print("包含学院的名称数量是{}".format(m))  # 输出学院计数

2. Comprehensive application questions - value chain

There is a material file data3.txt in the attachment, and the content of the file is as follows:

The business model value chain consists of three links: products, tools, and communities. Our team takes one-stop system development as the current main product, and uses XAMPP, PHPSTORM, WeChat developer tools and other software to provide a suitable integrated management system according to customer requirements. ‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

… (slightly

Please program to achieve the following functions:

(1) The first 10 words with the most frequent words in the statistics file and the length of not less than 2 characters are displayed on the screen after sorting the words and their frequency numbers in descending order of word frequency. Each line displays - a word, and connects the words and their word frequencies with English colons. ‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

The example is as follows:
We: 5
System: 3
Wechat: 3
... (omitted)

import jieba
dict_words = {
    
    }
with open('data3.txt', 'r', encoding='GBK') as f:
    txt = f.read()
wordlist = jieba.lcut(txt)
for word in wordlist:
    if len(word)>=2:
        dict_words[word]=dict_words.get(word,0)+1
ls=list(dict_words.items())
ls.sort(key=lambda x:x[1],reverse=True)

for i in range(10):
    print("{}:{}".format(ls[i][0],ls[i][1]))

(2) The document is divided into short sentences with Chinese commas and Chinese full stops as separators, and the sentences containing the words with the highest word frequency are output to the file out.txt, each sentence is one line, the example is as follows:

In this way, we can attract more business opportunities
and popularize our one-stop development technology
... (omitted)

code:

fi = open('data3.txt','r',encoding='GBK')
lines = fi.read().strip().split('。')
fi.close()

ls=[]
for line in lines:
    linelist = line.strip().split(',')
    for line in linelist:
        if '我们' in line:
            ls.append(line)
fi.close()

fo = open('out.txt','w')
for i in ls:
    fo.write("{}\n".format(i))
fo.close()

3. Basic operation questions - information output

Refer to the code template to improve the code to achieve the following functions. Enter an integer and a character from the keyboard, separated by a comma, and display and output a message on the screen. ‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬

Example 1:

Input: "10,@"
Output: "@@@@@@@@@@10@@@@@@@@@@@"

Code 1:

ls= input().split(',') #将从键盘上输入的用逗号隔开的字符串转换为列表
                       #例如输入:10,@  
                       #ls为['10', '@']
                       #ls[0]为'10' ,eval(ls[0])*2+len(ls[0])表达式值为22   
                       #ls[1]为'@'
print(ls[0].center(eval(ls[0])*2+len(ls[0]),ls[1]))
                       #'10'.center(22,"@")即为输出结果,考察字符串的center方法
#str.center(x,y) 会用字符串str构造一个新的字符串,
#新的字符串长度是x, 两边填充y。此处的x是数字,y是填充字符
# str:'10'
#   x:22
#   y:"@"
#输出结果为"@@@@@@@@@@10@@@@@@@@@@"

Code 2:

ls= input().split(',')
print(eval(ls[0])*ls[1]+ ls[0] + eval(ls[0])*ls[1])

Guess you like

Origin blog.csdn.net/guanguan12319/article/details/129510988