Skip to content

Instantly share code, notes, and snippets.

@dmkuznetsov
Created June 24, 2019 20:34
Show Gist options
  • Save dmkuznetsov/361c2b91799bf832447cf8cdb9be69f6 to your computer and use it in GitHub Desktop.
Save dmkuznetsov/361c2b91799bf832447cf8cdb9be69f6 to your computer and use it in GitHub Desktop.
retention
<?php
//$retentionByMonth = [100, 20, 25, 20]; // 3
$retentionByMonth = [100, 80, 75, 72, 70]; // 4
//$usersByMonth = array_fill(0, 12 * 10, 4000);
$usersByMonth = array_fill(0, 12 * 10, 1000);
$fill = $retentionByMonth[count($retentionByMonth) - 1];
for ($i = 0, $c = count($usersByMonth) - count($retentionByMonth); $i < $c; $i++) {
$retentionByMonth[] = $fill;
}
$matrix = [];
for ($row = 0, $c = count($retentionByMonth); $row < $c; $row++) {
for ($column = 0; $column < $c; $column++) {
if ($column === $row) {
$matrix[$column][$row] = $usersByMonth[$row];
} else {
$matrix[$column][$row] = null;
}
}
}
for ($row = 0, $ic = count($matrix); $row < $ic; $row++) {
for ($column = 0, $jc = count($matrix[$column]); $column < $jc; $column++) {
if ($column === $row) {
break;
}
foreach ($matrix[$column] as $key => $value) {
if ($value !== null) {
break;
}
}
$matrix[$column][$row] = $matrix[$column][$key] / 100 * $retentionByMonth[$row - $key];
}
}
$sum = [];
for ($row = 0, $ic = count($matrix); $row < $ic; $row++) {
$sum[$row] = array_sum(array_column($matrix, $row));
}
echo implode(", ", $sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment