For the list of of integer arrays, we can use an actual HTML list with <li> that contain CSS variables with the number of people going up (--up) and down (--down) the bus. Then using CSS counters we can keep a count of the people who are on the bus at each moment.
This also checks to make sure that more people don't get off the stop than were on + got on that stop. This assumes no passengers were on the bus initially.
pubfnriders_left(bus_stops:&[(u32,u32)])->Result<u32,&str>{bus_stops.iter().fold(Ok(0),|sum,(on,off)|{letnew_sum=sum?+on;ifoff>&new_sum{Err("Somehow more people left the bus than got on")}else{Ok(new_sum-off)}})}#[cfg(test)]modtests{usecrate::*;#[test]fnit_returns_zero_for_an_empty_array(){assert_eq!(riders_left(&[]),Ok(0))}#[test]fnit_for_a_semi_complicated_array_example(){assert_eq!(riders_left(&[(10,0),(5,9),(6,6),(2,6)]),Ok(2));}#[test]fnit_for_a_semi_complicated_vec_example(){assert_eq!(riders_left(&vec![(10,0),(5,9),(6,6),(2,6)]),Ok(2));}#[test]fnit_for_a_broken_example(){assert_eq!(riders_left(&vec![(10,0),(5,9),(6,6),(2,12)]),Err("Somehow more people left the bus than got on"));}}
#! /usr/bin/perlusewarnings;usestrict;usefeatureqw{ say };useList::Utilqw{ sum };sub bus{sum(map$_->[0]-$_->[1],@_)}useTest::Moretests=>1;my@people=([10,0],[5,3],[12,1],[1,3],[0,3],[1,7]);isbus(@people),12;
BTW, it seems today's post didn't make it into the series. Update: It's been fixed.
Didn't see anyone else try it in C, so here's my solution in C:
#include "stdio.h"
#include "stdlib.h"
unsignedintbus(unsignedint(*data)[2],size_tn){unsignedintsum=0;for(size_ti=0;i<n;++i){sum+=(data[i][0]-data[i][1]);}returnsum;}intmain(intargc,charconst*argv[]){// Read in data, somehowprintf("Passengers remaining after last stop: %u\n",bus((unsignedint[][2]){{1,0},{2,1},{5,3},{4,2}},4));// 6return0;}
19 yo student *and* a full-time developer.
Building with Rails currently, exploring new frontiers.
I enjoy functional programming, Linux, FOSS, refactoring and mentoring :)
Senior undergraduate at IIT Gandhinagar, Developer of Crusta Browser and Truck programming language +other cool projects, GSoC 2018 participant under KDE
JS:
Nice one, although you're missing the second argument to
reducewhich specifies the initial value fora.According to the MDN Web Docs, that argument is optional.
Right :)
CSS (kind of):
For the list of of integer arrays, we can use an actual HTML list with
<li>that contain CSS variables with the number of people going up (--up) and down (--down) the bus. Then using CSS counters we can keep a count of the people who are on the bus at each moment.Live demo on CodePen.
PHP 7.4:
Rust Solution
This also checks to make sure that more people don't get off the stop than were on + got on that stop. This assumes no passengers were on the bus initially.
BTW, it seems today's post didn't make it into the series.
Update: It's been fixed.
Ruby 2.6
Didn't see anyone else try it in C, so here's my solution in C:
Elixir:
Python:
R
JS
Here is the simple solution with Python:
Smalltalk --- because it seems nobody did it yet:
Haskell:
My solution in js
Haskell