Skip to content

Instantly share code, notes, and snippets.

@ajschlosser
Created September 5, 2014 17:30
Show Gist options
  • Save ajschlosser/91688d3b64a76bb2fa37 to your computer and use it in GitHub Desktop.
Save ajschlosser/91688d3b64a76bb2fa37 to your computer and use it in GitHub Desktop.
print_halves
function put_in_paragraph ($p) { return "<p>" . $p . "</p>"; }
function print_halves ($a) {
$total_length = 0;
for($i = 1; $i <= count($a); $i++) {
$total_length += strlen($a[$i]);
}
$first_half_length = 0;
$second_half_length = 0;
$second_half_index = 0;
$done = false;
for($i = 1; $i <= count($a); $i++) {
if (!$done) {
$first_half_length += strlen($a[$i]);
if ($first_half_length < ceil($total_length/2)) {
echo put_in_paragraph($a[$i]);
}
else {
echo put_in_paragraph($a[$i]);
$done = true;
$second_half_index = $i + 1;
$second_half_length = $total_length - $first_half_length;
}
}
}
if ($second_half_index > 0) {
for($i = $second_half_index; $i <= count($a); $i++) {
echo put_in_paragraph($a[$i]);
}
} else {
echo "There was a problem.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment