• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP solution函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中solution函数的典型用法代码示例。如果您正苦于以下问题:PHP solution函数的具体用法?PHP solution怎么用?PHP solution使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了solution函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: run

 public function run($params, $resultExpected)
 {
     echo "\n";
     echo "***** DeldaTestUnit" . PHP_EOL;
     if (!isset($params)) {
         echo "Undefined input parameters!" . PHP_EOL;
     }
     if (!isset($resultExpected)) {
         echo "Undefined result expected!" . PHP_EOL;
     }
     $startTime = microtime(true);
     $resultDetected = solution($params);
     $endTime = microtime(true);
     $time = sprintf("%.06f", $endTime - $startTime);
     $memory = $this->getMemoryUsage();
     echo "Time: {$time}; Memory: {$memory}" . PHP_EOL;
     $typeOfDetected = gettype($resultDetected);
     $typeOfExpected = gettype($resultExpected);
     if ($typeOfDetected != $typeOfExpected) {
         $failure = true;
         $error_message = "The type expected is different from detected!";
     } else {
         if ($resultDetected !== $resultExpected) {
             $failure = true;
             $error_message = 'Wrong result value!';
         } else {
             $failure = false;
         }
     }
     if ($failure) {
         echo "FAILURE!" . PHP_EOL;
         echo $error_message . PHP_EOL;
     } else {
         echo "OK!" . PHP_EOL;
     }
     echo "Expected: '{$resultExpected}' ({$typeOfExpected}); returned: '{$resultDetected}' ({$typeOfDetected})'";
     echo PHP_EOL . PHP_EOL . PHP_EOL;
 }
开发者ID:dukle,项目名称:codility,代码行数:38,代码来源:Tests.class.php


示例2: elseif

            //            var_dump($i.'-'.$key);
            //            var_dump('['.$P[$i].','.$Q[$i].']');
            //            var_dump($prefix_sum[$key][$Q[$i]].'-'.$prefix_sum[$key][$P[$i]]);
            // Se eseguo la ricerca del minimo fattore su un solo nucleotide,
            // assegno automaticamente il fattore di impatto di quel nucleotide
            if ($Q[$i] == $P[$i]) {
                //                var_dump('###');
                $result[$i] = $impact_factor[$S[$Q[$i]]];
                break;
                // Se la differenza della somme dei prefissi è positiva,
                // un nuovo nucleotide è apparso nella sequenza analizzata
            } elseif ($P[$i] - 1 >= 0 && $prefix_sum[$key][$Q[$i]] - $prefix_sum[$key][$P[$i] - 1] > 0) {
                //                var_dump("###");
                $result[$i] = $impact_factor[$key];
                break;
                // Se la sequenza parte dal primo nucleotide,
                // non posso controllare se il valore della somma dei prefissi è aumentata (rispetto al valore precedente)
                // quindi controllo se la somma dei prefissi è maggiore di 0
            } elseif ($P[$i] - 1 < 0 && $prefix_sum[$key][$Q[$i]] > 0) {
                //                var_dump("###");
                $result[$i] = $impact_factor[$key];
                break;
            }
        }
    }
    //    var_dump();
    //    var_dump($result);
    return $result;
}
var_dump(solution($S, $P, $Q));
echo "\n";
开发者ID:dukle,项目名称:codility,代码行数:31,代码来源:GenomicRangeQuery.php


示例3: count

    $K %= count($A);
    for ($i = 0; $i < $K; $i++) {
        array_unshift($A, array_pop($A));
    }
    return $A;
}
function test($expected, $actual)
{
    if ($expected === $actual) {
        echo ".\n";
    } else {
        echo "expected: \n";
        print_r($expected);
        echo "actual: \n";
        print_r($actual);
    }
}
/*
For example, given array A = [3, 8, 9, 7, 6] and K = 3,
the function should return [9, 7, 6, 3, 8].
*/
test(array(), solution(array(), 0));
test(array(3, 8, 9, 7, 6), solution(array(3, 8, 9, 7, 6), 0));
test(array(6, 3, 8, 9, 7), solution(array(3, 8, 9, 7, 6), 1));
test(array(7, 6, 3, 8, 9), solution(array(3, 8, 9, 7, 6), 2));
test(array(9, 7, 6, 3, 8), solution(array(3, 8, 9, 7, 6), 3));
test(array(8, 9, 7, 6, 3), solution(array(3, 8, 9, 7, 6), 4));
test(array(3, 8, 9, 7, 6), solution(array(3, 8, 9, 7, 6), 5));
test(array(6, 3, 8, 9, 7), solution(array(3, 8, 9, 7, 6), 6));
echo "\n";
// https://codility.com/demo/results/trainingD9ATZ7-QFC/
开发者ID:memiux,项目名称:codility,代码行数:31,代码来源:CyclicRotation.php


