Considering the following List in Scala :
List(4, 5, 6, 7, 8, 12, 13, 14, 17, 23, 24, 25)
I want to get the output
List(List(4,8), List(12,14), List(17), List(23,25))
I have this answer Scala List function for grouping consecutive identical elements
But it is working for grouping identical elements in the same List.
How to extend this solution to resolve my current problem?
I have tried this code
def sliceByRange[A <% Int](s: List[A]): List[List[A]] = s match {
case Nil => Nil
case x :: xs1 =
val (first, rest) = s.span(y => y - x == 1)
first :: sliceByRange(rest)
}
But it is not working.
Tail-recursive solution
Code
Note that you could also use List[(Int,Int)] as result type instead of List[List[Int]]. This would reflect the fact that the result is a List of ranges more appropriately. Of course then you couldn't turn List(x,x) into List(x) for singleton ranges. But I expect that this will come back to bite you later anyway.
import scala.annotation.tailrec
#tailrec
def split(in: List[Int], acc: List[List[Int]] = Nil): List[List[Int]] = (in,acc) match {
case (Nil,a) => a.map(_.reverse).reverse
case (n :: tail, (last :: t) :: tt) if n == last + 1 => split(tail, (n :: t) :: tt)
case (n :: tail, a ) => split(tail, (n :: n :: Nil) :: a)
}
val result = split(List(4, 5, 6, 7, 8, 12, 13, 14, 17, 23, 24, 25))
println(result)
println("removing duplicates:")
println(result.map{
case List(x,y) if x == y => List(x)
case l => l
})
Output
List(List(4, 8), List(12, 14), List(17, 17), List(23, 25))
removing duplicates:
List(List(4, 8), List(12, 14), List(17), List(23, 25))
Here is another example:
val myList = List(4, 5, 7, 8, 12, 13, 14, 17, 23, 24, 25)
def partition(list: List[Int]): (List[Int], List[Int]) = {
val listPlusOne = (list.head - 1 :: list) // List(1,2,5) => List(0, 1, 2, 5)
val zipped = list zip listPlusOne // zip List(1,2,5) with List(0, 1, 2, 5) => List((1,0), (2,1), (5,2))
val (a, b) = zipped span { case (a, b) => b + 1 == a } // (List((1,0), (2,1)), List((5,2)))
(a.map(_._1), b.map(_._1)) // (List(1, 2),List(5))
}
def group(list: List[Int]): List[List[Int]] = list match {
case Nil => Nil
case _ =>
val (a, b) = partition(list)
val listA = List(List(a.head, a.last).distinct) // remove middle numbers..
val listB = if (b.isEmpty) Nil else group(b)
listA ++ listB
}
println(group(myList))
A bit more complicated, but it works...
Paraphrasing the answer of the question you referenced:
def split(list: List[Int]) : List[List[Int]] = list match {
case Nil => Nil
case h::t =>
val segment = list.zipWithIndex.takeWhile { case (v, i) => v == h+i }.map(_._1)
List(h, segment.last).distinct :: split(list drop segment.length)
}
Using zipWithIndex to check for each element whether it's exactly the "next" integer (number should increase "together" with the index). Then - taking only the "boundaries" of the segment and recursively moving on to the rest of the list.
My solution:
def sliceByRange(items: List[Int]) =
items.sorted.foldLeft(Nil: List[List[Int]]) {
case (initRanges :+ (head :: Nil), next) if head == next - 1 =>
initRanges :+ (head :: next :: Nil) // append element to the last range
case (initRanges :+ (head :: last :: Nil), next) if last == next - 1 =>
initRanges :+ (head :: next :: Nil) // replace last range
case (ranges, next) =>
ranges :+ (next :: Nil) // add new range
}
Usage:
sliceByRange(List(1, 2, 3, 5, 8, 9, 12, 13, 14, 19))
// List(List(1, 3), List(5), List(8, 9), List(12, 14), List(19))
If you wish to keep middle values you can use the following example:
def makeSegments(items: List[Int]) =
items.sorted.foldLeft(Nil: List[List[Int]]) {
case (initSegments :+ lastSegment, next) if lastSegment.last == next - 1 =>
initSegments :+ (lastSegment :+ next)
case (segments, next) =>
segments :+ (next :: Nil)
}
Usage:
makeSegments(List(1, 2, 3, 5, 8, 9, 12, 13, 14, 19))
// List(List(1, 2, 3), List(5), List(8, 9), List(12, 13, 14), List(19))
When range size at least 3 elements:
def sliceByRange3elements(items: List[Int]) =
items.sorted.foldLeft(Nil: List[List[Int]]) {
case (initRanges :+ (head :: last :: Nil), next) if last == next - 1 =>
initRanges :+ (head :: next :: Nil) // replace last range
case (initRanges :+ (ll :: Nil) :+ (l :: Nil), next) if ll == next - 2 && l == next - 1 =>
initRanges :+ (ll :: next :: Nil) // make new range
case (ranges, next) =>
ranges :+ (next :: Nil)
}
Usage (note that (8,9) are not range now):
sliceByRange3elements(List(1, 2, 3, 5, 8, 9, 12, 13, 14, 19))
// List(List(1, 3), List(5), List(8), List(9), List(12, 14), List(19))
You can define printRanges method to more visual output:
def printRanges(ranges: List[List[Int]]) =
ranges.map({
case head :: Nil => head.toString
case head :: last :: Nil => s"$head-$last"
case _ => ""
}).mkString(",")
printRanges(
sliceByRange(List(1, 2, 3, 5, 8, 9, 12, 13, 14, 19)))
// 1-3,5,8-9,12-14,19
printRanges(
sliceByRange3elements(List(1, 2, 3, 5, 8, 9, 12, 13, 14, 19)))
// 1-3,5,8,9,12-14,19
Related
I have a list that can have certain numbers before an index that I don't need. For example:
val tempList: List[Any] = List(0,0,0,0,0,3,.,5,0,2,5)
How would I be able to remove all of the 0's before 3 without filtering out the 0 after the 3?
You can use dropWhile. Suppose you want to remove everything before 1:
scala> val l = List(0, 0, 0, 0, 1, 2, 0)
l: List[Int] = List(0, 0, 0, 0, 1, 2, 0)
scala> l.dropWhile(_ == 0)
res1: List[Int] = List(1, 2, 0)
What you need to do is a twist on dropWhile, that I would call filterWhile.
One simple solution that leverages the Collection API is the following:
def filterWhile[A](
list: List[A]
)(filterP: A => Boolean, whileP: A => Boolean): List[A] = {
val (toFilter, unfiltered) = list.span(whileP)
toFilter.filter(filterP) ++ unfiltered
}
You can play around with this code here on Scastie, alongside a couple of tests to verify it works as expected, which are the following:
def test[A](
input: List[A],
expected: List[A]
)(filterP: A => Boolean, whileP: A => Boolean): Unit =
assert(
filterWhile(input)(filterP, whileP) == expected,
s"input: $input, expected: $expected, got: ${filterWhile(input)(filterP, whileP)}"
)
test(
input = List(0, 0, 0, 0, 0, 3, '.', 5, 0, 2, 5),
expected = List(3, '.', 5, 0, 2, 5)
)(filterP = _ != 0, whileP = _ != 3)
test(
input = List(0, 0, 0, 1, 0, 3, '.', 5, 0, 2, 5),
expected = List(1, 3, '.', 5, 0, 2, 5)
)(filterP = _ != 0, whileP = _ != 3)
test(
input = List(1, 2, 3, 4),
expected = List(1, 2, 3, 4)
)(filterP = _ < 5, whileP = _ < 5)
In your case in particular all you have to do is invoke the function as follows:
filterWhile(tempList)(_ != 0, _ != 3)
Where the first predicate says how to filter and the second defines the "while" clause. I chose to align the predicate ordering to the name of the function (it first says "filter" and then "while") but feel free to adjust according to your preference. In any case, using named parameters is probably a good thing here.
Combining existing functions on lists, I came up with:
def filterUntil[A](list: List[A], filter: A => Boolean, cond: A => Boolean): List[A] = {
list.takeWhile(cond).filter(filter) ++ list.dropWhile(cond)
}
This produces much the same results as #stefanobaghino's answer.
This might be done with a foldLeft but in this case I would got for a recursive routine:
def remove(list: List[Any]): List[Any] = {
def loop(rem: List[Any], res: List[Any]): List[Any] =
rem match {
case Nil =>
res
case 0 :: tail =>
loop(tail, res)
case 3 :: _ =>
res ++ rem
case x :: tail =>
loop(tail, res :+ x)
}
loop(list, Nil)
}
remove(List(0,0,0,0,0,3,".",5,0,2,5))
// List(3, ., 5, 0, 2, 5)
Note that this won't be efficient for long lists because adding to the tail of a List gets very slow with longs lists.
Also, as noted in the comments, you should avoid Any if possible and use a more specific type.
[ Simplifications thanks to #MikhailIonkin ]
Suppose I have a list filled with zeroes
val a = List(0,0,0,0,2,4,0,6,0,7)
I want to slice away the zeroes preceding the first non-zero element and also return the index where the 1st non-zero element is present.
Foe the above case I want an output:
output = List(2,4,0,6,0,7)
idx = 4
How do I do it?
First, you can use zipWithIndex to conveniently pair each element with its index. Then use dropWhile to return all of the preceding zero elements. From there, you'll have all of the remaining elements paired with their indices from the original List. You can unzip them. Since this may result in an empty list, the index you're looking for should be optional.
scala> val (remaining, indices) = a.zipWithIndex.dropWhile { case (a, i) => a == 0 }.unzip
remaining: List[Int] = List(2, 4, 0, 6, 0, 7) // <--- The list you want
indices: List[Int] = List(4, 5, 6, 7, 8, 9)
scala> val index = indices.headOption
index: Option[Int] = Some(4) // <--- the index of the first non-zero element
This is a use-case for span:
val a = List(0,0,0,0,2,4,0,6,0,7)
val (zeros, output) = a.span(_ == 0)
val idx = zeros.length
use dropWhile:
val output = a.dropWhile{ _ == 0 }
val idx = output.headOption
.map(_ => a.length - output.length)
.getOrElse(-1) // index starting from 0, -1 if not found
Sightly modified from #bottaio answer, but returning an Option[Int] instead of a plain Int for the index.
def firstNonZero(l: List[Int]): (Option[Int], List[Int]) = {
#annotation.tailrec
def go(remaining: List[Int], idx: Int): (Int, List[Int]) =
remaining match {
case Nil => idx -> Nil
case 0 :: xs => go(remaining = xs, idx + 1)
case xs => idx -> xs
}
l match {
case 0 :: xs =>
val (idx, list) = go(remaining = xs, idx = 1)
Some(idx) -> list
case list =>
None -> list
}
}
Just another solution using foldLeft:
val (i, l) =
a.foldLeft((None: Option[Int], List.empty: List[Int]))((b, n) => {
if (n == 0 && b._2.isEmpty) (b._1.orElse(Some(0)).map(_ + 1), List.empty)
else (b._1.orElse(Some(0)), b._2 :+ n)
})
i: Option[Int] = Some(4)
l: List[Int] = List(2, 4, 0, 6, 0, 7)
You can do it pretty clean with indexWhere:
val idx = a.indexWhere(_!=0)
val output = a.drop(idx)
Others have provided answers that requires multiple list traversals. You can write a recursive function to calculate that in a single pass:
def firstNonZero(l: List[Int]): (Int, List[Int]) = {
#tailrec
def go(l: List[Int], idx: Int): (Int, List[Int]) = l match {
case Nil => (idx, Nil)
case 0 :: xs => go(xs, idx + 1)
case xs => (idx, xs)
}
go(l, 0)
}
what is also equivalent to
val (leadingZeros, rest) = a.span(_ == 0)
val (index, output) = (leadingZeros.length, rest)
val listA = List("one", "two", "three")
val listB = List(1, 3, 4, 9, 2, 6)
Result:
val m: Map[String, Int] = Map(one -> 1, two -> 2, three -> 3)
I want to pair listA and listB to a Map with corresponding key/value. I have tried to use the zip method but it only support sequential merging. How can I achieve the above result?
If f(x) is a function that can return the corresponding numerical value for the given alphabetical value, you can just map each x in listA to a pair (x, f(x)) and then turn the resulting list of pairs into a map using .toMap.
zip with sorted listB:
listA.zip(listB.sorted).toMap
Do you intend:
scala> val listA = List("one", "two", "three", "four")
listA: List[String] = List(one, two, three, four)
scala> val listB = List(1, 3, 4, 9, 2, 6)
listB: List[Int] = List(1, 3, 4, 9, 2, 6)
scala> val m = Map("one"->1, "two"->20, "three"->3)
m: scala.collection.immutable.Map[String,Int] = Map(one -> 1, two -> 20, three -> 3)
scala> listA flatMap { case k if (m contains k) && (listB contains m(k)) => Some(k, m(k)) ; case _ => None }
res4: List[(String, Int)] = List((one,1), (three,3))
I want to find all items before and equal the first 7:
val list = List(1,4,5,2,3,5,5,7,8,9,2,7,4)
My solution is:
list.takeWhile(_ != 7) ::: List(7)
The result is:
List(1, 4, 5, 2, 3, 5, 5, 7)
Is there any other solution?
One-liner for impatient:
List(1, 2, 3, 7, 8, 9, 2, 7, 4).span(_ != 7) match {case (h, t) => h ::: t.take(1)}
More generic version:
It takes any predicate as argument. Uses span to do the main job:
implicit class TakeUntilListWrapper[T](list: List[T]) {
def takeUntil(predicate: T => Boolean):List[T] = {
list.span(predicate) match {
case (head, tail) => head ::: tail.take(1)
}
}
}
println(List(1,2,3,4,5,6,7,8,9).takeUntil(_ != 7))
//List(1, 2, 3, 4, 5, 6, 7)
println(List(1,2,3,4,5,6,7,8,7,9).takeUntil(_ != 7))
//List(1, 2, 3, 4, 5, 6, 7)
println(List(1,2,3,4,5,6,7,7,7,8,9).takeUntil(_ != 7))
//List(1, 2, 3, 4, 5, 6, 7)
println(List(1,2,3,4,5,6,8,9).takeUntil(_ != 7))
//List(1, 2, 3, 4, 5, 6, 8, 9)
Tail-recursive version.
Just to illustrate alternative approach, it's not any more efficient than previous solution.
implicit class TakeUntilListWrapper[T](list: List[T]) {
def takeUntil(predicate: T => Boolean): List[T] = {
def rec(tail:List[T], accum:List[T]):List[T] = tail match {
case Nil => accum.reverse
case h :: t => rec(if (predicate(h)) t else Nil, h :: accum)
}
rec(list, Nil)
}
}
Borrowing the takeWhile implementation from scala.collection.List and changing it a bit:
def takeUntil[A](l: List[A], p: A => Boolean): List[A] = {
val b = new scala.collection.mutable.ListBuffer[A]
var these = l
while (!these.isEmpty && p(these.head)) {
b += these.head
these = these.tail
}
if(!these.isEmpty && !p(these.head)) b += these.head
b.toList
}
Here's a way to get there with foldLeft, and a tail recursive version to short circuit long lists.
There's also the tests I used while playing around with this.
import scala.annotation.tailrec
import org.scalatest.WordSpec
import org.scalatest.Matchers
object TakeUntilInclusiveSpec {
implicit class TakeUntilInclusiveFoldLeft[T](val list: List[T]) extends AnyVal {
def takeUntilInclusive(p: T => Boolean): List[T] =
list.foldLeft( (false, List[T]()) )({
case ((false, acc), x) => (p(x), x :: acc)
case (res # (true, acc), _) => res
})._2.reverse
}
implicit class TakeUntilInclusiveTailRec[T](val list: List[T]) extends AnyVal {
def takeUntilInclusive(p: T => Boolean): List[T] = {
#tailrec
def loop(acc: List[T], subList: List[T]): List[T] = subList match {
case Nil => acc.reverse
case x :: xs if p(x) => (x :: acc).reverse
case x :: xs => loop(x :: acc, xs)
}
loop(List[T](), list)
}
}
}
class TakeUntilInclusiveSpec extends WordSpec with Matchers {
//import TakeUntilInclusiveSpec.TakeUntilInclusiveFoldLeft
import TakeUntilInclusiveSpec.TakeUntilInclusiveTailRec
val `return` = afterWord("return")
object lists {
val one = List(1)
val oneToTen = List(1, 2, 3, 4, 5, 7, 8, 9, 10)
val boat = List("boat")
val rowYourBoat = List("row", "your", "boat")
}
"TakeUntilInclusive" when afterWord("given") {
"an empty list" should `return` {
"an empty list" in {
List[Int]().takeUntilInclusive(_ == 7) shouldBe Nil
List[String]().takeUntilInclusive(_ == "") shouldBe Nil
}
}
"a list without the matching element" should `return` {
"an identical list" in {
lists.one.takeUntilInclusive(_ == 20) shouldBe lists.one
lists.oneToTen.takeUntilInclusive(_ == 20) shouldBe lists.oneToTen
lists.boat.takeUntilInclusive(_.startsWith("a")) shouldBe lists.boat
lists.rowYourBoat.takeUntilInclusive(_.startsWith("a")) shouldBe lists.rowYourBoat
}
}
"a list containing one instance of the matching element in the last index" should `return`
{
"an identical list" in {
lists.one.takeUntilInclusive(_ == 1) shouldBe lists.one
lists.oneToTen.takeUntilInclusive(_ == 10) shouldBe lists.oneToTen
lists.boat.takeUntilInclusive(_ == "boat") shouldBe lists.boat
lists.rowYourBoat.takeUntilInclusive(_ == "boat") shouldBe lists.rowYourBoat
}
}
"a list containing one instance of the matching element" should `return` {
"the elements of the original list, up to and including the match" in {
lists.one.takeUntilInclusive(_ == 1) shouldBe List(1)
lists.oneToTen.takeUntilInclusive(_ == 5) shouldBe List(1,2,3,4,5)
lists.boat.takeUntilInclusive(_ == "boat") shouldBe List("boat")
lists.rowYourBoat.takeUntilInclusive(_ == "your") shouldBe List("row", "your")
}
}
"a list containing multiple instances of the matching element" should `return` {
"the elements of the original list, up to and including only the first match" in {
lists.oneToTen.takeUntilInclusive(_ % 3 == 0) shouldBe List(1,2,3)
lists.rowYourBoat.takeUntilInclusive(_.length == 4) shouldBe List("row", "your")
}
}
}
}
Possible way of doing this:
def takeUntil[A](list:List[A])(predicate: A => Boolean):List[A] =
if(list.isEmpty) Nil
else if(predicate(list.head)) list.head::takeUntil(list.tail)(predicate)
else List(list.head)
You could use following function,
def takeUntil(list: List[Int]): List[Int] = list match {
case x :: xs if (x != 7) => x :: takeUntil(xs)
case x :: xs if (x == 7) => List(x)
case Nil => Nil
}
val list = List(1,4,5,2,3,5,5,7,8,9,2,7,4)
takeUntil(list) //List(1,4,5,2,3,5,5,7)
Tail Recursive version
def takeUntilRec(list: List[Int]): List[Int] = {
#annotation.tailrec
def trf(head: Int, tail: List[Int], res: List[Int]): List[Int] = head match {
case x if (x != 7 && tail != Nil) => trf(tail.head, tail.tail, x :: res)
case x => x :: res
}
trf(list.head, list.tail, Nil).reverse
}
Some ways by use built-in functions:
val list = List(1, 4, 5, 2, 3, 5, 5, 7, 8, 9, 2, 7, 4)
//> list : List[Int] = List(1, 4, 5, 2, 3, 5, 5, 7, 8, 9, 2, 7, 4)
//Using takeWhile with dropWhile
list.takeWhile(_ != 7) ++ list.dropWhile(_ != 7).take(1)
//> res0: List[Int] = List(1, 4, 5, 2, 3, 5, 5, 7)
//Using take with segmentLength
list.take(list.segmentLength(_ != 7, 0) + 1)
//> res1: List[Int] = List(1, 4, 5, 2, 3, 5, 5, 7)
//Using take with indexOf
list.take(list.indexOf(7) + 1)
//> res2: List[Int] = List(1, 4, 5, 2, 3, 5, 5, 7)
Many of the solutions here are not very efficient, because they explore the whole list, rather than stopping early. Here is a short solution using the in-built functions:
def takeUntil[T](c: Iterable[T], f: T => Boolean): Iterable[T] = {
val index = c.indexWhere(f)
if (index == -1) c else c.take(index + 1)
}
What is the best Scala way to replace from some list every occurrence of element x by some other element y? This is what I am doing right now:
list map {
case `x` => y
case a => a
}
Is there more concise way available? Thanks.
list.map(i => if (i==x) y else i)
how about this?
If you need to do this a lot, you might write a utility function:
def replace[T](x: T, y: T) = (i: T) => if (i == x) y else i
This would allow you to write
list map replace(x, y)
Or, for infix syntax:
class ReplaceWith[T](x: T) {
def replaceWith(y: T) = (i: T) => if (i == x) y else i
}
object ReplaceWith {
implicit def any2replaceWith[T](x: T) = new ReplaceWith(x)
}
// now you can write
list map (x replaceWith y)
Another solution is to use a Map:
list map Map(x -> y).withDefault(identity)
With a utility function:
scala> def replace[T](pairs: (T, T)*) = Map(pairs: _*).withDefault(identity)
replace: [T](pairs: (T, T)*)scala.collection.immutable.Map[T,T]
scala> List(1,2,3) map replace(1 -> -1, 3 -> 4)
res0: List[Int] = List(-1, 2, 4)
You can create custom method for replacing:
class RichIterable[E] (col: Iterable[E]) {
def replace(pairs: (E, E)*): Iterable[E] = col map {
a => pairs.find(_._1 == a) match {
case None => a
case Some(p) => p._2
}
}
}
object RichIterable {
implicit def iterable2RichIterable[A](col: Iterable[A]) =
new RichIterable(col)
}
Replacing elements should then be easy:
scala> import RichIterable._
import RichIterable._
scala> List(1, 2, 3, 4, 5, 4, 3, 4, 7).replace(3 -> 30, 4 -> 40)
res1: Iterable[Int] = List(1, 2, 30, 40, 5, 40, 30, 40, 7)