Minty
11/08/2017, 11:26 AMczuckie
11/08/2017, 11:36 AM{
"questions": [
{
"q": "q1",
"a": [
"a1",
"a2",
"a3"
],
"ca": 1
},
{
"q": "q2",
"a": [
"a1",
"a2",
"a3"
],
"ca": 0
}
]
}
And get your system parsing that correctly, then work on selecting random questions from thatMinty
11/08/2017, 11:38 AM[["Q1.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Discussion Method can be used when: \n (A) The topic is very difficult\u00a0 (B) The topic is easy \n (C) The topic is difficult\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (D) All of the above \n ", "C"], ["Q2.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Which of the following is a teaching aid? \n (A) Working Model of Wind Mill\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (B) Tape Recorder \n (C) 16mm Film Projector\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 (D) All the above \n ", "D"],
czuckie
11/08/2017, 11:39 AMMinty
11/08/2017, 11:40 AMczuckie
11/08/2017, 11:40 AMMinty
11/08/2017, 11:41 AMdata = []
for r in re.findall(r"Q\d+.*?(?=Q\d+)", soup.text, flags=re.S):
data_qa = []
rs = re.findall(r"Q\d+.*?(?=Answer.*?\:.*?[a-dA-D])", r.strip(), flags=re.S)
ra = re.findall(r"Answer.*?\:.*?[a-dA-D]", r.strip(), )
for rse in rs:
# print rse
data_qa.append(rse)
for rsa in ra:
if ra.index(rsa) == 0:
# print("The answer is "+ rsa.replace("Answer", "").replace(":", "").strip())
data_qa.append(rsa.replace("Answer", "").replace(":", "").strip())
# print "__________________________________________________________________"
data.append(data_qa)
for d in data:
if len(d)<2:
data.remove(d)
print d
jdump = json.dumps(data)
with file("qNa.json", "w+") as f:
f.write(jdump)
czuckie
11/08/2017, 11:42 AMdata class Question(val question:String, val answer:String)
Minty
11/08/2017, 11:43 AM[["Question1 and choices", "Answer"],["Q2","A"]
czuckie
11/08/2017, 11:43 AMMinty
11/08/2017, 11:44 AMdata class
outside oncreate
right? Can I do it in the main activity classs?czuckie
11/08/2017, 11:44 AMMinty
11/08/2017, 11:46 AMindex
in a list. How do I do that with a data class. I still don't understand the concept of data class. i guess I'll have to look it upczuckie
11/08/2017, 11:46 AMclass MainActivity : AppCompatActivity() {
data class Question(val question: String, val answer: String)
fun parseQuestions(rawQuestionJson: String): List<Question> {
TODO("Parse json")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
parseQuestions("[[\"Question 1\",\"A\"],[\"Question 2\",\"B\"]]")
}
}
class Question(val question:String, val answer:String)
val q1 = Question("q1", "a")
val q2 = Question("q1", "a")
if (q1 == q2) {
println("They are equal")
}
Minty
11/08/2017, 11:49 AMczuckie
11/08/2017, 11:50 AMMinty
11/08/2017, 11:50 AMczuckie
11/08/2017, 11:51 AMMinty
11/08/2017, 11:51 AMczuckie
11/08/2017, 11:51 AMprivate fun parseQuestions(rawQuestionJson: String): List<Question> {
val result = mutableListOf<Question>()
val questionArray = JSONArray(rawQuestionJson)
for (i in 0..questionArray.length()) {
val question = questionArray.getJSONArray(i)
result += Question(question.getString(0), question.getString(1))
}
return result
}
Minty
11/08/2017, 11:54 AMczuckie
11/08/2017, 11:54 AMclass MainActivity : AppCompatActivity() {
data class Question(val question: String, val answer: String)
private fun parseQuestions(rawQuestionJson: String): List<Question> {
val result = mutableListOf<Question>()
val questionArray = JSONArray(rawQuestionJson)
for (i in 0..questionArray.length()) {
val question = questionArray.getJSONArray(i)
result += Question(question.getString(0), question.getString(1))
}
return result
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var tenQuestions = parseQuestions("[[\"Question 1\",\"A\"],[\"Question 2\",\"B\"]]").shuffled().take(10)
TODO("Display ten questions")
}
}
org.json
which is shipped with androidvar tenQuestions = parseQuestions("[[\"Question 1\",\"A\"],[\"Question 2\",\"B\"]]").shuffled().take(10)
may look like black magic butvar tenQuestions = parseQuestions("...JSONHERE...")
.shuffled() // Randomises the order of list items
.take(10) // Take the first 10 items from the list
Minty
11/08/2017, 11:57 AMczuckie
11/08/2017, 11:58 AMMinty
11/08/2017, 12:00 PMczuckie
11/08/2017, 1:04 PM