示例4: solution

    return true;
}
function solution($n)
{
    $curN = 0;
    $curValue = 0;
    while ($curN != $n) {
        $curValue++;
        while (!checkIfPal($curValue) || !checkIfPal(decbin($curValue))) {
            $curValue++;
        }
        $curN++;
    }
    return $curValue;
}
echo solution(20);
//my version:
/* function solution ($a) {
	if ($a == 20) return 1758571; // сервер не справляется
    $count = 0;
    for ($i = 0; $count <= $a; $i++) {     	
		$rev_i = strrev ($i);
        $dec_i = decbin ($i);
        $rev_dec_i = strrev ($dec_i);
        if ($i == $rev_i && $dec_i == $rev_dec_i) $count++;    
	}      
	return $i-1;
    
}
    
} */
开发者ID:vsepod,项目名称:battle,代码行数:31,代码来源:double_base_palindromes.php


示例5: max

        }
        $best = max($best, $count + 1);
    }
    $result = $best + $K;
    return $result;
}
function test($expected, $actual)
{
    if ($expected === $actual) {
        echo ".\n";
    } else {
        echo "*error* expected: \n";
        var_dump($expected);
        echo "actual: \n";
        var_dump($actual);
    }
}
test(0, solution(array(1), 0));
test(1, solution(array(1), 1));
test(2, solution(array(2, 2), 2));
test(8, solution(array(4, 5, 2, 4, 4, 2, 2, 6), 8));
test(7, solution(array(4, 5, 2, 4, 4, 2, 2, 6), 7));
test(6, solution(array(42, 42, 42, 42, 42, 42), 6));
test(6, solution(array(1, 1, 1, 2, 1, 1), 2));
test(5, solution(array(1, 1, 2, 2, 2, 3, 4), 2));
test(4, solution(array(1, 1, 2, 2, 2, 3, 4), 1));
test(5, solution(array(3, 3, 3, 5, 5, 5, 1, 1, 5, 4), 2));
test(5, solution(array(5, 4, 3, 5, 3, 1, 5, 1, 3, 5), 2));
test(5, solution(array(1, 1, 3, 3, 3, 4, 5, 5, 5, 5), 2));
echo "\n";
// https://codility.com/demo/results/
开发者ID:memiux,项目名称:codility,代码行数:31,代码来源:Task2.php


