[Computer Level 2 Python] Comprehensive Topics

Computer level two python real questions



insert image description here


1. Simple application questions 1

five colored circles

Please refer to programming, write code to replace the horizontal line content, and do not modify other codes to achieve the following functions.

(1) Use the turtle library and random library to draw 5 colored circles on the screen;

(2) The color of the circle is randomly obtained from the color list color;

(3) The x and y coordinates of the center of the circle are selected from the range [-100, 100], and the radius is selected from the range [10,30]. Results as shown below.
insert image description here

code:

import turtle as t
import random as r

color = ['red','green','blue','purple','black']
r.seed(1)
for j in range(
5
):
    t.pencolor(color[r.randint(0,4)])
    t.penup()
    t.goto(r.randint(-100,100),r.randint(-100,100))
    t. pendown()
    t.circle(r.randint(10,30))
t.done()

2. Simple application questions 2

Chinese participle

Please write code to replace the ellipsis, and other codes can be modified to achieve the following functions:

(1) Obtain a piece of text input by the user, including but not limited to Chinese characters, Chinese punctuation marks and other characters;

(2) Use jieba's precise mode word segmentation, and count the word frequency of Chinese words after word segmentation, specifically: write words with a character length greater than or equal to 2 and their word frequency into the file data.txt, each line - a word, words and word frequencies are separated by Chinese colons.

Examples are as follows (where the data is for illustration only):

Input:
With the advantage of the platform, publicize and promote corresponding products, and provide more opportunities for technology practitioners to learn, communicate, and discuss, thereby promoting technology exchanges, enterprise interoperability, talent training, and technological development.

output:

Rely on: 1
platform: 1
advantage: 1
publicity: 1
promotion: 1
response: 1
product: 1
technology: 3
practitioners: 1
provision: 1
learning: 1
communication: 2
discussion: 1
opportunity: 1
thus: 1
promotion: 2
enterprise: 1
intercommunication: 1
talent training: 1
development: 1
… (omitted)

code:

import jieba
s = input("请输入一个中文字符串,包含逗号和句号:")       #请输入一个中文字符串,包含逗号和句号:
k = jieba.lcut(s)   #使用jieba库的精确分词模式
dict1 = {
    
    }          #定义空字典dict1    
for i in k:         #构建词与词频键值对存放在字典dict1中
    dict1[i] = dict1.get(i, 0) + 1      #字典的get函数
with open('data.txt', 'w') as f:        #使用with语句处理文件时,无论是否抛出异常,都能保证with语句执行完毕后关闭已经打开的文件
    list1 = []                      
    for k,v in dict1.items():            #筛选键值字符串长度大于等于2的键值对存放到list1列表中 
        if len(k)>=2:
            list1.append(k + ':' + str(v))
    for item in list1:                         #将列表元素逐行写入data.txt文件
        f.write(item +'\n')                   #'\n'表示换行

3. Comprehensive application questions 1

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: product, tool, and community. 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) For the first 10 words with the most frequent words in the statistics file, the length of which is not less than 2 characters, the words and their frequency numbers are sorted in descending order of word frequency and displayed on the screen. One word is displayed in each line, and the words and their word frequencies are connected with English colons.

Examples are as follows:

We: 5
System: 3
Wechat: 3
... (omitted)

code:

import jieba
dict_words = {
    
    }
with open('data3.txt', 'r', encoding='GBK') as f:
    lines = f.read().split("\n")
for line in lines:
    words = jieba.lcut(line)
    for word in words:
        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]))

code:

import jieba
dict_words = {
    
    }
with open('data3.txt', 'r', encoding='GBK') as f:
    k = jieba.cut(f.read())
for i in k:
    if len(i) >= 2:
        dict_words[i] = dict_words.get(i, 0) + 1
data = sorted(dict_words.items(), key=lambda x:x[1], reverse=True)
print(''.join([k + ':' + str(v) +'\n' for k, v in data[:10]]))

4. Comprehensive application questions 2

Value Chain

Program to achieve the following functions:

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

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

code:

import jieba
import re
dict_words = {
    
    }
with open('data3.txt', 'r', encoding='GBK') as f:
    senses =  re.sub('([,。\n])', '|' , f.read())
k = jieba.cut(senses)
for i in k:
    if len(i) >= 2:
        dict_words[i] = dict_words.get(i, 0) + 1
data = sorted(dict_words.items(), key=lambda x:x[1], reverse=True)
with open('out.txt', 'w') as f:
    for sense in senses.split('|'):
        if data[0][0] in sense:
            f.write(sense+ '\n')

Python re module: https://www.cnblogs.com/shenjianping/p/11647473.html

insert image description here

Guess you like

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