當前位置: 首頁>>代碼示例>>PHP>>正文


PHP solution函數代碼示例

本文整理匯總了PHP中solution函數的典型用法代碼示例。如果您正苦於以下問題:PHP solution函數的具體用法?PHP solution怎麽用?PHP solution使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了solution函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的solution函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。