scala - Method that add and remove an element from a List -
I am writing a method that takes a The method is the following behavior: I have applied the method as: One of the more express ways to express this behavior Method exists ?? and some tests 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. and the result of its test But as I said in a comment, the whole sequence of x is used to better use Process and returns the top n. 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.
n elements in the list.
list is low, add the
x element to the list.
x code extract the minimum element from the list of rules, and add the
x element to the list.
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
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}}
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))
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
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))
Comments
Post a Comment