DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #87 - Pony Express

The Pony Express was a mail service that operated from 1859-60 in the United States. It was made completely obsolete by the intercontinental telegraph.

There were a number of stations where a rider would stop to switch to a fresh horse, or pass the mail bag along to another rider to continue the trek.

stations is an array/list of distances in miles from one station to the next along the route.

Implement a riders function, to return how many riders it would take to get from point A to point B. Riders will never agree to riding more than 100 miles.

example: stations = [43, 23, 40, 13]
output: 2 riders

Good luck!


This challenge comes from dinglemouse on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

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

Top comments (15)

Collapse
 
mbaas2 profile image
Michael Baas

The question is a bit unclear: it speaks about getting from A to B and then lists 4 points. I assume that A is the first and B the last.
So then...

APL (Dyalog APL)

The complete program:

      riders←{⌈.01×+/⍵}

Testing...

      riders  43 23 40 13
2

Try it online!

Collapse
 
kvharish profile image
K.V.Harish • Edited

My solution in js

const riders = (stations = []) => {
  let numOfRiders = + !!stations.reduce((acc, distance) => acc + distance, 0), totalDistance = 0;
  stations.forEach((distance) => {
    if(totalDistance + distance <= 100) {
      totalDistance += distance;
    }
    else {
      numOfRiders++;
      totalDistance = distance;
    }
  });
  return numOfRiders;
};

console.log(riders()); // 0
console.log(riders([])); // 0
console.log(riders([0, 0, 0, 0, 0])); // 0
console.log(riders([43, 23, 40, 13])); // 2
console.log(riders([100, 100, 100, 50, 50, 30, 20, 25, 25, 25, 25])); // 6
Enter fullscreen mode Exit fullscreen mode
Collapse
 
blessedtawanda profile image
Blessed T Mahuni

Nice your solution is a little bit on an expert level here my JS solution

function riders(stations) {
  // check if stations is defined
  if(stations) {
    let distanceRode = 0
    let numberOfRiders = 0

    stations.forEach(distance => {
      distanceRode += distance
    });

    //Check if there is anything passed within the array 
    if(distanceRode == 0)
      return 0

    //modulus division
    let i = distanceRode%100

    //check if distance is greater than 100
    if (distanceRode >= 100) {
      if(i == 0) {
       /* 
         if distance is greater than 100 
         and modulus division is 0 then simply divide by 
         100 to get distance rode
       */
       numberOfRiders = distanceRode/100
      } else {
        /* 
         if distance is greater than 100 
         and modulus division is not 0 then simply divide by 
         100 to get number of riders and set the remainder as an extra rider
      */
        numberOfRiders = Math.floor(distanceRode/100) + 1
      }
    }
    return numberOfRiders
  } else {
    return 0
  }
}

console.log(riders()); // 0
console.log(riders([])); // 0
console.log(riders([0, 0, 0, 0, 0])); // 0
console.log(riders([43,23,40,13])) //2
console.log(riders([34,56,12,67,23,90,56])) //4
console.log(riders([56,67,100,345,12,45])) //7
console.log(riders([100, 100, 100, 50, 50, 30, 20, 25, 25, 25, 25])); //6

Collapse
 
kvharish profile image
K.V.Harish

Good approach. Taking the total and dividing by 100 to see how many riders.

Collapse
 
ddowy profile image
ddowy

is the + !!stations just to add one?

Collapse
 
savagepixie profile image
SavagePixie

Gotta love that .reduce()

const riders = stations => {
  const total = stations.reduce((a, b) => 
    a.currentMiles + b <= 100 ? { currentMiles: a.currentMiles + b, riders: a.riders }
      : { currentMiles: b, riders: a.riders + 1 }, 
  { currentMiles: 0, riders: 1 })
  return total.riders
}

It could also be done as a one-liner with something like this, but it's a lot less clear:

const riders = s => s.reduce(([a, r], d) => a + d <= 100 ? [a+d, r] : [d, r+1], [0,1])[1]
Collapse
 
avalander profile image
Avalander

My challenge is writing this is Haskell on the phone without running the code.

calc_riders :: [Int] -> Int
calc_riders = fst . foldl count (1, 0)

count :: (Int, Int) -> Int -> (Int, Int)
count (r, m) x
  | m + x > 100 = (r + 1, m)
  | otherwise   = (r, m + x)
Collapse
 
larisho profile image
Gab

Clojure:

(def stations [43 23 40 13])

(defn riders [lst]
  "Calculate the number of riders it takes to go to each station"
  (loop [stations lst
         rider 1
         traveled 0]
    (cond
      (empty? stations) rider
      (> (+ traveled (first stations)) 100) (recur (cons (- (+ traveled (first stations)) 100)  (rest stations)) (inc rider) 0)
      :else (recur (rest stations) rider (+ traveled (first stations))))))

(riders stations)
;; 2
Collapse
 
ap13p profile image
Afief S

In reason:

let countRiders = (stations: list(int)) => {
  let total = List.fold_right((acc, it) => { acc +. it }, List.map(float, stations), 0.0);
  int_of_float(ceil(total /. 100.0))
}

Here on sketch: sketch.sh/s/uYFZAhtZSc2Ya4r6t1GNKM/

Collapse
 
aminnairi profile image
Amin

Elm

module PonyExpress exposing (riders)


riders : List Int -> Int
riders stations =
    ceiling <| toFloat (List.sum stations) / 100

Tests

module PonyExpressTest exposing (suite)

import Expect exposing (equal)
import PonyExpress exposing (riders)
import Test exposing (Test, describe, test)


suite : Test
suite =
    describe "Pony express"
        [ test "It should return 2 for a total ride of 119 miles" <|
            \_ -> equal 2 <| riders [ 43, 23, 40, 13 ]
        , test "It should return 1 for a total ride of 100 miles" <|
            \_ -> equal 1 <| riders [ 43, 23, 34 ]
        , test "It should return 0 for a total ride of 0 miles" <|
            \_ -> equal 0 <| riders [ 0 ]
        ]

Playground

Hosted here on Ellie App.

Collapse
 
jaumevn profile image
Jaume Viñas Navas

This is a Swift solution:

func riders(stations: [Int]) -> Int {
    var n = 1
    var d = 0

    for distance in stations {
        if (d + distance > 100) {
            n += 1
            d = distance
        } else {
            d += distance
        }
    }

    return n
}
Collapse
 
benstigsen profile image
Benjamin Stigsen

In Python:

from math import ceil

def countRidersNeeded(stations, max_distance):
    distance = 0
    for i in stations:
        distance += i

    return ceil(distance / max_distance)

print(countRidersNeeded([43, 23, 40, 13], 100))
Collapse
 
gustavosfq profile image
Gustavo Flores

My code in JS

const riders = (array = []) => Math.ceil(array.reduce((a, b) => a + b, 0) / 100);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
peledzohar profile image
Zohar Peled

c#:

int Riders(int[] stations)
{
    var totalSum = stations.Sum();
    return (totalSum / 100) + (totalSum % 100 == 0 ? 0 : 1);
}