Created
May 14, 2023 20:24
-
-
Save oliveiraev/4b86857294ceb6684b0d79569be6f8bd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Calcula a participação total de cada contribuinte em uma vaquinha | |
* a partir de suas contribuições iniciais | |
* | |
* @param float $total O valor total da vaquinha | |
* @param float[] $iniciais A parcela inicial de cada contribuinte | |
* @return float[] A parcela final de cada contribuinte | |
*/ | |
public static function vaquinha(float $total, float ...$iniciais): array | |
{ | |
$parcela = ($total - array_sum($iniciais)) / sizeof($iniciais); | |
return array_map(fn(float $inicial): float => $inicial + $parcela, $iniciais); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use PHPUnit\Framework\TestCase; | |
require 'vaquinha.php'; | |
class VaquinhaTest extends TestCase | |
{ | |
public function testVaquinha(): void | |
{ | |
$this->assertSame(Stream::vaquinha(1000, 950, 0), [975.0, 25.0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment