0

I have the below arrays with me

 scala> arr1
res77: Array[Array[Int]] = Array(Array(5, 1, 99), Array(1, 2, 99), Array(2, 3, 99), Array(5, 6, 99))

scala> arr2
res78: Array[Array[Int]] = Array(Array(5, 1, 110), Array(1, 2, 110), Array(2, 3, 110), Array(5, 6, 110))

The third element of each item of this array will be a constant value(ie, 99 for first and 110 for second). I have to take the values from the array, based on this third element

ie, if the third element <=100 , I have to get those items in the array whose second element is < 5

if the third element >100 , I have to get those items in the array whose second element is > 5

Expected Output:

Array[Array[Int]] = Array(Array(5, 1, 99), Array(1, 2, 99), Array(2, 3, 99) ) //Output for arr1
Array[Array[Int]] = Array(Array(5, 6, 110)) //Output for arr2

How can i make a generalized code ?

2 Answers 2

2

If you are guaranteed to have 3rd element to be the same you can try something like this:

arr1.filter(arr => {if (arr(2) <= 100) arr(1) < 5 else arr(1) > 5 })
Sign up to request clarification or add additional context in comments.

Comments

1

Not sure what you call a generalised code, but this is how I would do it:

First you can have n method equal to the number of conditions you desire.

def filterArrayByCondition1(input: Array[Array[Int]]): Array[Array[Int]] = {
    input.filter { arr =>
      val optConstant: Option[Int] = arr.lift(2)
      val optDecider: Option[Int] = arr.lift(1)

      val optCondition: Option[Boolean] = for {
        constant <- optConstant
        decider <- optDecider
      } yield {
        constant <= 100 && decider < 5
      }
      optCondition.getOrElse(false)

    }
  }

  def filterArrayByCondition2(input: Array[Array[Int]]): Array[Array[Int]] = {

    input.filter { arr =>
      val optConstant: Option[Int] = arr.lift(2)
      val optDecider: Option[Int] = arr.lift(1)

      val optCondition = for {
        constant <- optConstant
        decider <- optDecider
      } yield {
        constant > 100 && decider > 5
      }

      optCondition.getOrElse(false)

    }

  }

// you then can call them like this
filterArrayByCondition1(arr1).foreach(arr => println(arr.toSeq))
filterArrayByCondition2(arr2).foreach(arr => println(arr.toSeq))

Or define a condition like this:

val condition1: Array[Int] => Boolean = { arr: Array[Int] =>
    val optConstant: Option[Int] = arr.lift(2)
    val optDecider: Option[Int] = arr.lift(1)

    val optCondition: Option[Boolean] = for {
      constant <- optConstant
      decider <- optDecider
    } yield {
      constant <= 100 && decider < 5
    }

    optCondition.getOrElse(false)

  }

 // you then can call them like this
 arr1.filter(condition1.apply).foreach(arr => println(arr.toSeq))

Or combine them both to have maybe the cleanest:

def filterArrayByCondition(input: Array[Array[Int]], condition: Array[Int] => Boolean): Array[Array[Int]] = {
    input.filter(condition)
  }

// you then can call them like this
filterArrayByCondition(arr1, condition1).foreach(arr => println(arr.toSeq))

At the end you will have to define the condition that you want to filter the arrays, so the generalised code that you desire seems to be the last option that I gave you.

Hope it helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.