示例6: foreach

    $currentMax = 0;
    foreach ($A as $v) {
        if ($v >= 1 && $v <= $N) {
            $counters[$v] += 1;
            if ($counters[$v] > $currentMax) {
                $currentMax = $counters[$v];
            }
        } else {
            if ($v === $N + 1) {
                $counters = array_fill(1, $N, $currentMax);
            }
        }
    }
    return array_values($counters);
}
$result = solution(5, [3, 4, 4, 6, 1, 4, 4]);
print_r($result);
/**
You are given N counters, initially set to 0, and you have two possible operations on them:

increase(X) − counter X is increased by 1,
max counter − all counters are set to the maximum value of any counter.
A non-empty zero-indexed array A of M integers is given. This array represents consecutive operations:

if A[K] = X, such that 1 ≤ X ≤ N, then operation K is increase(X),
if A[K] = N + 1 then operation K is max counter.
For example, given integer N = 5 and array A such that:

A[0] = 3
A[1] = 4
A[2] = 4
开发者ID:rooslunn,项目名称:codelity,代码行数:31,代码来源:04-MaxCounters.php


示例7: solution

<?php

// example
$X = 10;
$Y = 85;
$D = 30;
// solution: 3
function solution($X, $Y, $D)
{
    $mod = ($Y - $X) % $D;
    $diff = (int) (($Y - $X) / $D);
    if ($mod > 0) {
        $diff++;
    }
    return $diff;
}
echo solution($X, $Y, $D);
echo "\n";
开发者ID:dukle,项目名称:codility,代码行数:18,代码来源:FrogJmp.php


示例8: var_export

    var_export($a);
    echo "\n";
    var_export($arr_num);
    echo "\n";
    var_export($arr_repl);
    echo "\n";
    var_export($sum);
    echo "\n";
    var_export(is_int($is_int));
    echo "\n";
    //wrong tests?
    if ($a == 20839221428003.0) {
        return true;
    }
    if ($a == 280613327023445.0) {
        return false;
    }
    if ($a == 7537040646406564.0) {
        return true;
    }
    return $is_int == 0;
}
solution(280613327023445.0);
// END
/* Не прошёл тесты на значениях "20839221428003" (ожидалось true), "280613327023445" (ожидалось false), "7537040646406564" (ожидалось true).

Может я туплю, но вроде при значении "280613327023445" логика такая:
1. Количество цифр не чётное, значит берём только не чётные цифры в значении - "20137245"
2. Прогоняем по формулам: 2*2 + 0*2 + 1*2 + 3*2 + (7*2 - 9) + 2*2 + 4*2 + (5*2 - 9) = 4 + 0 + 2 + 6 + 5 + 4 + 8 + 1 = 30
3. 30 делится на 10, т.е. true. Тест ожидает false. 
На питоне решено*/
开发者ID:vsepod,项目名称:battle,代码行数:31,代码来源:credit_card_validator.php


示例9: var_dump

        // se devo calcolare il max_counter
        if ($A[$i] > $N) {
            $max_counter = $max_value;
            // se devo incrementare una variabile
        } else {
            if (isset($B[$A[$i] - 1])) {
                if ($B[$A[$i] - 1] > $max_counter) {
                    $B[$A[$i] - 1]++;
                } else {
                    $B[$A[$i] - 1] = $max_counter + 1;
                }
                if ($B[$A[$i] - 1] > $max_value) {
                    $max_value = $B[$A[$i] - 1];
                }
            }
        }
        //        var_dump($max_counter);
        //        var_dump($max_value);
        //        var_dump($B);
    }
    //    var_dump($B);
    for ($i = 0; $i < $N; $i++) {
        if ($B[$i] < $max_counter) {
            $B[$i] = $max_counter;
        }
    }
    //    var_dump($B);
    return $B;
}
var_dump(solution($N, $A));
echo "\n";
开发者ID:dukle,项目名称:codility,代码行数:31,代码来源:MaxCounters.php


示例10: solution

 */
function solution($A, $B, $K)
{
    if ($A > $B) {
        return 0;
    }
    $result = ceil($B - $A) / $K;
    if ($B % $K < $A % $K or $A % $K === 0) {
        $result++;
    }
    return (int) $result;
}
echo solution(6, 11, 2) . PHP_EOL;
echo solution(3, 10, 5) . PHP_EOL;
echo solution(0, 0, 11) . PHP_EOL;
var_dump(solution(1, 1, 11));
/**
Write a function:

function solution($A, $B, $K);

that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:

{ i : A ≤ i ≤ B, i mod K = 0 }

For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.

Assume that:

A and B are integers within the range [0..2,000,000,000];
K is an integer within the range [1..2,000,000,000];
开发者ID:rooslunn,项目名称:codelity,代码行数:31,代码来源:01-CountDiv.php


示例11: test

}
function test($expected, $actual)
{
    if ($expected === $actual) {
        echo ".\n";
    } else {
        echo "expected: \n";
        var_dump($expected);
        echo "actual: \n";
        var_dump($actual);
    }
}
/*
For example, given array A such that:

  A[0] = 9  A[1] = 3  A[2] = 9
  A[3] = 3  A[4] = 9  A[5] = 7
  A[6] = 9

the function should return 7, as explained in the example above.

Assume that:

N is an odd integer within the range [1..1,000,000];
each element of array A is an integer within the range [1..1,000,000,000];
all but one of the values in A occur an even number of times.
*/
test(0, solution(array()));
test(7, solution(array(9, 3, 9, 3, 9, 7, 9)));
echo "\n";
// https://codility.com/demo/results/training9ESR7D-VH4/
开发者ID:memiux,项目名称:codility,代码行数:31,代码来源:OddOccurrencesInArray.php


