如何使用蛮力来解决并存储多项选择题的答案?
我在理解这种蛮力方法时遇到了一些困难。我正在使用 Perl 来提取问题及其选择。现在所有的问题都存储在一个数组中。我不知道如何处理这些答案。
我应该如何存储答案并组织我的代码,以便它执行类似于此的操作...
选择问题的第一个选项。 (所以通过一题,所有问题都应该选择选项A)。
提交以检查答案是否正确。
解析答案,如果给定的答案是正确的,则将其标记为“正确”答案,并忘记尝试为该问题选择任何其他选项。
否则,请在下一遍中继续浏览该问题的答案列表。
因此,下一遍将选择该问题的第二个答案,直到多次提交后,它通过暴力找到所有“正确”答案。
我无法存储答案并将其与问题关联起来,因为错误而将其划掉或将其标记为“已找到”。
我正在考虑使用哈希。请让我知道有关如何构建代码的任何建议。
谢谢!
编辑
示例数据 -
所以我使用哈希方法...我的哈希看起来像这样:
打印出哈希:
question_123 => a,b,c,d
question_155 => a,b,c
question_234 => T,F
现在我必须找到一种方法来遍历每个选项,直到找到该问题的正确答案。
编辑
为了澄清一些事情,让我们假设有 10 个问题。用户单击“开始练习”,系统会从 10 个问题中随机生成 4 个问题。因此,在当前状态下,我有 4 个问题及其答案。我仔细检查并将这些问题及其答案添加到数据结构中(下面的一个......或使用文件来存储它们)。接下来我要选择一个答案。然后用户必须提交这些问题以供审核。点击提交按钮后,系统会提示问题 x 是否正确。基于此,数据结构必须更新,哪个是该问题的正确答案。
现在冲洗并重复。这次,从同一个池中生成了另外四组随机问题。这次,发现了两个新问题,因此必须将它们添加到数据结构中。应该使用类似的逻辑来寻找答案。此外,每个答案选项(选择)始终附加一个唯一的数值,以便我的服务器可以使用该值检查答案 ID。在我的服务器端,每个问题只有一个 ID 及其关联的正确答案。使用我的桌子会破坏这个实验的目的。
正在发生的情况的视觉效果:
Pass One -- 4/10 Random Questions
1. Who is the US president?
21. Barrak
22. Chap
23. Jim
24. Nivea
2. How many states are there?
25. 99
26. 90
27. 51
3. What is the color of the sky?
28. blue
29. black
30. none
4. Is time relative?
31. False
32. True
因此,在第一次传递时,选择应如下所示:
1 => a - 21
2 => a - 99
3 => a - 28
4 => False - 31
点击提交按钮。服务器响应:
1. Correct
2. Incorrect
3. Correct
4. Incorrect
现在用找到的正确答案更新数据结构。
程序现在根据提示请求一组新问题。这次服务器返回:
Pass Two -- 4/10 Random Questions
4. Is time relative?
31. False
32. True
6. What is not a plant?
65. Cow
66. Rose
67. Tree
1. Who is the US president?
21. Barrack
22. Chap
23. Jim
24. Nivea
8. What is a programming language?
99. C++
100. Tylenol
101. Mr.Monster
看,现在在这一关出现了两个新问题。对于这些人来说,必须选择第一个选项,但对于重复的选项,应该选择下一个选项,除非已经找到正确的答案。
因此,这将被发送到服务器:
4. True - 32
6. Cow - 65
1. Barrack - 21
8. C++ - 99
服务器响应:
4. Correct
6. Correct
1. Correct
8. Correct
相同处理响应。希望这真的能解决问题。另请注意,每个答案都将始终附加一个唯一的数值。
I am having some trouble understanding this brute force approach. I am using Perl to extract questions and their choices. All of the questions right now are being stored inside of an array. I am not sure what to do with the answers.
How should I store the answers and organize my code so it does something similar to this...
Select the first option for the question. (So pass one, all the questions should have option A selected).
Submit to check for correct answers.
Parse answers, if the given answer was correct mark that as "correct" answer and forget trying to select any other choice for that question.
Otherwise, continue through the list of answers for that question on the next pass.
So the next pass would pick the second answer for the that question, until after multiple submits it finds all of the "correct" answers via brute force.
I am having trouble how to store the answers and associate them with the question, cross them off as they are wrong or tag it "found".
I was thinking about using a hash. Please let me know any suggestions on how I should structure the code.
Thanks!
EDIT
Sample data --
So I am using a HASH method... my hash looks something like this:
print out of the hash :
question_123 => a,b,c,d
question_155 => a,b,c
question_234 => T,F
Now I have to find a way to go through each option until I find the correct answer for that question.
EDIT
To clarify somethings let us assume there is a pool of 10 questions. The user clicks "Begin Practice" which generates 4 random questions from the pool of 10. Thus, at this current state I have four questions with their answers. I go through and add these questions and their answers to a data structure (one from below... or using files to store them). Next I being to pick an answer. Then the user must submit these questions for review. Once the submit button is hit, the prompt says if question x is correct or incorrect. Based on this, the data structure must update which is the correct answer for that question.
Now to rinse and repeat. This time another four set of random questions is generated from that same pool. This time, two new questions are found so these must be added to the data structure. Similar logic should be used to find the answers. Also, each answer option (selection) always has an unique numeric value attached to it so my server can check the answer ID with the value. On my server side each question just has an ID and it's associated right answer. Using my table would defeat the purpose of this experiment.
Visual of what is happening:
Pass One -- 4/10 Random Questions
1. Who is the US president?
21. Barrak
22. Chap
23. Jim
24. Nivea
2. How many states are there?
25. 99
26. 90
27. 51
3. What is the color of the sky?
28. blue
29. black
30. none
4. Is time relative?
31. False
32. True
So, on the first pass the selections should be like the following:
1 => a - 21
2 => a - 99
3 => a - 28
4 => False - 31
Hit the submit button. Server responds with :
1. Correct
2. Incorrect
3. Correct
4. Incorrect
Now update the data structures with correct answer found.
Program now requests new set of questions at the prompt. This time the server returns:
Pass Two -- 4/10 Random Questions
4. Is time relative?
31. False
32. True
6. What is not a plant?
65. Cow
66. Rose
67. Tree
1. Who is the US president?
21. Barrack
22. Chap
23. Jim
24. Nivea
8. What is a programming language?
99. C++
100. Tylenol
101. Mr.Monster
See, now on this pass two new questions arose. For these guys the first options have to be selected but for the repeated ones, the next up should be picked unless the correct answer has already been found.
So this will be sent to the server:
4. True - 32
6. Cow - 65
1. Barrack - 21
8. C++ - 99
The server responds with:
4. Correct
6. Correct
1. Correct
8. Correct
Same deal with the response. Hope this really clears things up. Also note that each answer will always have an unique numerical value attached to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将正确答案和所有可能的选项与每个问题相关联:
然后使用简单的脚本循环浏览选项:
Associate the right answer and all possible options to each question:
Then cycle through the options with a simple script:
我认为你需要一个结构来存储复杂的数据。我可能会建议这样的事情。
I think you need a structure to store complex data. I might suggest something like this.
其他答案都可以,但我会添加一个......
如果您想表示探测过程的某些“状态”或“标志”,您可以简单地向数据结构添加更多字段,如下所示:
对于处理嵌套的基础知识Perl 中的数据结构,请参阅 http://perldoc.perl.org/perldsc.html
Other answes are OK, but I'll add one...
If you want to represent some "state" or "flags" for probing process, you can simply add more fields to your data structures, like this:
For basics of handling nested data structures in Perl, see code examples in http://perldoc.perl.org/perldsc.html