【问题标题】:Data structure to implement for millions of data为数百万数据实现的数据结构
【发布时间】:2020-12-30 05:25:48
【问题描述】:

有 1000 名学生在竞争性考试中尝试 x 个问题,其中 x 是您的生日,编码为 ddmmyyyy 格式。例如,如果您的生日是 2000 年 11 月 12 日,那么 x=11122000。每个学生答对一题,答错一题扣0.5分。作为惩罚 p=0.5*n,每个错误答案的负分都会增加,其中 n 代表第 n 个错误答案。问题分为5个主题,类别中的问题数量比例为10:4:3:2:1。所有问题均为多项选择题 (MCQ) 类型,可能有多个正确答案。

我尝试实施以下方法,但由于数据非常高,因此需要很长时间才能执行。还有其他方法可以实现吗?

import random

userInput = int(input("Enter a DOB:"))
ratio = [10,4,3,2,1]
nQues = []
for r in ratio:
    input1 = int((r/20)*userInput )
    nQues.append(input1)
    print('catogorise')
    print(input1)

nStud = 10

que = []
ans = []

for i in range(userInput):
     quesOption = random.randint(1,4)
     que.append(quesOption)
        
     ansOption = random.randint(1,4)
     ans.append(ansOption)
        

correct = []
incorrect = []

category1 = {}
category2 = {}
category3 = {}
category4 = {}
category5 = {}
   
correctMarks = 0
incorrectMarks = 0
noOfIncorrect = 0

a=0

for n in nQues:
    b=n
   
    for j in range(nStud):    
        
        for i in range(a,b):  #(n)
            if que[i] == ans[i] :
                correct.append(i+1)
                correctMarks += 1
            
            if que[i] != ans[i] :
                incorrect.append(i+1)
                noOfIncorrect += 1
                incorrectMarks += 0.5*(noOfIncorrect)
            totMarks = correctMarks - incorrectMarks
        
            # category1[j+1] = totMarks
        
            if n == nQues[0]:
                category1[j+1] = totMarks
                # print("CAt1 finihed")
        
            if n == nQues[1]:
                category2[j+1] = totMarks
                # print("CAt2 finihed")
            
            if n == nQues[2]:
                category3[j+1] = totMarks
                # print("CAt3 finihed")
            
            if n == nQues[3]:
                category4[j+1] = totMarks
                # print("CAt4 finihed")
            
            if n == nQues[4]:
                category5[j+1] = totMarks
                # print("CAt5 finihed")
            a = n+1
  
    
    
print(que)
print(ans)       
print(correct)
print(incorrect)
print(correctMarks)
print(incorrectMarks)
print(totMarks)
print(category1)
print(category2)

【问题讨论】:

  • 结构取决于您打算如何处理数据。按照问题陈述的表述方式,如果学生每周 7 天、每天 16 小时、每个问题用 5 秒时间回答 x 个问题,则需要 2.6 年以上的时间。你需要模拟结果吗?计算统计?另外,多个正确答案的分布是什么?
  • 我们还必须确定学生的水平,才能进行模拟。如果他们都没有研究任何东西而只是随机回答,那么平坦的概率分布将起作用,否则正态分布将是有序的。
  • 目前你所有的学生都给出了相同的答案?对吗?

标签: python python-3.x list dictionary data-structures


【解决方案1】:

我试着把它清理一下,并重命名变量,这样会更容易理解。我还随机分配了每个学生的答案

import random

userInput = int(input("Enter a DOB:"))
questionRatio = [10,4,3,2,1]
questionsPerCategory = [int((r/20)*userInput) for r in questionRatio]

numberStudents = 10

# initialize the solutions for the questions randomly
solutions = [random.randint(1,4) for i in range(userInput)]
# initialize the answers of the students randomly 
# (if the same answers for each student are needed as before change to):
# studentAnswers = [[random.randint(1,4) for i in range(userInput)] * numberStundents]
studentAnswers = [[random.randint(1,4) for i in range(userInput)] for j in range(numberStudents)]

category1 = {}
category2 = {}
category3 = {}
category4 = {}
category5 = {}

#Save correct, incorrect noOfIncorrect, totalMark per student
correctMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
noOfIncorrectForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
incorrectMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))
totalMarksForStudent = dict(zip(range(1,numberStudents+1),[0]*numberStudents))

# we iterate over the solutions, with the indices of the solutions
for index, solution in enumerate(solutions):   
    # we check every studentAnswer for their answer at the current index, and compare them to the solution for current index
    for studentNumber, studentAnswer in enumerate(studentAnswers):
        # Print for Debug purposes
#print("Checking Student", studentNumber+1, "Index", index, studentAnswer[index], solutions[index])
        if studentAnswer[index] == solution:
            # For correct answers increase the correctMarks for this student by one
            correctMarksForStudent[studentNumber+1] += 1
        else:
            # For incorrectAnswers increase the number of incorrect answers for this student by one
            noOfIncorrectForStudent[studentNumber+1] += 1
            # Then add to the already incorrectMarks 0.5 times the no of incorrect answers (this gets big fast)
            incorrectMarksForStudent[studentNumber+1] += 0.5 * noOfIncorrectForStudent[studentNumber+1]
        # update the total mark of the student by setting it to correctMark - incorrectMark
        totalMarksForStudent[studentNumber+1] = correctMarksForStudent[studentNumber+1] - incorrectMarksForStudent[studentNumber+1]
        # print for Debug purposes
#print("Total For Student", studentNumber+1, totalMarksForStudent[studentNumber+1])
        # updating the correct Category at the student index with the new totalMarks of the student
        if index < questionsPerCategory[0]:
            category1[studentNumber+1] = totalMarksForStudent[studentNumber+1]
        elif index < questionsPerCategory[0] + questionsPerCategory[1]:
            category2[studentNumber+1] = totalMarksForStudent[studentNumber+1]
        elif index < questionsPerCategory[0] + questionsPerCategory[1] + questionsPerCategory[2]:
            category3[studentNumber+1] = totalMarksForStudent[studentNumber+1]
        elif index < questionsPerCategory[0] + questionsPerCategory[1] + questionsPerCategory[2] + questionsPerCategory[3]:
            category4[studentNumber+1] = totalMarksForStudent[studentNumber+1]
        else:
            category5[studentNumber+1] = totalMarksForStudent[studentNumber+1]
    
# printing the solutions and the student answers takes a lot of time and terminal space

#print("Solutions",solutions)
#print("StudentAnswers",studentAnswers)     
print("CorrectMark",correctMarksForStudent)
print("IncorrectMarks",incorrectMarksForStudent)
print("StudentMarks",totalMarksForStudent)
print("Category1",category1)
print("Category2",category2)
print("Category3",category3)
print("Category4",category4)
print("Category5",category5)

但这仍然需要很长时间(对我来说大约需要 2 分钟),而且我不确定最终结果应该是什么样子,这就是我能从中得到的。但也许这会帮助你或其他人找出更好的方法。我不确定哪个是更好的做法,迭代每个解决方案并检查每个学生的答案,或者迭代每个学生并检查每个给定答案的解决方案。可能会有一些性能改进

示例输出:

Enter a DOB:08071994
CorrectMark {1: 2018876, 2: 2017217, 3: 2019374, 4: 2017334, 5: 2018198, 6: 2018962, 7: 2017675, 8: 2018907, 9: 2019666, 10: 2016580}
IncorrectMarks {1: 9160060893760.5, 2: 9165082643626.5, 3: 9158553729255.0, 4: 9164728442565.0, 5: 9162113015853.0, 6: 9159800611514.0, 7: 9163696152020.0, 8: 9159967070664.0, 9: 9157670067978.0, 10: 9167011191702.5}
StudentMarks {1: -9160058874884.5, 2: -9165080626409.5, 3: -9158551709881.0, 4: -9164726425231.0, 5: -9162110997655.0, 6: -9159798592552.0, 7: -9163694134345.0, 8: -9159965051757.0, 9: -9157668048312.0, 10: -9167009175122.5}
Category1 {1: -2291264732790.5, 2: -2290928705099.0, 3: -2289516750015.5, 4: -2291965626589.0, 5: -2290094796904.0, 6: -2291553857417.0, 7: -2290554865412.0, 8: -2288873720765.5, 9: -2287783047122.0, 10: -2292098853947.0}
Category2 {1: -4489931797886.0, 2: -4491550819509.0, 3: -4486677684734.0, 4: -4490156409103.5, 5: -4490175480126.0, 6: -4489135109064.0, 7: -4490128862142.5, 8: -4486618375911.0, 9: -4483367581308.5, 10: -4493394825181.5}
Category3 {1: -6618527592931.0, 2: -6623800004942.5, 3: -6616937789939.5, 4: -6620431491756.0, 5: -6619860293359.5, 6: -6618211160609.5, 7: -6621028446142.0, 8: -6615397046632.0, 9: -6612452385439.5, 10: -6624265848443.5}
Category4 {1: -8267619890383.5, 2: -8273785786215.5, 3: -8265316896835.5, 4: -8270291304668.0, 5: -8268056949093.5, 6: -8266360535811.0, 7: -8270711178658.5, 8: -8264977656482.0, 9: -8263226941953.5, 10: -8274205748901.0}
Category5 {1: -9160058874884.5, 2: -9165080626409.5, 3: -9158551709881.0, 4: -9164726425231.0, 5: -9162110997655.0, 6: -9159798592552.0, 7: -9163694134345.0, 8: -9159965051757.0, 9: -9157668048312.0, 10: -9167009175122.5}

【讨论】:

  • 非常感谢,我现在得到了解决方案
  • 如果我的回答有帮助,我将不胜感激,如果你能以某种方式证明这一点。如果没有,您可以发布自己的解决方案,以便偶然发现类似问题的任何人都可以找到解决方案:)
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2017-02-20
  • 2015-08-16
  • 2011-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
相关资源
最近更新 更多