Guest User

Untitled

a guest
Mar 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. def mycomb[T](n: Int, l: List[T]): List[List[T]] =
  2. n match {
  3. case 0 => List(List())
  4. case _ => for(el <- l;
  5. sl <- mycomb(n-1, l dropWhile { _ != el } ))
  6. yield el :: sl
  7. }
  8.  
  9. def comb[T](n: Int, l: List[T]): List[List[T]] = mycomb(n, l.removeDuplicates)
  10.  
  11. > comb(3, List(1,2,3))
  12. > List[List[Int]] = List(
  13. List(1, 1, 1), List(1, 1, 2), List(1, 1, 3), List(1, 2, 2),
  14. List(1, 2, 3), List(1, 3, 3), List(2, 2, 2), List(2, 2, 3),
  15. List(2, 3, 3), List(3, 3, 3))
  16.  
  17. > comb(6, List(1,2,1,2,1,2,1,2,1,2))
  18. > List[List[Int]] = List(
  19. List(1, 1, 1, 1, 1, 1), List(1, 1, 1, 1, 1, 2), List(1, 1, 1, 1, 2, 2),
  20. List(1, 1, 1, 2, 2, 2), List(1, 1, 2, 2, 2, 2), List(1, 2, 2, 2, 2, 2),
  21. List(2, 2, 2, 2, 2, 2))
  22.  
  23. def perm[T](n: Int, l: List[T]): List[List[T]] =
  24. n match {
  25. case 0 => List(List())
  26. case _ => for(el <- l.removeDuplicates;
  27. sl <- perm(n-1, l.slice(0, l.findIndexOf {_ == el}) ++ l.slice(1 + l.findIndexOf {_ == el}, l.size)))
  28. yield el :: sl
  29. }
  30.  
  31. perm(2, List(1,2,2,2,1))
  32.  
  33. List(List(2, 2), List(2, 1), List(1, 2), List(1, 1))
  34.  
  35. List(
  36. List(1, 2), List(1, 2), List(1, 2), List(2, 1),
  37. List(2, 1), List(2, 1), List(2, 1), List(2, 1),
  38. List(2, 1), List(1, 2), List(1, 2), List(1, 2)
  39. )
  40.  
  41. //P26
  42. def combinations[A](n:Int, xs:List[A]):List[List[A]]={
  43. def lift[A](xs:List[A]):List[List[A]]=xs.foldLeft(List[List[A]]())((ys,y)=>(List(y)::ys))
  44.  
  45. (n,xs) match {
  46. case (1,ys)=> lift(ys)
  47. case (i,xs) if (i==xs.size) => xs::Nil
  48. case (i,ys)=> combinations(i-1,ys.tail).map(zs=>ys.head::zs):::combinations(i,ys.tail)
  49. }
  50. }
  51.  
  52. def mycomb[T](n: Int, l: List[T]): List[List[T]] =
  53. n match {
  54. case 0 => List(List())
  55. case _ => for(el <- l;
  56. sl <- mycomb(n-1, l))
  57. yield el :: sl
  58. }
  59.  
  60. def perm[T](n: Int, l: List[T]): List[List[T]] =
  61. n match {
  62. case 0 => List(List())
  63. case _ => for(el <- l;
  64. sl <- perm(n-1, l filter (_ != el)))
  65. yield el :: sl
  66. }
  67.  
  68. def perm[T](n: Int, l: List[T]): List[List[T]] = {
  69. def perm1[T](n: Int, l: List[T]): List[List[T]] =
  70. n match {
  71. case 0 => List(List())
  72. case _ => for(el <- l;
  73. (hd, tl) = l span (_ != el);
  74. sl <- perm(n-1, hd ::: tl.tail))
  75. yield el :: sl
  76. }
  77. perm1(n, l).removeDuplicates
  78. }
  79.  
  80. def comb[T](n: Int, l: List[T]): List[List[T]] =
  81. n match {
  82. case 0 => List(List())
  83. case _ => for(i <- (0 to (l.size - n)).toList;
  84. l1 = l.drop(i);
  85. sl <- comb(n-1, l1.tail))
  86. yield l1.head :: sl
  87. }
  88.  
  89. def comb[T](n: Int, l: List[T]): List[List[T]] = {
  90. def comb1[T](n: Int, l: List[T]): List[List[T]] =
  91. n match {
  92. case 0 => List(List())
  93. case _ => for(i <- (0 to (l.size - n)).toList;
  94. l1 = l.drop(i);
  95. sl <- comb(n-1, l1.tail))
  96. yield l1.head :: sl
  97. }
  98. comb1(n, l).removeDuplicates
  99. }
Add Comment
Please, Sign In to add comment