# A History Lesson

The Pony Express was a mail service operating in the US in 1859-60.





It reduced the time for messages to travel between the Atlantic and Pacific coasts to about 10 days, before it was made obsolete by the transcontinental telegraph.

# How it worked

There were a number of *stations*, where:


* The rider switched to a fresh horse and carried on, or

* The mail bag was handed over to the next rider


# Kata Task


`stations` is a list/array of distances (miles) from one station to the next along the Pony Express route.

Implement the `riders` method/function, to return how many riders are necessary to get the mail from one end to the other.

##  Missing rider

In this version of the Kata a rider may go missing. In practice, this could be for a number of reasons - a lame horse, an accidental fall, foul play... 

After some time, the rider's absence would be noticed at the **next** station, so the next designated rider from there would have to back-track the mail route to look for his missing colleague. The missing rider is then safely escorted back to the station he last came from, and the mail bags are handed to his rescuer (or another substitute rider if necessary). 

`stationX` is the number (2..N) of the station where the rider's absence was noticed.

# Notes

* Each rider travels as far as he can, but never more than 100 miles.

# Example

GIven

* `stations = [43, 23, 40, 13]`

* `stationX = 4`

So

`S1` ...  ... 43 ...  ... `S2` ...  ... 23 ...  ... `S3` ...  ... 40 ...  ... `S4` ...  ... 13 ...  ... `S5`

* Rider 1 gets as far as Station S3
* Rider 2 (at station S3) takes mail bags from Rider 1
* Rider 2 never arrives at station S4
* Rider 3 goes back to find what happened to Rider 2
* Rider 2 and Rider 3 return together back to Station S3
* Rider 3 takes mail bags from Rider 2
* Rider 3 completes the journey to Station S5

**Answer:** 
3 riders




*Good Luck.
DM.*

---

See also
* The Pony Express
* The Pony Express (missing rider)

Difficulty: EASY
Starter code: def riders(stations, station_x):
	

Solutions:
def riders(stations, lost):
	stations = stations[:lost - 1] + stations[lost - 2:]
	(rider, dist) = (1, 0)
	for (i, d) in enumerate(stations):
		rider += (dist + d > 100) + (i == lost - 2)
		dist = dist * (dist + d <= 100 and i != lost - 2) + d
	return rider


LIMIT = 100

def riders(stations, station_x):
	riders = 1
	rode = 0
	for (station, distance) in enumerate(stations, 2):
		if rode + distance > LIMIT:
			riders += 1
			rode = 0
		if station == station_x:
			riders += 1
			if distance > LIMIT / 2:
				riders += 1
				rode = distance
			else:
				rode = 2 * distance
		else:
			rode += distance
	return riders


def riders(s, x):
	(station, i, riders, p) = (1, 0, [0], 0)
	while station <= len(s) + 1:
		if riders[p] + s[i] <= 100:
			riders[p] += s[i]
		else:
			riders.append(0)
			p += 1
			continue
		station += 1
		i += 1
		if station == x:
			riders.append(0)
			p += 1
			i -= 1
			riders[p] += s[i]
	return len(riders)


def riders(sts, x):
	(st, x, back, n) = (0, x - 1, 0, 1)
	while True:
		(remain, back) = (100 - back, 0)
		while remain >= sts[st]:
			remain -= sts[st]
			st += 1
			if st == x:
				st -= 1
				x = -1
				back = sts[st]
				break
			elif st == len(sts):
				return n
		n += 1


def riders(stations, station_x):
	riders = 2
	miles = 0
	for i in range(len(stations)):
		if i == station_x - 2:
			miles = stations[i]
		miles += stations[i]
		if miles > 100:
			riders += 1
		if i == len(stations) - 1:
			break
		if miles + stations[i + 1] > 100:
			riders += 1
			miles = 0
	return riders


def riders(S, x):
	R = []
	for (i, s) in enumerate(S):
		R += R and R[-1] + s <= 100 and [R.pop() + s] or [s]
		if i + 2 == x:
			R += 100 < s * 2 and [s] * 2 or [s * 2]
	return len(R)


def riders(stations, station_x):
	stations.insert(station_x - 2, stations[station_x - 2])
	riders = 1
	miles = 0
	for (i, m) in enumerate(stations):
		miles += m
		if miles > 100:
			miles = m
			riders += 1
		if i == station_x - 2:
			miles = m
			riders += 1
	return riders


def riders(stations, station_x):
	r = 1
	d = 0
	for (i, e) in enumerate(stations, 1):
		d += e
		if i + 1 == station_x:
			r += 1
			d = 2 * e
			if d > 100:
				d = e
				r += 1
		if i < len(stations):
			temp = d + stations[i]
			if temp > 100:
				r += 1
				d = 0
	return r


def riders(stations, station_x):
	riders = 1
	index = 1
	r_miles = 0
	for s_distance in stations:
		index += 1
		r_miles += s_distance
		if r_miles > 100:
			riders += 1
			r_miles = s_distance
		if station_x == index:
			riders += 1
			r_miles = s_distance * 2
			if r_miles > 100:
				riders += 1
				r_miles = s_distance
	return riders