Skip to content

Instantly share code, notes, and snippets.

@markdboyd
Created June 8, 2016 23:57
Show Gist options
  • Save markdboyd/53cb85afdbd132e9a4a716fccdafc792 to your computer and use it in GitHub Desktop.
Save markdboyd/53cb85afdbd132e9a4a716fccdafc792 to your computer and use it in GitHub Desktop.
Code for America challenge - Summary of Building Code Violations in 2012
<?php
$csv_contents = file_get_contents('http://forever.codeforamerica.org/fellowship-2015-tech-interview/Violations-2012.csv');
$csv_data = explode(PHP_EOL, $csv_contents);
$results = array();
for ($i = 1; $i < count($csv_data); $i++) {
$row_data = explode(',', $csv_data[$i]);
$violation_category = trim($row_data[2]);
$violation_datetime = strtotime(trim($row_data[3]));
if (!empty($results[$violation_category])) {
$category_data = $results[$violation_category];
} else {
$category_data = array(
'total' => 0,
'max_date' => $violation_datetime,
'min_date' => $violation_datetime,
);
}
$category_data['total']++;
$category_data['max_date'] = ($category_data['max_date'] < $violation_datetime) ? $violation_datetime : $category_data['max_date'];
$category_data['min_date'] = ($category_data['min_date'] > $violation_datetime) ? $violation_datetime : $category_data['min_date'];
$results[$violation_category] = $category_data;
}
?>
<h1>Summary of building code violations in 2012</h1>
<table cellpadding="10">
<tr>
<th align="left">Category</th>
<th align="left">Earliest date</th>
<th align="left">Latest date</th>
<th align="left">Total violations</th>
</tr>
<?php foreach ($results as $category => $category_data): ?>
<tr>
<td><?php print $category; ?></td>
<td><?php print date('F j, Y', $category_data['min_date']); ?></td>
<td><?php print date('F j, Y', $category_data['max_date']); ?></td>
<td><?php print $category_data['total']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment