In: Computer Science
You are a college professor who suspects that students have copied
each others answers on a multiple choice (a,b,c,d) exam.
Building on the code I have developed CheatingCodeForCompletion.py , you are to
develop a program that will tell you which students have identical answers.
I have developed the code that will generate answers for each of the students.
You are to complete the code to analyze the answers and print to the screen
your analysis as demonstrated in the two example below
(please note that the output of the names of those who copied prints the names twice).
CheatingCodeForCompletion.py, (below and loaded to eLearn)
import random
##the students in the class
##the most popular names in 2020
["Oliver",\
"Liam",\
"Ethan",\
"Aiden",\
"Gabriel",\
"Caleb",\
"Theodore",\
"Declan",\
"Owen",\
"Elijah",\
"Charlotte",\
"Ava",\
"Amelia",\
"Olivia",\
"Aurora",\
"Violet",\
"Luna",\
"Hazel",\
"Cloe",\
"Aria"
]
##needed to build the test results
test_results = []
answers = ['a', 'b', 'c', 'd']
num_questions = 3 ##Smaller number yields more instances of cheating
##this code will build a dictionary of length 20 of
##key - student name
##value - a list of num_questions length of randomly selected
##values from the list answers above
for student in students:
result = []
for i in range(num_questions):
result.append(random.choice(answers))
test = {}
test[student] = result
test_results.append(test)
Done in Python format Please
Here is the code:
import random
##the students in the class
##the most popular names in 2020
students = ["Oliver","Liam","Ethan","Aiden","Gabriel","Caleb","Theodore","Declan","Owen","Elijah","Charlotte","Ava","Amelia","Olivia","Aurora","Violet","Luna","Hazel","Cloe","Aria"]
len(students)
##needed to build the test results
test_results = []
answers = ['a', 'b', 'c', 'd']
num_questions = 3 ##Smaller number yields more instances of cheating
##this code will build a dictionary of length 20 of
##key - student name
##value - a list of num_questions length of randomly selected
##values from the list answers above
for student in students:
result = []
for i in range(num_questions):
result.append(random.choice(answers))
test = {}
test[student] = result
test_results.append(test)
test_results
indices = list()
for loop1 in range(len(test_results)):
for loop2 in range(loop1+1,len(test_results)):
if(loop1 != loop2): # if both the index are not same
if(test_results[loop1][students[loop1]] == test_results[loop2][students[loop2]]):
indices.append((loop1,loop2))
indices
# printing names of students who cheated
for item in indices:
print(students[item[0]],students[item[1]])
Here is the results:
If you have any doubt, please comment below.