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!
Top comments (19)
JS:
Nice one, although you're missing the second argument to
reduce
which specifies the initial value fora
.According to the MDN Web Docs, that argument is optional.
Right :)
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.
PHP 7.4:
BTW, it seems today's post didn't make it into the series.
Update: It's been fixed.
Ruby 2.6
Elixir:
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.
Didn't see anyone else try it in C, so here's my solution in C:
Python:
JS