List 如何在列表中随机插入元素
给定一个列表[Int]l,如何在列表[Int]l中随机插入一个新元素elem def randomInsertl:List[Int],elem:Int:List[Int]=???List 如何在列表中随机插入元素,list,scala,random,List,Scala,Random,给定一个列表[Int]l,如何在列表[Int]l中随机插入一个新元素elem def randomInsertl:List[Int],elem:Int:List[Int]=??? 这可以通过首先在列表中选择一个随机索引,然后再选择一个来完成。此外,这可以通过一般方式完成: 导入scala.util.Random 定义随机插入[A]l:List[A],元素:A:List[A]={ val random=新随机 val randomIndex=random.nextIntl.length+1 l、
这可以通过首先在列表中选择一个随机索引,然后再选择一个来完成。此外,这可以通过一般方式完成: 导入scala.util.Random 定义随机插入[A]l:List[A],元素:A:List[A]={ val random=新随机 val randomIndex=random.nextIntl.length+1 l、 patchrandomIndex,列表项,0 } 用法:
scala>randomInsert(List(1,2,3,4,5),100)
res2: List[Int] = List(1, 2, 3, 4, 5, 100)
scala>randomInsert(List(1,2,3,4,5),100)
res3: List[Int] = List(100, 1, 2, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100)
res4: List[Int] = List(1, 2, 100, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res10: List[Int] = List(1, 2, 101, 3, 4, 5, 100, 102)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res11: List[Int] = List(1, 2, 102, 100, 101, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res12: List[Int] = List(1, 2, 3, 4, 100, 5, 102, 101)
我们可以使用此方法递归添加几个元素:
import scala.util.Random
import scala.annotation.tailrec
def randomInsert[A](l: List[A], elem: A, elems: A*): List[A] = {
val random = new Random
@tailrec
def loop(elemToInsert: List[A], acc: List[A]): List[A] =
elemToInsert match {
case Nil => acc
case head :: tail =>
val randomIndex = random.nextInt(acc.length + 1)
loop(tail, acc.patch(randomIndex, List(head), 0))
}
loop(elem :: elems.toList, l)
}
用法:
scala>randomInsert(List(1,2,3,4,5),100)
res2: List[Int] = List(1, 2, 3, 4, 5, 100)
scala>randomInsert(List(1,2,3,4,5),100)
res3: List[Int] = List(100, 1, 2, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100)
res4: List[Int] = List(1, 2, 100, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res10: List[Int] = List(1, 2, 101, 3, 4, 5, 100, 102)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res11: List[Int] = List(1, 2, 102, 100, 101, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res12: List[Int] = List(1, 2, 3, 4, 100, 5, 102, 101)
编辑:
根据评论,一种更有效的方法是加入列表并洗牌组合的单音符,而不是这样做,你可能会失去列表的原始顺序:
导入scala.util.Random
定义随机插入[A]l:List[A],元素:A,元素:A*:List[A]={
Random.ShuffleLem::elems.toList反转u::l
}
这可以通过首先在列表中选择一个随机索引,然后再选择一个来完成。此外,这可以通过一般方式完成: 导入scala.util.Random 定义随机插入[A]l:List[A],元素:A:List[A]={ val random=新随机 val randomIndex=random.nextIntl.length+1 l、 patchrandomIndex,列表项,0 } 用法:
scala>randomInsert(List(1,2,3,4,5),100)
res2: List[Int] = List(1, 2, 3, 4, 5, 100)
scala>randomInsert(List(1,2,3,4,5),100)
res3: List[Int] = List(100, 1, 2, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100)
res4: List[Int] = List(1, 2, 100, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res10: List[Int] = List(1, 2, 101, 3, 4, 5, 100, 102)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res11: List[Int] = List(1, 2, 102, 100, 101, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res12: List[Int] = List(1, 2, 3, 4, 100, 5, 102, 101)
我们可以使用此方法递归添加几个元素:
import scala.util.Random
import scala.annotation.tailrec
def randomInsert[A](l: List[A], elem: A, elems: A*): List[A] = {
val random = new Random
@tailrec
def loop(elemToInsert: List[A], acc: List[A]): List[A] =
elemToInsert match {
case Nil => acc
case head :: tail =>
val randomIndex = random.nextInt(acc.length + 1)
loop(tail, acc.patch(randomIndex, List(head), 0))
}
loop(elem :: elems.toList, l)
}
用法:
scala>randomInsert(List(1,2,3,4,5),100)
res2: List[Int] = List(1, 2, 3, 4, 5, 100)
scala>randomInsert(List(1,2,3,4,5),100)
res3: List[Int] = List(100, 1, 2, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100)
res4: List[Int] = List(1, 2, 100, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res10: List[Int] = List(1, 2, 101, 3, 4, 5, 100, 102)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res11: List[Int] = List(1, 2, 102, 100, 101, 3, 4, 5)
scala>randomInsert(List(1,2,3,4,5),100,101,102)
res12: List[Int] = List(1, 2, 3, 4, 100, 5, 102, 101)
编辑:
根据评论,一种更有效的方法是加入列表并洗牌组合的单音符,而不是这样做,你可能会失去列表的原始顺序:
导入scala.util.Random
定义随机插入[A]l:List[A],元素:A,元素:A*:List[A]={
Random.ShuffleLem::elems.toList反转u::l
}