Skip to content

Instantly share code, notes, and snippets.

@oliveiraev
Created May 14, 2023 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oliveiraev/4b86857294ceb6684b0d79569be6f8bd to your computer and use it in GitHub Desktop.
Save oliveiraev/4b86857294ceb6684b0d79569be6f8bd to your computer and use it in GitHub Desktop.
<?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);
}
<?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