I do not have a smart or at least working idea how to solve following challenge:
I have 198 assignments and let each assignment solved by 10 persons, then I wrote everything in a PostgreSQL database by using Java hibernate & persistence API. That worked fine so far.
Sometimes I do have 10 different/distinct answers for an assignment – for other assignments I do have only 2 or 3 different/distinct answers (e.g. for the assignment “what is 5 + 5”, 8 persons told “10” and 2 persons told “25”)
Now I run a SQL statement to get a list with my assignments and the distinct answers:
SELECT DISTINCT question, answer FROM survey INNER JOIN results ON results.survey_id=results.id;
what I get now is a result list that looks more or less like this:
+---------+----------+--------+
| ID | Question | Answer |
+---------+----------+--------+
| 1 | Q1 | 20 |
| 2 | Q1 | 22 |
| 3 | Q1 | 25 |
| 4 | Q1 | 21 |
| 5 | Q1 | 22 |
| 6 | Q1 | 10 |
| 7 | Q1 | 20.5 |
| 8 | Q1 | 22.3 |
| 9 | Q1 | 28 |
| 10 | Q1 | 26 |
| 11 | Q2 | 52 |
| 12 | Q2 | 51 |
| 13 | Q3 | 78 |
| 14 | Q3 | 80 |
| ... | ... | ... |
| ... | ... | ... |
| ... | ... | ... |
+---------+---------+---------+
now the challening part:
I want now randomly pick out 4 distinct answers (if possible) from each assignment (Q1, Q2, Q3, …) and create a new assignment where people have to vote on the best answer.
But as shown, sometimes I do have less then 4 distinct answers for an assignment. In this case I want to take everything that’s available.
How could I iterate through my list and perform this kind of “picking”?
P.S. it’s not very important to pick the answers randomly – would also be okay to pick the first 4 answers.
appreciate your help
regards
if the resultset is that small i would just