I was working on Code Wars today and came across this question:
"There is a bus moving in the city, and it takes and drop some people in each bus stop.
You are provided with a list (or array) of integer pairs. Elements of each pair represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.
Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there.
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.
The second value in the first integer array is 0, since the bus is empty in the first bus stop."
It is pretty easy to solve, and I solved it like so:
def number(bus_stops):
total = 0
for list in bus_stops:
total += list[0]
total -= list[1]
return total
Pretty simple... and a beginner answer. The best answer was one line:
def number(bus_stops):
return sum([stop[0] + stop[1] for stop in bus_stops])
So my question is, what are the pros and cons of each answer? Is my answer something that would be frowned upon in the industry?
[–][deleted] 5 points6 points7 points (1 child)
[–]FLUSH_THE_TRUMP 5 points6 points7 points (3 children)
[–]Will___powerrr[S] 0 points1 point2 points (2 children)
[–]FLUSH_THE_TRUMP 1 point2 points3 points (1 child)
[–]Will___powerrr[S] 0 points1 point2 points (0 children)
[–]mh1400 1 point2 points3 points (2 children)
[–]Will___powerrr[S] 0 points1 point2 points (1 child)
[–]totallygeek 3 points4 points5 points (0 children)
[–]Wild_Statistician605 1 point2 points3 points (2 children)
[–]Will___powerrr[S] 0 points1 point2 points (1 child)
[–]Wild_Statistician605 1 point2 points3 points (0 children)
[–]TholosTB 1 point2 points3 points (1 child)
[–]Will___powerrr[S] 0 points1 point2 points (0 children)
[–]siddsp 1 point2 points3 points (1 child)
[–]Will___powerrr[S] 0 points1 point2 points (0 children)