示例12: define

<?php

require './solution.php';
define("START_INDEX", 1);
define("END_INDEX", 100);
$map = [15 => 'FizzBuzz', 3 => 'Fizz', 5 => 'Buzz'];
echo solution(START_INDEX, END_INDEX, $map);
开发者ID:yumerov,项目名称:programming-tasks,代码行数:7,代码来源:main.php


示例13: solution

            if ($positions[$A[$i]] === 0) {
                $sum++;
            }
            $positions[$A[$i]] = 1;
        }
        if ($sum === $X) {
            return $i;
        }
    }
    return -1;
}
$result = solution(3, [1, 3, 1, 3, 2, 1, 3]);
echo "{$result}\n";
$result = solution(5, [1, 3, 1, 4, 2, 3, 5, 4]);
echo "{$result}\n";
$result = solution(2, [2, 2, 2, 2, 2]);
echo "{$result}\n";
/*
A small frog wants to get to the other side of a river. The frog is currently located at position 0, and wants to get to position X. Leaves fall from a tree onto the surface of the river.

You are given a non-empty zero-indexed array A consisting of N integers representing the falling leaves. A[K] represents the position where one leaf falls at time K, measured in seconds.

The goal is to find the earliest time when the frog can jump to the other side of the river. The frog can cross only when leaves appear at every position across the river from 1 to X. You may assume that the speed of the current in the river is negligibly small, i.e. the leaves do not change their positions once they fall in the river.

For example, you are given integer X = 5 and array A such that:

 A[0] = 1
 A[1] = 3
 A[2] = 1
 A[3] = 4
 A[4] = 2
开发者ID:rooslunn,项目名称:codelity,代码行数:31,代码来源:01-FrogRiverOne.php


示例14: test

that, given a non-empty zero-indexed array A of N integers,
returns the maximum number of flags that can be set on the peaks of the array.

For example, the following array A:

    A[0] = 1
    A[1] = 5
    A[2] = 3
    A[3] = 4
    A[4] = 3
    A[5] = 4
    A[6] = 1
    A[7] = 2
    A[8] = 3
    A[9] = 4
    A[10] = 6
    A[11] = 2
the function should return 3, as explained above.

Assume that:

N is an integer within the range [1..400,000];
each element of array A is an integer within the range [0..1,000,000,000].
*/
test(3, solution(array(1, 5, 3, 4, 3, 4, 1, 2, 3, 4, 6, 2)));
test(1, solution(array(1, 3, 2)));
test(2, solution(array(0, 0, 0, 0, 0, 1, 0, 1, 0, 1)));
test(2, solution(array(1, 0, 0, 0, 0, 1, 0, 1, 0, 1)));
echo "\n";
// https://codility.com/demo/results/training95EDNP-QYS/
开发者ID:memiux,项目名称:codility,代码行数:30,代码来源:Flags.php


示例15: solution

<?php

solution([1, 2, 4, 3, 5, 7]);
function solution($A)
{
    // write your code in PHP5.5
    sort($A);
    //var_dump($A);
    foreach ($A as $key => $value) {
        if ($value !== $key + 1) {
            echo $key + 1;
        }
    }
}
开发者ID:aaronleslie,项目名称:aaronunix,代码行数:14,代码来源:PermMissingElem.php


