An Array represents a sequence of cars in a sequence, either going East or West.
Count the pairs of cars that pass each other eventually.
For example, if we represent the cars as a string
EEWEW, then the number of passes would be 5: First 2 EEs pass both WWs, so 4 passes, and then the last E passes the last W, so in total 5 passes.
Test cases in Scala
assert(countPassingCars(List.empty) == 0) assert(countPassingCars(List(CarDirection.East)) == 0) assert(countPassingCars(List(CarDirection.West)) == 0) assert(countPassingCars(List(CarDirection.East, CarDirection.East)) == 0) assert(countPassingCars(List(CarDirection.West, CarDirection.West)) == 0) assert(countPassingCars(List(CarDirection.East, CarDirection.West)) == 1) assert( countPassingCars( List(CarDirection.East, CarDirection.East, CarDirection.West) ) == 2 ) assert( countPassingCars( List( CarDirection.East, CarDirection.East, CarDirection.West, CarDirection.West ) ) == 4 ) assert( countPassingCars( List( CarDirection.East, CarDirection.East, CarDirection.West, CarDirection.East, CarDirection.West ) ) == 5 )
Algorithm in Scala
27 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!
For every car going West, we need to know how many cars going East it will cross, and then combine that count to get the total number of passes.
Let's say if we have
EEWEW representing 5 cars in total, then we can represent them as prefix sums: Number of cars going East from position 0 is 1, from position 1 is 2, from position 2 is 2, and so forth; so
12233. (this is © from www.scala-algorithms.com)
Scala concepts & Hints
'collect' allows you to use Pattern Matching, to filter and map items.
Drop, Take, dropRight, takeRight
Scala's `drop` and `take` methods typically remove or select `n` items from a collection.
Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.
scanLeft and scanRight
Scala's `scan` functions enable you to do folds like foldLeft and foldRight, while collecting the intermediate results
Stack safety is present where a function cannot crash due to overflowing the limit of number of recursive calls.
This function will work for n = 5, but will not work for n = 2000 (crash with java.lang.StackOverflowError) - however there is a way to fix it :-)
In Scala Algorithms, we try to write the algorithms in a stack-safe way, where possible, so that when you use the algorithms, they will not crash on large inputs. However, stack-safe implementations are often more complex, and in some cases, overly complex, for the task at hand.
A state machine is the use of `sealed trait` to represent all the possible states (and transitions) of a 'machine' in a hierarchical form.
.viewsyntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.
'zip' allows you to combine two lists pair-wise (meaning turn a pair of lists, into a list of pairs)
It can be used over Arrays, Lists, Views, Iterators and other collections.