繁体   English   中英

如何根据长度不同的多个数组创建新的设定长度数组,以及根据重要性从每个较小的数组中取出的项目数

[英]How to create a new array of set length from multiple arrays of varying length, the number of items taken from each smaller arrays based on importance

我想从不同的“主题”数组中提取“问题”以建立测验。 主题是按照对用户的重要性进行选择的,每个主题中包含不同数量的问题,并根据他们的选择来填充20个问题测验的问题库。

在这20个问题中,评分最高的主题将有更多问题。 完成此操作的算法使我很难。

我尝试遍历父主题数组(包含所有具有name:str和question:[]属性的主题对象)。 然后使用平均数量(在主题数量奇数的情况下四舍五入)从所有主题的问题中创建20个问题所需的问题,我算出是否有开销。

即6个主题=每个主题的3.3个q,四舍五入为24个q =开销为4。

然后,我试图从最不重要的额定主题中抽出的问题数量减少开销。 数组中的最后一项是最不重要的。

function createWeightedQuestionBank(topicsArray) {

  // Returned Object of questions.
  let questionsBanks = [];

  // How many questions we want our quiz/question bank to have.
  const questionsLimit = 20;

  // The topics are passed in via the topics Array. Each Topic is an object
  // with {name: "topicName", questions: [q1,q2,q3,q4]}
  const topics = topicsArray;

  if (topics) {

  // The amount of topics in the topics array to be included in the questions banks.
  // TODO: inclusion and exclusion of topics.
  const topicsAmount = topics.length;

  // This is the average amount of questions that should be taken from each group

  // to make 20q's (rounded up).
  const questionsAverage = Math.ceil(questionsLimit / topicsAmount);

  // If an uneven number of topics is selected rounding up is necessary, but not
  // Leaves us with too many questions when the average amount is selected from each group.
  // 20questions/6topics = 4(rounded up from each group). 6*4 = 24.
  const projectedQuestions = (questionsAverage * topicsAmount);

  let overhead;

  if (questionsLimit > projectedQuestions ) {
    overhead = questionsLimit - projectedQuestions;

  } else {
    overhead = projectedQuestions - questionsLimit;
  }

  let overheadVariance = overhead;

  for ( let i = 0; i < topics.length; i++) {
    const topic = topics[i];
    let pullAmount;

    if (topics.length - (overhead - 1) <= i) {
      pullAmount = questionsAverage - (overheadVariance - overhead);
      overheadVariance++;
    } else {
      pullAmount = questionsAverage;
    }
    console.log(topics.length - (overhead));

主题数组是什么样的。


 this.topics = [
      {
        name: 'testy',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy1',
        isSelected: false,
        questions:
          [
            'one',
            'one',
            'one',
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'test2',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy3',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy4',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
      {
        name: 'testy5',
        isSelected: false,
        questions:
          [
            'one',
            'two',
            'three',
            'four',
          ]
    },
    ];

我不知道如何从最后3个主题问题数组中删除4个开销。 因此,我不会从三个最不重要的主题中抽取3 2 1,而不是抽取4(使平均数量达到20)。

我希望输出总计为4 4 4 3 2 120。但是它只记录2次六次。

实际上, 4+4+4+3+2+118 ,而不是20 =)

如果您需要4+4+4+4+3+1 ,可以尝试:

const amountOfTopics = topics.length;
const questionsPerTopic = Math.ceil(questionsLimit / amountOfTopics);
const overhead = questionsPerTopic * topics.length - questionsLimit;
const maxQuestionsCanBeRemoved = questionsPerTopic - 1;
const singleQuestionTopics = Math.floor(overhead / maxQuestionsCanBeRemoved);
const restQuestionsToBeRemoved = overhead - singleQuestionTopics * maxQuestionsCanBeRemoved;

const getAmountOfQuestions = topicIndex => {
  if (topicIndex >= amountOfTopics - singleQuestionTopics) {
    return 1;
  }

  if (topicIndex === amountOfTopics - singleQuestionTopics - 1) {
    return questionsPerTopic - restQuestionsToBeRemoved;
  }

  return questionsPerTopic;
};

topics.flatMap(
  (topic, index) => {
    const amount = getAmountOfQuestions(index);

    return topic.questions.slice(0, amount);
  }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM