user9198426
user9198426

Reputation:

Not repeating elements in repeated random choice

I have two text files, students and questions. In students are students and in questions are questions. Each student corresponds to one question, but sometimes questions are the same and I want to not repeat questions.

import random

k = int(input("how many questions do you want ? -"))
questions1 = open("question.txt",'r')
studenti = open("students.txt",'r')
splited = questions1.read().split('\n')
splitedstud = student1.read().split('\n')

for studens in splitedstud:
    file = open(studens + ".txt", "w")
    for i in range(int(k)):
         questiion = random.choice(splited)
         file.write(questiion + '\n')
         file1 = open(studens + ".txt", "r")
         f = file1.read()
         if questiion in f:
             continue
    file.close()

questions.close()
students.close()

Upvotes: 2

Views: 152

Answers (3)

Patrick Artner
Patrick Artner

Reputation: 51683

If you want to pair up each student with exactly one question use:

import random

studs = [chr(ord('a')+x) for x in range(20)]   # testdata - instead of your read in files
ques = ["question"+str(x) for x in range(20)]  # the files in, just demodata

random.shuffle(ques) # get questions into a random arrangement

for p in  zip(studs,ques):  # pairs each student to the same indexed question [(s1,q1),...]
    with open(p[0]+".txt","w") as f:  # with syntax: better than using blank open/close ops
        f.write(p[1])

Output:

20 files 'a.txt' to 'p.txt' containing one random question from ques - no duplicates over all files.

If either list given as input to zip() is shorter, zip will only create pairs up to that item, the rest is discarded - so make sure you have at least len(studenti) questions.


For more then one question per student without repeat you can use:

for s in studs:
    # randomize questions for each student
    random.shuffle(ques)   
    amount = random.randint(3,8)  # how many to be put into file
    with open(s+".txt","w") as f:
        for n in (range(amount)): # write questions into file
            f.write(ques[n] + "\n")

Output:

20 files 'a.txt' to 'p.txt' containing random 3-8 question from ques - no duplicates inside each file.

Remark:

this is much slower then the solution by Jojo using random.sample(..) because you need to reshuffle the compllete list of question for each student, while random.sample() will only draw k samples per student.

Doku for shuffle: https://docs.python.org/3/library/random.html#random.shuffle

Upvotes: 1

j-i-l
j-i-l

Reputation: 10997

What you want is to draw a random sample of the questions. From the docs:

random.sample(population, k): Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement.

In that way you avoid drawing the same question multiple times for a single student.

Something like:

for studens in splitedstud:
    file = open(studens + ".txt", "w")
    random_questions = random.sample(splited, k)
    for rand_question in random_questions:
         file.write(rand_question + '\n')
    file.close()

Upvotes: 1

Rakesh
Rakesh

Reputation: 82805

Change

splited = questions1.read().split('\n')

to

splited = list(set(questions1.read().split('\n')))

to remove all duplicate questions.

Upvotes: 0

Related Questions