DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #16 - Number of People on the Bus

Today's challenge comes from aryan-firouzian on Codewars. You are to figure out how many people are on the bus given the following scenario:

There is a bus moving in the city, and it takes and drop some people at each bus stop.

You are provided with a list (or array) of integer arrays (or tuples). Each integer array has two items which represent the number of people that get on the bus (The first item) and the number of people that get off the bus (The second item) at a bus stop.

Your task is to return number of people who are still left on the bus after the last bus station (after the last array).

Good luck, and happy coding!


Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!

Latest comments (19)

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with Python:

def number(bus_stops):
    people = 0
    for items in bus_stops:
        people += items[0]
        people -= items[1]

    return people
Collapse
 
kvharish profile image
K.V.Harish

My solution in js

const totalPeopleInBus = (arr) => arr.reduce((total, [enter, exit]) => total + enter - exit, 0);
Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell:

type BusStop = (Int, Int)

getChange :: BusStop -> Int
getChange stop = fst stop - snd stop

peopleOnBus :: [BusStop] -> Int
peopleOnBus = sum . map getChange
Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

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.

Collapse
 
mattonem profile image
maxmattone

Smalltalk --- because it seems nobody did it yet:

result := arr sum: [ :aStop | aStop first - aStop second ]
Collapse
 
brightone profile image
Oleksii Filonenko

Elixir:

defmodule Bus do
  @spec passengers([{non_neg_integer, non_neg_integer}]) :: non_neg_integer
  def passengers(list) do
    list
    |> Enum.map(fn {went_on, went_off} -> went_on - went_off end)
    |> Enum.sum()
  end
end
Collapse
 
yzhernand profile image
Yozen Hernandez

Didn't see anyone else try it in C, so here's my solution in C:

#include "stdio.h"
#include "stdlib.h"

unsigned int bus( unsigned int ( *data )[2], size_t n ) {
    unsigned int sum = 0;

    for ( size_t i = 0; i < n; ++i ) {
        sum += ( data[i][0] - data[i][1] );
    }

    return sum;
}

int main( int argc, char const *argv[] ) {
    // Read in data, somehow
    printf( "Passengers remaining after last stop: %u\n",
      bus( ( unsigned int[][2] ){{1, 0}, {2, 1}, {5, 3}, {4, 2}}, 4 ) );
    // 6
    return 0;
}
Collapse
 
abumostafa profile image
Ahmed Abumostafa

JS

const stops = [
 [10, 0],
 [5, 3],
 [4, 4],
 [3, 8],
 [0, 2],
]

const inBus = stops.reduce(
(n, [gotIn, gotOut]) => n + gotIn - gotOut,
0
)
Collapse
 
tarptaeya profile image
Anmol Gautam

Haskell

passengers :: [(Int, Int)] -> Int
passengers xs = sum $ map (\x -> fst x - snd x) xs

main = print $ passengers [(1, 0), (2, 1), (5, 3), (4, 2)]
Collapse
 
yzhernand profile image
Yozen Hernandez

R

# Read in data, somehow. Likely into a data.frame
bus_data <- data.frame(on = c(1, 2, 5, 4), off = c(0, 1, 3, 2))
sum(bus_data$on - bus_data$off)
# [1] 6