示例16: foreach

    foreach ($A as $d) {
        if ($d === 0) {
            $started++;
        } else {
            if ($d === 1 and $started > 0) {
                $result += $started;
            }
        }
    }
    if ($result > (int) 1000000000.0) {
        return -1;
    }
    return $result;
}
echo solution([0, 1, 0, 1, 1]) . PHP_EOL;
echo solution([0, 1, 0, 1, 1, 1, 0, 1, 0]) . PHP_EOL;
/**
* A non-empty zero-indexed array A consisting of N integers is given. The consecutive elements of array A represent consecutive cars on a road.

Array A contains only 0s and/or 1s:

0 represents a car traveling east,
1 represents a car traveling west.
The goal is to count passing cars. We say that a pair of cars (P, Q), where 0 ≤ P < Q < N, is passing when P is traveling to the east and Q is traveling to the west.

For example, consider array A such that:

A[0] = 0
A[1] = 1
A[2] = 0
A[3] = 1
开发者ID:rooslunn,项目名称:codelity,代码行数:31,代码来源:02-PassingCars.php


示例17: foreach

    foreach ($A as $ai) {
        if ($ai <= $N) {
            /*
             *  increase operation
             */
            // even the value
            if ($C[$ai - 1] < $minC) {
                $C[$ai - 1] = $minC;
            }
            // actual increase
            $C[$ai - 1]++;
            // calculate max
            if ($max < $C[$ai - 1]) {
                $max = $C[$ai - 1];
            }
        } elseif ($ai == $N + 1) {
            // do not actual increase to speed up operations
            $minC = $max;
        }
    }
    // even the values
    foreach ($C as &$ci) {
        if ($ci < $minC) {
            $ci = $minC;
        }
    }
    return $C;
}
$input = [3, 4, 4, 6, 1, 4, 4];
$output = solution(5, $input);
print_r($output);
开发者ID:ecommy,项目名称:codility-lessons,代码行数:31,代码来源:4_MaxCounters.php


示例18: explode

{
    $zeroes = explode("1", trim(decbin($N), "0"));
    return array_reduce($zeroes, "find_longest_gap", 0);
}
function test($expected, $actual)
{
    if ($expected === $actual) {
        echo ".\n";
    } else {
        echo "expected: " . $expected . "\n";
        echo "actual: " . $actual . "\n";
    }
}
/*
For example, given N = 1041 the function should return 5,
because N has binary representation 10000010001 and so its longest binary gap is of length 5.

N is an integer within the range [1..2,147,483,647].
*/
test(0, solution(1));
test(0, solution(6));
test(0, solution(15));
test(1, solution(20));
test(2, solution(328));
test(4, solution(529));
test(5, solution(1041));
test(2, solution(51712));
test(0, solution(2 ** 4));
test(0, solution(2 ** 10));
echo "\n";
// https://codility.com/demo/results/trainingXCVZ4B-4V4/
开发者ID:memiux,项目名称:codility,代码行数:31,代码来源:BinaryGap.php


示例19: test

function test($expected, $actual)
{
    if ($expected === $actual) {
        echo ".\n";
    } else {
        echo "expected: \n";
        var_dump($expected);
        echo "actual: \n";
        var_dump($actual);
    }
}
/*
For example, given:

  X = 10
  Y = 85
  D = 30
the function should return 3, because the frog will be positioned as follows:

after the first jump, at position 10 + 30 = 40
after the second jump, at position 10 + 30 + 30 = 70
after the third jump, at position 10 + 30 + 30 + 30 = 100
Assume that:

X, Y and D are integers within the range [1..1,000,000,000];
X ≤ Y.
*/
test(1, solution(1, 1, 1));
test(3, solution(10, 85, 30));
echo "\n";
// https://codility.com/demo/results/trainingTGK2HY-4EP/
开发者ID:memiux,项目名称:codility,代码行数:31,代码来源:FrogJmp.php


示例20: solution

<?php

solution([1, 3, 3, 1, 5]);
function solution($A)
{
    //var_dump($A);
    // write your code in PHP5.5
    $newa = $A;
    $counta = count($A);
    foreach ($newa as $key => $value) {
        for ($i = $key; $i < $counta; $i++) {
            if ($value == $newa[$i]) {
                unset($newa[$i]);
                unset($newa[$key]);
                if (count($newa) == 1) {
                    goto a;
                }
            }
        }
    }
    a:
    foreach ($newa as $key => $value) {
        echo $value;
    }
}
开发者ID:aaronleslie,项目名称:aaronunix,代码行数:25,代码来源:OddOccurrencesInArray.php



注:本文中的solution函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP solve函数代码示例发布时间:2022-05-23
下一篇:
PHP soliloquy函数代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap