Sha256: e55c9210ec6f4302d55dcb7b2a85ddc373d9b6f4a9f25ec257d4c8376a490f24

Contents?: true

Size: 379 Bytes

Versions: 396

Compression:

Stored size: 379 Bytes

Contents

<?php

function toDecimal($ternaryString, $base = 3)
{
    if (!preg_match('/^[0-2]+$/', $ternaryString)) {
        return 0;
    }

    $decimalNumber = 0;
    $numbers = str_split($ternaryString);
    $maxPower = count($numbers) - 1;
    for ($i = 0; $i <= $maxPower; ++$i) {
        $decimalNumber += $numbers[$maxPower-$i]*pow($base, $i);
    }

    return $decimalNumber;
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/php/exercises/trinary/example.php
trackler-2.2.1.179 tracks/php/exercises/trinary/example.php
trackler-2.2.1.178 tracks/php/exercises/trinary/example.php
trackler-2.2.1.177 tracks/php/exercises/trinary/example.php
trackler-2.2.1.176 tracks/php/exercises/trinary/example.php
trackler-2.2.1.175 tracks/php/exercises/trinary/example.php
trackler-2.2.1.174 tracks/php/exercises/trinary/example.php
trackler-2.2.1.173 tracks/php/exercises/trinary/example.php
trackler-2.2.1.172 tracks/php/exercises/trinary/example.php
trackler-2.2.1.171 tracks/php/exercises/trinary/example.php
trackler-2.2.1.170 tracks/php/exercises/trinary/example.php
trackler-2.2.1.169 tracks/php/exercises/trinary/example.php
trackler-2.2.1.167 tracks/php/exercises/trinary/example.php
trackler-2.2.1.166 tracks/php/exercises/trinary/example.php
trackler-2.2.1.165 tracks/php/exercises/trinary/example.php
trackler-2.2.1.164 tracks/php/exercises/trinary/example.php
trackler-2.2.1.163 tracks/php/exercises/trinary/example.php
trackler-2.2.1.162 tracks/php/exercises/trinary/example.php
trackler-2.2.1.161 tracks/php/exercises/trinary/example.php
trackler-2.2.1.160 tracks/php/exercises/trinary/example.php