Repository navigation
Expand file tree
/
Copy pathRandomized_Quizzes.R
More file actions
91 lines (59 loc) · 2.14 KB
/
Copy pathRandomized_Quizzes.R
File metadata and controls
91 lines (59 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# CONSTANTS First
No.Of.Questions <- 100
# This is a simple function to swap elements of a vector
swap <- function(x,i,j) {x[c(i,j)] <- x[c(j,i)]; x}
# This function
randomOrder <- function(choices, num.answers){
lapply(choices, str)
for (i in seq(1, length(choices), by=1)) {
if( i %in% 1:num.answers){
choices[i] <- paste(choices[i], "\t", "correct" , sep = "")
}else{
choices[i] <- paste(choices[i], "\t", "incorrect", sep = "")
}
}
# pick a random position for the correct answer.
len <- length(choices)
answerPosition <- sample(1:len, 1)
# randomly swap the correct answer.
choices<-swap(choices , 1, answerPosition)
# add a tab to each of them
choices<-paste(choices, collapse="\t", sep = "" )
choices<-paste("\t", choices, sep = "")
return (choices)
}
# roundUP to 4 digists
roundUP <- function(number){
result <- round(number, 4)
return (result)
}
# Write it to file
writeItToFile <- function(questionID, poolOfQuestions ){
fileConn <- file(questionID)
writeLines(poolOfQuestions, fileConn , useBytes=T)
close(fileConn)
}
# This function generates the random question.
#
randomAndAddToPool<- function(answerVector, question.Text, poolOfQuestions, num.answer){
# Randomize the order of correct answer.
multipleChoiceAnswers <-randomOrder(answerVector, num.answer)
# Multiple Answer should start with MC
if (num.answer == 1) {
question.Text <- paste("MA\t", question.Text, sep = "" )
} else if (num.answer == 0 ) {
# Essay Question
question.Text <- paste("ESS\t", question.Text, sep = "" )
# If it is an essay question then we do not have any answers.
multipleChoiceAnswers <- ""
} else if (num.answer > 1) {
# Multiple Choice questions should start with MC
question.Text <- paste("MC\t", question.Text, sep = "" )
} else {
#ERROR
print("ERROR: Number of answers must be 0 for Essay, 1 for Multiple Choice or larger than one for Multiple Answer.")
question.Text <- "ERROR"
}
finalQuestion <- paste(question.Text , multipleChoiceAnswers , sep = "")
return(c(poolOfQuestions, finalQuestion))
}