英文:
PHP Complex Number Selection
问题
我已经编写了以下代码:
$row_array = range(1, $puzzle_size, 1);
$yes_array = array_rand($row_array, $total_numbers_to_display);
$puzzle_size
和 $total_numbers_to_display
的值取决于难度级别:
对于简单、中等和困难级别,$puzzle_size
将分别为 8、20 和 40,而 $total_numbers_to_display
为 3。
$yes_array
的值没有给我所需的输出。目前它给了我这个:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
我需要的是至少每个结果之间有一个数字间隔。
示例 1:
Array
(
[0] => 1
[1] => 3
[2] => 5
)
示例 2:
Array
(
[0] => 1
[1] => 4
[2] => 7
)
示例 3:
Array
(
[0] => 2
[1] => 4
[2] => 7
)
我不确定如何做到这一点。
英文:
I have wrote the following code:
$row_array = range( 1 , $puzzle_size , 1 );
$yes_array = array_rand( $row_array , $total_numbers_to_display );
The values of $puzzle_size and $total_numbers_to_display depends on the difficulty level:
$puzzle_size will be 8, 20 and 40 for easy, medium and hard levels. $total_numbers_to_display = 3
The value of $yes_array
is not giving me the output I need. What it gave me right now is:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
What I am requiring is for there to be at least 1 number gap between each result.
Example 1:
Array
(
[0] => 1
[1] => 3
[2] => 5
)
Example 2:
Array
(
[0] => 1
[1] => 4
[2] => 7
)
Example 3:
Array
(
[0] => 2
[1] => 4
[2] => 7
)
I am unsure of how to do this.
答案1
得分: 0
请按如下所示编辑您的代码:
<?php
// 需要的总项目数
$puzzle_size = 50;
// 要显示的总项目数
$total_numbers_to_display = 5;
// 根据 $puzzle_size 的值初始化数组项
$row_array = range(1, $puzzle_size, 1);
// 获取随机数组项并仅选择所需的项目数量的函数。array_rand() 仅返回数组的键,而不是值。
function randItems($sourceArray){
// 声明全局变量
global $total_numbers_to_display;
// 生成随机数组元素并返回
return array_rand($sourceArray, $total_numbers_to_display);
}
// 检查从上面的 randItems 函数生成的任何连续数字是否存在。如果存在,则从 $row_array 重新选择随机元素。
function get_non_consequtive_numbers($outputArray)
{
// 声明全局变量
global $row_array;
// 遍历以参数形式提供的数组
foreach($outputArray as $num){
// 检查生成的数组中是否存在任何前一个或下一个数字。如果存在,则从 $row_array 重新选择随机元素。
if( in_array(($num+1), $outputArray ) or in_array(($num-1), $outputArray ) )
{
// 调用 randItems 和递归函数进行检查。
return get_non_consequtive_numbers(randItems($row_array));
}
}
// 如果没有连续的数字,则数组有效并返回。
return $outputArray;
}
// 启动生成过程
$yes_array = get_non_consequtive_numbers(randItems($row_array));
// 打印从上述行获得的输出
print_r($yes_array);
?>
输出与问题中期望的一致。
英文:
Please edit your code as shown below :
<?php
// Total number of items required
$puzzle_size = 50;
// Total items to be displayed
$total_numbers_to_display = 5;
// Seed the array items as per $puzzle_size value
$row_array = range( 1 , $puzzle_size , 1 );
// Function to get the random array items and pick only the number of items required. array_rand() will return only the keys of the array, not the values.
function randItems($sourceArray){
// declare global variable
global $total_numbers_to_display;
// generate random array elements and return
return array_rand( $sourceArray , $total_numbers_to_display );
}
// Function to check whether any consequtive numbers are generated from randItems function above. If exists, then re-pick the random elements from $row_array
function get_non_consequtive_numbers($outputArray)
{
// declare global variable
global $row_array;
// iterate through the array provided as parameter
foreach($outputArray as $num){
// check if any previous or next number exists in the generated array. If exists, then re-pick the random elements from $row_array
if( in_array(($num+1) , $outputArray ) or in_array(($num-1), $outputArray ) )
{
// calling randItems and recursive function to check.
return get_non_consequtive_numbers(randItems($row_array));
}
}
// if no consequtive numbers found, then the array is valid and return.
return $outputArray;
}
// initiating the generation process
$yes_array = get_non_consequtive_numbers(randItems($row_array));
// printing the output obtained from above line
print_r($yes_array);
Output is as expected in the question.
答案2
得分: 0
以下是翻译好的代码部分:
The answer by `@PracticalOpportunist` got me on the right track. This is the function I wrote:
function get_non_consequtive_numbers( $puzzle_size , $total_numbers_to_display ) {
$non_consequtive_numbers = [ ];
$eligible_numbers = range( 1 , $puzzle_size , 1 );
while ( count( $non_consequtive_numbers ) < $total_numbers_to_display ) {
# get a random number
$candidate_number = rand( 1 , $puzzle_size );
# check if number is in $eligible_numbers
if ( in_array( $candidate_number , $eligible_numbers ) ) {
# add to $non_consequtive_numvbers
$non_consequtive_numbers[ count( $non_consequtive_numbers ) + 1 ] = $candidate_number;
# remove from $eligible_numbers
foreach ( range( ( $candidate_number - 1 ) , ( $candidate_number + 1 ) , 1 ) AS $ineligible_number ) {
if ( ( $key = array_search( $ineligible_number , $eligible_numbers ) ) !== false ) {
unset( $eligible_numbers[$key] );
}
}
}
}
return $non_consequtive_numbers;
}
希望这对你有帮助。
英文:
The answer by @PracticalOpportunist
got me on the right track. This is the function I wrote:
function get_non_consequtive_numbers( $puzzle_size , $total_numbers_to_display ) {
$non_consequtive_numbers = [ ];
$eligible_numbers = range( 1 , $puzzle_size , 1 );
while ( count( $non_consequtive_numbers ) < $total_numbers_to_display ) {
# get a random number
$candidate_number = rand( 1 , $puzzle_size );
# check if number is in $eligible_numbers
if ( in_array( $candidate_number , $eligible_numbers ) ) {
# add to $non_consequtive_numvbers
$non_consequtive_numbers[ count( $non_consequtive_numbers ) + 1 ] = $candidate_number;
# remove from $eligible_numbers
foreach ( range( ( $candidate_number - 1 ) , ( $candidate_number + 1 ) , 1 ) AS $ineligible_number ) {
if ( ( $key = array_search( $ineligible_number , $eligible_numbers ) ) !== false ) {
unset( $eligible_numbers[$key] );
}
}
}
}
return $non_consequtive_numbers;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论