2019-2020-1 semester 20,192,428 eighth week of work - four elementary arithmetic practice

The work involves the retrieval of this function, the following is what I have made to try and resolve this assignment:
Figure, file Exercises.txt file is generated at the title, but Answers.txt was generated at the answer. Grade.txt generated at retrieval after final answer:

Wherein the code generating portion is subject to the following:

Its code is:

#小学四则运算题目问题
#需要在生成题目的同时生成答案
import random

doc1 = open('Exercises.txt','w')  #打开Exercises.txt文件编写
doc2 = open('Answers.txt','w')  #同上

#随机生成四种运算(加减乘除)并生成对应答案
op = ['+','-','x','÷']  #题目生成语言列表
num1 = num2 = 0
for i in range(100):  #题目循环
    num = i + 1
    num1 =random.randint(0,99)
    num2 =random.randint(0,99)
    opnum = random.randint(0,3) #随机生成0到3的整数以生成不同运算
    if opnum == 3 and num2 == 0:  #被除数不能等于0
        opnum = random.randint(0,2)
    doc1.write('第{:0>3}题  {:0>2}{}{:0>2} =  \n'.format(num,num1,op[opnum],num2))
    #下面四步是分组判断写入答案
    if opnum == 0:  #加法
        doc2.write('第{:0>3}题  {} \n'.format(num,num1+num2))
    elif opnum == 1:  #减法
        doc2.write('第{:0>3}题  {} \n'.format(num,num1-num2))
    elif opnum == 2:  #乘法
        doc2.write('第{:0>3}题  {} \n'.format(num,num1*num2))
    else :  #除法
        if isinstance(num1/num2,int):  #判断答案是否为分数
            doc2.write('第{:0>3}题  {} \n'.format(num,num1/num2))
        else:
            if num1 > num2:
                doc2.write('第{:0>3}题  {}又{}/{} \n'.format(num,num1//num2,num1%num2,num2))
            else:
                doc2.write('第{:0>3}题  {}/{} \n'.format(num,num1,num2))
doc1.close  #关闭文件并保存

Can work to change the code, the corresponding output txt document will show as follows:
Exercises.txt run once after the file:

Answers.txt file after the first run:

These are the topic of the code.


The search part, I encountered difficulties, made the first attempt the following:

Its code is:

data =[]
for line in open("Exercises.txt","r"): #设置文件对象并读取每一行文件
    data.append(line)               #将每一行文件加入到list中

num1 =0  #大循环数
num2 =1  #小循环数

ex1 =data[num1]  #expression表达式
ex2 =data[num2]

tp1 =ex1[1:4]#topic题目数
tp2 =ex2[1:4]

num11 =ex1[7:9]  #第一操作数
num12 =ex1[10:12]  #第二操作数
num21 =ex2[7:9]
num22 =ex2[7:9]

op1 =ex1[9]  #操作符
op2 =ex2[9]

list_repeat =[]  #重复题目
exnum =len(data)  #读取题目数
while num1 <=len(data):
    while num2 <=len(data)-num1-1:
        if op1 ==op2 and op1 in ['+','x']:
            if num11 ==num21 or num11 ==num22:
                list_repeat =list_repeat +tp1 +tp2
                num2 =num2 +1
        elif op1 ==op2 and op1 in ['-','÷']:
            if num11 ==num21 and num12 ==num22:
                list_repeat =list_repeat +tp1 +tp2
                num2 =num2 +1
        else:
            num2 =num2 +1
    num1 =num1 +1
    num2 =num1 +1
print(list_repeat)

Visible code computation is extremely large, need to calculate the minimum number if the topic is 10.0001 trillion times , seen in this way is extremely unscientific (when testing my computer have been crying), which I continued for the second attempts.


Then retrieve different from the first, the second time I first narrowed the range, the first for the first time in narrowing the answer, find the same answer when the subject, you can reduce a lot of time in this case the subject computer workload to reduce computation time.
second attempt code is as follows:

#读取txt文件2
data_answer =[]  #Answers.txt文件语句
data_Exercises =[]  #Exercises.txt文件语句
for line in open("Answers.txt","r"): #设置文件对象并读取每一行文件
    data_answer.append(line)               #将每一行文件加入到list中
for line in open("Exercises.txt","r"):  #同上
    data_Exercises.append(line)

#生成一个只存有答案的列表  
loopnum1 =0  #循环数
answers =""  #答案字符串
while loopnum1 <= len(data_answer) -1:
    answer =data_answer[loopnum1]  #答案语句
    awnum =answer[7:-2]  #答案数据
    answers = answers +awnum +" "
    loopnum1 =loopnum1 +1
awlist =answers.split(sep=" ")  #答案列表
del awlist[-1]  #删除最后一个空字符串

#索引相同答案的列表
loopnum2 =0  #循环数
repeat =""
while loopnum2 <= len(data_answer) -1:
    item =awlist[loopnum2]  #每个元素
    itnum =str(awlist.count(item))  #每个元素重复个数
    repeat =repeat +itnum +" "
    loopnum2 =loopnum2 +1
rep =repeat.split(sep=" ")  #所有元素出现次数的字符串
del rep[-1]  #删除最后一个空字符串

#生成重复题目的题号
repnum =rep.count('2') #重复题目总数
loopnum3 =0
aimnum =""
repaimnum = 0
while loopnum3 <=repnum -1:
    repaimnum =rep.index('2') +1 +loopnum3  #题号
    aimnum =aimnum +str(repaimnum) +" "
    rep.remove('2')
    loopnum3 =loopnum3 +1

print("一共有{}道题目错误,他们的题号是{}".format(repnum,aimnum))

# 文本进度条,纯属好玩才加的
import time
scale = 50
print("检查重复题目开始".center(scale//2,"_"))
start = time.perf_counter()
for i in range(scale + 1):
    a = "■" * i
    b = "□" * (scale - i)
    c = (i / scale) * 100
    dur = time.perf_counter() - start
    print("\r{:3^.0f}%[{}{}]{:.1f}秒".format(c,a,b,dur),end="")
    #         └百分数   └进度条  └秒数
    time.sleep(0.1)
print("\n"+"检查结束".center((scale +8)//2,"_"))

Remove the progress bar is purely for fun, a total of 42 lines of code, and is not yet completed, the following is the result of the current runtime:

Guess you like

Origin www.cnblogs.com/KamanFuture/p/11944511.html