scala - Method that add and remove an element from a List -


I am writing a method that takes a list: list [[string, ante]] x: (string, int) and n: int parameter and a list represents [[string, int]] list parameter Input list indicates element to add to the x element list; n indicates the maximum dimension of the list.

The method is the following behavior:

  1. Firstly check that there are n elements in the list.
  2. If the list is low, add the x element to the list.
  3. If in the list, x code extract the minimum element from the list of rules, and add the x element to the list.

    I have applied the method as:

      def method (list: list [(string, int)], x: (string , Int), n: int) = {if (list.size & lt; N) list: + x other {var index = 1 var min = 10000000 // some large numbers (L & L; - 0 in the list. ) {If (list (el) ._ 2 & lt; x._2 & amp; Amp; list (L) ._ 2   

    One of the more express ways to express this behavior Method exists ??

      def method (list: list [[string, ante]], x: (string, int), en: int = {if (list.size & lt; n) list: + x else {Var index = -1 (up to list.size up to -1) {val el = list (i) if (lol and lieutenant; x._2) index = i} if (index! = -1) {Well (before, after) = list. Plot at (index) value = +++++ new list: + x} and list}}   

    and some tests

      val l1 = list (("one", 1)) val l2 = method (L1, ("three", 3), 2) / /> L2: List [(string, int) = list ((a, 1), (three, 3)) val l3 = method (L2, ("four", 4), 2) // & gt; L3: List [(string, int) = list ((a, 1), (four, 4)) Val L4 = method (L2, ("zero", 0), 2) // & gt; L4: List [(string, int) = list ((1, 1), (three, 3))   

    The blue version (but still as a comment mentioned in the fiction Def (method: list [(string, int)], x: (string, int, n: int) = {if (list .size & lt; n) list: + x other {val (before, after) = list.span (_._ 2> = x._2) if (after.isEmpty) ++: List first: ++ : + X}}

    Another version that always minimizes the minimum, if it is less than x.

      def method (list: list [[string, int]], x: (string, int), n: int) = {if (list.size & lt; n) + X other (value smaller) = list.minBy (_._ 2) if (smallest ._2   

    and the result of its test

     before = list.span (_! = Small)  val l1 = list (("one", 1)) val l2 = method (L1 ("Three", 3), 2) //> L2: list [(string, int) = list ((a, 1), (three, 3)) val l3 = method (L2, ( "Four", 4), 2) //> L3: list [(string, int) = list ((three, 3), (four, 4)) val L4 = method (L2, ( "Zero", 0), 2) //> L4: list [(string, Unt) list = ((one, 1), (three, 3))   

    But as I said in a comment, the whole sequence of x is used to better use Process and returns the top n.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

c++ - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr -