鍍金池/ 問答/PHP/ 多選題評分算法 求指導(dǎo)

多選題評分算法 求指導(dǎo)

多選題評分規(guī)則

每個正確選項都有得分權(quán)重,選正確一個給該正確選項得分權(quán)重的分值,選錯一個,則總分為0

假如:正確答案ABC ,總分10分,權(quán)重分別為 20% 40% 40%

提交答案 ABC 得 (20%+40%+40%)* 10 = 10分

提交答案 AB 得 (20%+40%)* 10 = 6分

提交答案 ABD 得 0分

集思廣益,如果可以請使用PHP作答

回答
編輯回答
脾氣硬

PHP面向?qū)ο蟀?/p>

/**
 * Created by: Singee77
 */

class Standard
{
    //答對全部題所得總分
    private $totalScore = 0;
    //標準答案
    private $standard = [];
    //提交答案
    private $answer = [];
    //所得總分
    private $getScore = 0;

    public function __construct($totalScore)
    {
        $this->setTotalScore($totalScore);
    }

    /**
     * @return int
     */
    public function getTotalScore()
    {
        return $this->totalScore;
    }

    /**
     * @param int $totalScore
     */
    public function setTotalScore($totalScore)
    {
        $this->totalScore = $totalScore;
    }


    /**
     * @param array $standard
     */
    public function setStandard($standard)
    {
        $this->standard = $standard;
    }

    /**
     * @return array
     */
    public function getStandard()
    {
        return $this->standard;
    }

    /**
     * @param $answer
     */
    public function checkStandard()
    {
        foreach ($this->answer as $each) {
            if (!$weight = $this->checkAnswer($each)) {
                //選錯一個總分為0
                $this->setGetScore(0);
                break;
            }
            //答對一個就追加分數(shù)
            $this->appendGetScore($this->getTotalScore() * $weight);
        }
    }

    /**
     * @param array $answer
     */
    public function setAnswer($answer)
    {
        $this->answer = $answer;
    }

    /**
     * @return array
     */
    public function getAnswer()
    {
        return $this->answer;
    }

    /**
     * @param $each
     * return $weight
     */
    private function checkAnswer($each)
    {
        return array_key_exists($each, $this->standard) ? $this->standard[$each] : 0;
    }

    /**
     * @param int $getScore
     */
    public function setGetScore($score)
    {
        $this->getScore = $score;
    }

    /**
     * @return int
     */
    public function getGetScore()
    {
        return $this->getScore;
    }

    /**
     * @param int $totalScore
     */
    public function appendGetScore($appendScore)
    {
        $this->getScore += $appendScore;
    }


}

//實例一個CHECK對象并設(shè)置總分
$std = new Standard(10);
//設(shè)置標準答案以及占比
$std->setStandard(['A' => 0.2, 'B' => 0.4, 'C' => 0.4]);
//設(shè)置答案
$std->setAnswer(['A', 'B']);
//計算分數(shù)
$std->checkStandard();
//獲取所得總分
$totalScore = $std->getTotalScore();
echo $totalScore;
2018年9月18日 14:48
編輯回答
離夢

不會php, 用一下js

// 選擇的選項
const select = ['a','b','d']
// 分數(shù) 
const score = {
    'a': 20,
    'b': 20,
    'c': 60,
    'd': 0
}
// 根據(jù)所選 得到 分數(shù)如:abd 的到 20,20,0
const a = select.map(x => score[x])
// 如果 a里面有 0 直接返回 0 否則 reduce 后得到 分數(shù)
total = a.includes(0) ? 0 : a.reduce((total, i) =>  total+ (i *0.1) , 0)
2017年10月6日 09:57
編輯回答
雨萌萌

假設(shè)你的答案有一些分隔符例如,,你有每道題的標準答案,可以如下:
/**
*@param $standard array 標準答案
*@param $answer string 提交答案
*/
function get_score($standard,$answer){

//分數(shù)
$score = 0;

$answer = explode(',', $answer);
if(!empty($answer)){
    foreach ($answer as $v) {
        if(isset($standard[$v])){
            $score+=(int)($standard[$v]*10);
        }else{
            $score = 0;
            break;
        }
    }
}
return $score;

}

var_dump(get_score([

'A' => 0.2,
'B' => 0.4,
'C' => 0.4

],'A,B'));

2018年4月10日 10:07
編輯回答
旖襯

思路,獲得提交與答案的交集,如果交集個數(shù)不等于提交個數(shù),則認為,存在選錯的值,如果交集個數(shù)等于提交個數(shù),遍歷求分即可

<?php

function multiSelectScore($answer, $wight, $allScore, $submit)
{
    $score = 0;
    $answerWight = array_combine($answer, $wight);
    // 計算交集
    $selects = array_intersect($answer, $submit);

    if(count($selects) === count($submit)) {
        // 遍歷交集
        foreach($selects as $key => $select) {
            $score += $answerWight[$select] * 0.01 * $allScore;
        }
    }
    return $score;
}

// 答案
$answer = ["A", "B", "C"];
// 權(quán)重
$wight = ["20","30","50"];
// 總分
$allScore = 10;
// 提交的選項
$submit = ["A","C"];
$score = multiSelectScore($answer, $wight, $allScore, $submit);

echo json_encode($score) . "\n";
2017年12月21日 00:33
編輯回答
熟稔
   function selectScore(preAnswerScore,answer) {
      let totalScore = 0;
      let noScoreKey = [];
      let hasNoScoreKey = false;
      for(let key in preAnswerScore) {
        if(preAnswerScore[key] == 0){
          noScoreKey.push(key);
        }
      }
      noScoreKey.map( item => {
        if(answer.includes(item)){
          hasNoScoreKey = true;
        }
      });
      if(hasNoScoreKey) {
        totalScore = 0;
      }else{
        answer.map (item => {
          totalScore += preAnswerScore[item];
        });
        totalScore /= 10;
      }
      return totalScore;
    } 
    let preAnswerScore = {a: 20, b: 40, c: 40, d: 0}; 
    let answer = ['c','b','d']
    console.log(selectScore(preAnswerScore, answer));
  }

js走一波,考慮到你其他選擇題也適用

2017年1月26日 19:27
編輯回答
尛曖昧
function calc($result,$answer,$score){
    $count = count($result);
    $d = array_diff($answer,$result);
    if($d){
        return  0;
    }
    $c = array_diff($result,$answer);
    $lost = count($c);
    if(!$lost){
         return  $score;
    }
    switch ($count) {
        case 1:
            $per = [1=>1];
            break;
        case 2:
            $per = [1=>0.4,2=>1];
            break;
        case 3:
            $per = [1=>0.2,2=>0.6,3=>1];
            break;
        default:
             $per = [1=>0.1,2=>0.5,3=>0.8,4=>1];
            break;
    }
    $true = $count - $lost ;
    return $per[$true]*$score;
    
}

$result = ['A','B','C'];
$answer = ['A','B'];
$score = 10;

echo calc($result,$answer,$score);
2018年9月18日 05:16
編輯回答
苦妄
function check_score(array $answer, array $correct, $total_score){
    $answer=array_flip($answer);
    $check=array_intersect_key($correct, $answer);
    return in_array($check, 0) ? 0 : array_sum($ckeck)*$total_score;
}

$answer為答案數(shù)組(['A', 'B', 'C']

$correct為正確答案數(shù)組(['A'=>0.2, 'B'=>0.4, 'C'=>0.4, 'D'=>0]

$total_score為總分

首先翻轉(zhuǎn)$answer讓選項成為key(['A'=>0, 'B'=>1, 'C'=>2]

然后根據(jù)key計算$correct$answer的交集$check$check的key和value都保留$correct的。

檢查$check的value中是否有0,有的話說明有錯誤答案,返回0分,否則計算$check的value的和,并乘以總分返回。

2018年4月15日 15:41
編輯回答
柚稚
function score(customAnswer, totalScore, weight) {
    let ret = 0
    for (let a of customAnswer) {
        if (!weight[a]) {
            return 0
        } else {
            ret += totalScore * weight[a]
        }
    }
    return ret
}

score('abc', 10, {a: 0.2, b: 0.4, c: 0.4})
2018年6月14日 12:00