Make 3 levels of difficulty in the quiz app

I am developing a simple quiz app on Android. The questions in the quiz should get more difficult as you progress in the quiz. Now I'm trying to implement a static method that takes a list of objects Question

and produces (selects) a sub-list of matching questions based on complexity and needs to be ordered (the first question in the list is the simplest one). The application has 3 levels (modes) of difficulties.

Here's a snippet of the method:

public static List<Question> getQuestions(List<Question> availableQuestions,
                                       int quizDifficulty, int numberOfQuestion)
{
    if(availableQuestions.size() < numberOfQuestion)
        throw NotEnoughQuestionsException();

    List<Question> questions = new ArrayList<Question>(numberOfQuestion);
    if(quizDifficulty == 0) // Easy
    {
        // ...
        return questions;
    }
    else if(quizDifficulty == 2) // Hard
    {
        // ...
        return questions;   
    }
    else /*if(quizDifficulty == 1)*/ // Normal
    {
        // ...
        return questions;
    }
}

      

Each object Question

has a field difficulty

that ranges from 1 (most simple)

to 10 (most difficult)

, and this field can be accessed by method getDifficulty()

.

However, in order to implement the method, I decided that the difficulties with the tasks should not exceed the level 8

for the mode Easy

, and they should be greater than the level 3

in Hard

and between the level 9

and the level 2

in the mode Normal

.

The problem is that the list of questions provided is availableQuestions

not guaranteed to have all the required difficulty levels, for example, all questions are at level 1.

So my question is, what is the best way to implement this method?


EDIT:

Here's my progress:

public static List<Question> getQuestions(List<Question> availableQuestions,
                                      int quizDifficulty, int numberOfQuestion)
{
    if(availableQuestions.size() < numberOfQuestion)
        throw NotEnoughQuestionsException();

    List<Question> questions = new ArrayList<Question>(numberOfQuestion);
    Map<Integer, List<Question>> map = new HashMap<Integer, List<Question>>();
    for(int i = 1; i <= 10; i++) map.put(i, new ArrayList<Question>());
    for(Question question : availableQuestions) 
        map.get(question.getDifficulty()).add(question);

    int L1 = map.get(1).size(); // number of questions with level 1
    int L2 = map.get(2).size();
    int L3 = map.get(3).size();
    int L4 = map.get(4).size();
    int L5 = map.get(5).size();
    int L6 = map.get(6).size();
    int L7 = map.get(7).size();
    int L8 = map.get(8).size();
    int L9 = map.get(9).size();
    int L10 = map.get(10).size();

    final int L1_TO_L8  = 0;
    final int L1_TO_L9  = 1;
    final int L1_TO_L10 = 2;
    final int L2_TO_L9  = 3;
    final int L2_TO_L10 = 4;
    final int L3_TO_L10 = 5;

    int status;

    if(difficulty == 0) // Easy (level 1 to level 8)
    {
        int missing = questionsCount - (L1+L2+L3+L4+L5+L6+L7+L8);
        if(missing > 0) // not enough questions in L1 through L8
        {
            if(missing - L9 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L1 through L9
            {
                status = L1_TO_L9;
            }
        }
        else // enough questions in L1 through L8
        {
            status = L1_TO_L8;
        }
    }
    else if(difficulty == 2) // Hard (level 3 to level 10)
    {
        int missing = questionsCount - (L3+L4+L5+L6+L7+L8+L9+L10);
        if(missing > 0) // not enough questions in L3 through L10
        {
            if(missing - L2 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L2 through L10
            {
                status = L2_TO_L10;
            }
        }
        else // enough questions in L3 through L10
        {
            status = L3_TO_L10;
        }
    }
    else /*if(difficulty == 1)*/ // Normal (level 2 to level 9)
    {
        int missing = questionsCount - (L2+L3+L4+L5+L6+L7+L8+L9);
        if(missing > 0) // not enough questions in L2 through L9
        {
            if(missing - L1 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L1 through L9
            {
                status = L1_TO_L9;
            }
        }
        else // enough questions in L2 through L9
        {
            status = L2_TO_L9;
        }
    }

    // ...
}

      

+3


source to share


5 answers


I implemented my own algorithm and it works just fine, although it has a lot of duplicate codes:



public static List<Question> getQuestions(List<Question> availableQuestions,
                                      int quizDifficulty, int numberOfQuestion)
{
    if(availableQuestions.size() < numberOfQuestion)
        throw NotEnoughQuestionsException();

    List<Question> questions = new ArrayList<Question>(numberOfQuestion);
    Map<Integer, List<Question>> map = new HashMap<Integer, List<Question>>();
    for(int i = 1; i <= 10; i++) map.put(i, new ArrayList<Question>());
    for(Question question : availableQuestions) 
    map.get(question.getDifficulty()).add(question);

    int L1 = map.get(1).size(); // number of questions with level 1
    int L2 = map.get(2).size();
    int L3 = map.get(3).size();
    int L4 = map.get(4).size();
    int L5 = map.get(5).size();
    int L6 = map.get(6).size();
    int L7 = map.get(7).size();
    int L8 = map.get(8).size();
    int L9 = map.get(9).size();
    int L10 = map.get(10).size();

    final int L1_TO_L8  = 0;
    final int L1_TO_L9  = 1;
    final int L1_TO_L10 = 2;
    final int L2_TO_L9  = 3;
    final int L2_TO_L10 = 4;
    final int L3_TO_L10 = 5;

    int status;

    if(difficulty == 0) // Easy (level 1 to level 8)
    {
        int missing = questionsCount - (L1+L2+L3+L4+L5+L6+L7+L8);
        if(missing > 0) // not enough questions in L1 through L8
        {
            if(missing - L9 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L1 through L9
            {
                status = L1_TO_L9;
            }
        }
        else // enough questions in L1 through L8
        {
            status = L1_TO_L8;
        }
    }
    else if(difficulty == 2) // Hard (level 3 to level 10)
    {
        int missing = questionsCount - (L3+L4+L5+L6+L7+L8+L9+L10);
        if(missing > 0) // not enough questions in L3 through L10
        {
            if(missing - L2 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L2 through L10
            {
                status = L2_TO_L10;
            }
        }
        else // enough questions in L3 through L10
        {
            status = L3_TO_L10;
        }
    }
    else /*if(difficulty == 1)*/ // Normal (level 2 to level 9)
    {
        int missing = questionsCount - (L2+L3+L4+L5+L6+L7+L8+L9);
        if(missing > 0) // not enough questions in L2 through L9
        {
            if(missing - L1 > 0) // we must include all the level
            {
                status = L1_TO_L10;
            }
            else // enough questions in L1 through L9
            {
                status = L1_TO_L9;
            }
        }
        else // enough questions in L2 through L9
        {
            status = L2_TO_L9;
        }
    }

    if(status == L1_TO_L8) // Look into level 1 through level 8 only
    {
        int q = 0;
        for(int level = 1; level <= 8 && q < numberOfQuestion; level += 8 * q <= numberOfQuestion * level? 0 : 1)
        {
            if(level == 1)
            {
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
            }
            else if(level == 2)
            {
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
            }
            else if(level == 3)
            {
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
            }
            else if(level == 4)
            {
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
            }
            else if(level == 5)
            {
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 6)
            {
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 7)
            {
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 8)
            {
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
        }
    }
    else if(status == L1_TO_L9)
    {
        int q = 0;
        for(int level = 1; level <= 9 && q < numberOfQuestion; level += 9 * q <= numberOfQuestion * level? 0 : 1)
        {
            if(level == 1)
            {
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 2)
            {
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 3)
            {
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 4)
            {
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 5)
            {
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 6)
            {
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 7)
            {
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 8)
            {
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
            else if(level == 9)
            {
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 1)){q++; continue;}
            }
        }
    }
    else if(status == L2_TO_L9)
    {
        int q = 0;
        for(int level = 2; level <= 9 && q < numberOfQuestion; level += 9 * q <= numberOfQuestion * level? 0 : 1)
        {
            if(level == 2)
            {
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 3)
            {
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 4)
            {
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 5)
            {
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
            }
            else if(level == 6)
            {
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 7)
            {
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 8)
            {
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 9)
            {
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
        }
    }
    else if(status == L2_TO_L10)
    {
        int q = 0;
        for(int level = 2; level <= 10 && q < numberOfQuestion; level += 10 * q <= numberOfQuestion * level? 0 : 1)
        {
            if(level == 2)
            {
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 3)
            {
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 4)
            {
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 5)
            {
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 6)
            {
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 7)
            {
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 8)
            {
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 9)
            {
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
            else if(level == 10)
            {
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 2)){q++; continue;}
            }
        }
    }
    else if(status == L3_TO_L10)
    {
        int q = 0;
        for(int level = 3; level <= 10 && q < numberOfQuestion; level += 10 * q <= numberOfQuestion * level? 0 : 1)
        {
            if(level == 3)
            {
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 4)
            {
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 5)
            {
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 6)
            {
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
            }
            else if(level == 7)
            {
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
            }
            else if(level == 8)
            {
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
            }
            else if(level == 9)
            {
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
            }
            else if(level == 10)
            {
                if(check(readyQuestionsList, map, 10)){q++; continue;}
                if(check(readyQuestionsList, map, 9)){q++; continue;}
                if(check(readyQuestionsList, map, 8)){q++; continue;}
                if(check(readyQuestionsList, map, 7)){q++; continue;}
                if(check(readyQuestionsList, map, 6)){q++; continue;}
                if(check(readyQuestionsList, map, 5)){q++; continue;}
                if(check(readyQuestionsList, map, 4)){q++; continue;}
                if(check(readyQuestionsList, map, 3)){q++; continue;}
            }
        }
    }
    else /*if(status == L1_TO_L10)*/
    {
        // same idea is implemented here
    }
    return questions;
}

private boolean check(List<Question> readyQuestionsList, Map<Integer, List<Question>> map, int level)
{
    if(map.get(level).size() > 0 && map.get(level).get(0) != null)
    {
        readyQuestionsList.add(map.get(level).remove(0));
        return true;
    }
    return false;
}

      

0


source


The simplest solution is to get all the questions that have a certain level depending on your mode, then you can sort this list, for example:



public static List<Question> getQuestions(List<Question> availableQuestions, 
     int quizDifficulty, int numberOfQuestion)
  {
      if(availableQuestions.size() < numberOfQuestion)
          throw NotEnoughQuestionsException();

      List<Question> questionsForUserMode = getQuestionsFromMode(
                        availableQuestions, quizDifficulty); 

       // sort this questionsForUserMode by Difficulty using comprator
  }

  // please put these magic numbers in constant fields or enum :)
  public static List<Question> getQuestionsFromMode (List<Question> questions,
             int mode) {
    if ( mode == 1 ) {
      return getQuestionsWithCertainLevel(questions, 1, 8);
    }
    else if ( moode == 2 ) {
      return getQuestionsWithCertainLevel(questions, 2, 9);
    }
    else
      return getQuestionsWithCertainLevel(questions, 3, 10);
  }

  private static List<Question> getQuestionsWithCertainLevel(
          List<Question> questions, int fromLeve, int toLevel) {
    List<Question> subQuestions = new ArrayList<Question>();

    for(Question question: questions) {
      if ( question.getDifficulty() >= fromLevel &&
           question.getDifficulty() <= toLevel ) {
        subQuestions.add(question);
      }
    }

    return subQuestions;
  }

      

+2


source


I would probably start by sorting the questions after the requirement, implementing different sorting algorithms for different levels (asking the most relevant questions for the current difficulty level first). Then just select the questions starting from the beginning of the list.

Something like that:

static final int[] easy = {1,2,3,4,5,6,7};
static final int[] medium = {3,4,5,6,7,8};
static final int[] hard = {4,5,6,7,8,9,10};

public static List<Question> getQuestions(List<Question> availableQuestions,
        int quizDifficulty, int numberOfQuestion){

     if(quizDifficulty == 0) // Easy
         Collections.sort(availableQuestions, new CompareQuestion(easy));
     else if(quizDifficulty == 2) // Hard
         Collections.sort(availableQuestions, new CompareQuestion(hard));
     else if(quizDifficulty == 1) // Normal
         Collections.sort(availableQuestions, new CompareQuestion(medium));

    int questions  = availableQuestions.size();
    return availableQuestions.subList(0, numberOfQuestion <= questions ? numberOfQuestion : questions);
}

static class CompareQuestion implements Comparator<Question>{

    final int[] compareList;

    public CompareQuestion(int[] compareList){
        this.compareList = compareList;
    }

    @Override
    public int compare(Question q1, Question q2) {
        boolean o1wanted = inList(q1.difficulty, compareList);
        boolean o2wanted = inList(q2.difficulty, compareList);

        if(o1wanted && o2wanted) return 0;

        if(o1wanted && !o2wanted) return -1;

        return 1;
    }

    public boolean inList(int a, int[] list){
        for(int i : list) if(a == i) return true;
        return false;
    }
}

      

+1


source


This is what I would do.

  • Cycle through the list of questions.
  • Based on the EASY: NORMAL: HARD condition, I would add them to the list
    • if (easy)
      • if (difficulty <8)
      • add to the list. .... Likewise for hard and normal.
  • In the end, I would sort the sub-list using one of the sorting algos.

In this case, if the given difficulty level is not met, then you have an empty list of questions (for example, for the one mentioned, for example, u, i.e. if all questions are of difficulty level 1 and you require difficult difficulties).

+1


source


I hope I read your question correctly. My suggestion is to do something in this format:

level|question

      

then when your question is inserted into an array, separate the level from the question.

I don't know Java, but here is an idea with objective-c

NSArray* components = [string componentsSeparatedByString:@"|"];
NSString* level = [components objectAtIndex:0];
NSString* question = [components objectAtIndex:1];

      

then compare if level == selectedLevel

I've always found this to be the easiest given your question and its level is side-by-side and no need to worry about the question and its level not the same.

0


source







All Articles