Sha256: 0dc45ada885824001685c2d7a4938fadf8718d1470bc4cbbd5e706e2ad8d8608

Contents?: true

Size: 684 Bytes

Versions: 127

Compression:

Stored size: 684 Bytes

Contents

<?php

function rebase(int $fromBase, array $digits, int $toBase)
{
    if (empty($digits) || $digits[0] == 0 || $fromBase <= 1 || $toBase <= 1) {
        return null;
    }

    $decTotal = 0;
    $ordered = array_reverse($digits);
    for ($i = 0; $i < count($digits); $i++) {
        $decTotal += $ordered[$i] * pow($fromBase, $i);
        if ($ordered[$i] >= $fromBase || $ordered[$i] < 0) {
            return null;
        }
    }

    $newArr = [];
    while ($decTotal >= $toBase) {
        $remainder = $decTotal % $toBase;
        $newArr[] = $remainder;
        $decTotal = floor($decTotal / $toBase);
    }
    $newArr[] = $decTotal;

    return array_reverse($newArr);
}

Version data entries

127 entries across 127 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.77 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.76 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.75 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.74 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.73 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.72 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.71 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.70 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.69 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.68 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.67 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.66 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.65 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.64 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.63 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.62 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.61 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.60 tracks/php/exercises/all-your-base/example.php
trackler-2.2.1.59 tracks/php/exercises/all-your-base/example.php