Example #1
0
#!/usr/bin/php
<?php 
$lines = file('php://stdin');
$lines = array_map('trim', $lines);
$testcases = $lines[0];
for ($i = 0; $i < $testcases; $i++) {
    $tests[] = array('max' => $lines[4 * $i + 1], 'total' => $lines[4 * $i + 2], 'stations' => $lines[4 * $i + 3], 'dists' => $lines[4 * $i + 4]);
}
foreach ($tests as $test) {
    echo getStops($test) . PHP_EOL;
}
function getStops($test)
{
    $max = $test['max'];
    $total = $test['total'];
    $stations = $test['stations'];
    $dists = explode(' ', $test['dists']);
    if ($max >= $total) {
        return 'No stops';
    }
    $out = '';
    $distalready = 0;
    for ($i = 0; $i < $stations - 1; $i++) {
        if ($dists[$i + 1] - $distalready > $max) {
            $out .= $dists[$i] . ' ';
            $distalready = $dists[$i];
        }
    }
    return trim($out);
}
Example #2
0
    $csv .= $stop_lat . ",";
    $csv .= $stop_lon . ",";
    $csv .= $platform_code . ",";
    $csv .= $parent_station . ",";
    $csv .= $location_type;
    appendCSV($dist, $csv);
}
function appendCSV($dist, $csv)
{
    file_put_contents($dist, trim($csv) . PHP_EOL, FILE_APPEND);
}
// header CSV
$header = "stop_id,stop_name,stop_lat,stop_lon,platform_code,parent_station,location_type";
appendCSV($dist, $header);
// content
$stops = getStops()->{"@graph"};
for ($i = 0; $i < count($stops); $i++) {
    $stop = $stops[$i];
    $parent_id = $stop->{"@id"};
    if (preg_match("/NMBS\\/(\\d+)/i", $parent_id, $matches)) {
        $stationId = $matches[1];
        $parent_stop_id = 'stops:' . $matches[1];
    }
    $parent_name = $stop->{"name"};
    $parent_lat = $stop->{"latitude"};
    $parent_lon = $stop->{"longitude"};
    // what we describe are all parent_stations, which have "platforms"
    // Because we don't always the platforms of a trip, we need to add these parent_stations as normal stops
    $parent_station_type = 1;
    // Add parent station as stop
    addStop($parent_stop_id, $parent_name, $parent_lat, $parent_lon, '', '', $parent_station_type);