在不正确的测试数据上创建php数组
有人问我一个问题:在不正确的测试数据上创建php数组,php,arrays,Php,Arrays,有人问我一个问题: /** * return structure should be an array of hashmaps which looks something like * * array(3) { * [0] => * array(2) { * 'min' => int(500) * 'max' => int(750) * } * [1] => * array(2) { * 'min' => int(750) * 'max'
/**
* return structure should be an array of hashmaps which looks something like
*
* array(3) {
* [0] =>
* array(2) {
* 'min' => int(500)
* 'max' => int(750)
* }
* [1] =>
* array(2) {
* 'min' => int(750)
* 'max' => int(1000)
* }
* [2] =>
* array(2) {
* 'min' => int(1000)
* 'max' => int(1250)
* }
* }
*
* @param int $min
* @param int $max
* @param int $incrementStep
* @return array
*/
function getPriceRangeCollection($min, $max, $incrementStep) {
//code goes here
}
//test code - DO NOT REMOVE
$priceRangeCollection = getPriceRangeCollection(500, 5000, 250);
assert($priceRangeCollection[17]['min'] === 4750 && $priceRangeCollection[17]['max'] === 5000);
assert($priceRangeCollection[9]['min'] === 2750 &&
$priceRangeCollection[9]['max'] === 3000);
$priceRangeCollection2 = getPriceRangeCollection(1, 100, 3);
assert(count($priceRangeCollection2) === 33);
assert($priceRangeCollection2[32]['min'] === 96);
assert($priceRangeCollection2[32]['max'] === 99);
对于本练习,您需要创建函数getPriceRangeCollection,该函数将创建一个值对数组(如上面的示例所示),从最小范围到最大范围,以增量值为起点
到目前为止,我所拥有的对第一组测试数据有效,但对第二组测试数据无效。使用assert($priceRangeCollection2[32]['min']==96);比我的代码生成的值低1,即97
function getPriceRangeCollection($min, $max, $incrementStep) {
//code goes here
$myArray = array();
$index = 0;
for( $i = $min; $i < $max; $i = $i + $incrementStep ){
$myArray[ $index ] = array( 'min' => $i, 'max' => $i + $incrementStep );
$index ++;
}
return $myArray;
}
警告:assert():第50行C:\xampp\htdocs\InSite\php_exercise.php中的assert($priceRangeCollection2[32]['min']==96)失败
警告:assert():assert($priceRangeCollection2[32]['max']==99)在第51行的C:\xampp\htdocs\InSite\php_exercise.php中失败,如果您更改了测试数据,则代码可以正常工作,如下所示:
$priceRangeCollection2 = getPriceRangeCollection(0, 98, 3);
assert($priceRangeCollection2[32]['min'] === 96);
assert($priceRangeCollection2[32]['max'] === 99);
assert(count($priceRangeCollection2) === 33);
“这是创建一个值对数组(根据上面的示例)”-上面的示例是什么?编辑问题为您提供此信息!请提供
priceRangeCollection2
的转储。测试数据没有错。我认为他们不会让我更改测试数据,如果我按照你的建议更改它,那么我的原始代码也会工作。所以,我假设,测试数据是不正确的!是的,我也是这么想的
$priceRangeCollection2 = getPriceRangeCollection(0, 98, 3);
assert($priceRangeCollection2[32]['min'] === 96);
assert($priceRangeCollection2[32]['max'] === 99);
assert(count($priceRangeCollection2) === 33);