scala> val strs = Seq("BCE", "CE", "AC", "BE", "BC")
strs: Seq[String] = List(BCE, CE, AC, BE, BC)
scala> val rules = for(s <- strs;i <- 1 to s.size-1;p <- s combinations i) yield p -> s.diff(p)
rules: Seq[(String, String)] =
List((B,CE),
(C,BE),
(E,BC),
(BC,E),
(BE,C),
(CE,B),
(C,E),
(E,C),
(A,C),
(C,A),
(B,E),
(E,B),
(B,C),
(C,B))
編輯這裏是新問題的代碼:
scala> case class Rule(src: Seq[String], dst: Seq[String]) {
| override def toString = s"${src.mkString(" ")} -> ${dst.mkString(" ")}"
| }
defined class Rule
scala> val strs = Seq("Ball Cat Egg", "Cat Egg", "Apple Cat", "Ball Egg", "Ball Cat")
strs: Seq[String] = List(Ball Cat Egg, Cat Egg, Apple Cat, Ball Egg, Ball Cat)
scala> val rules = for(s <- strs;l = s.split(" ");i <- 1 to l.size-1;p <- l combinations i)
yield Rule(p, l diff p)
rules: Seq[Rule] = List(Ball -> Cat Egg, Cat -> Ball Egg, Egg -> Ball Cat,
Ball Cat -> Egg, Ball Egg -> Cat, Cat Egg -> Ball,
Cat -> Egg, Egg -> Cat, Apple -> Cat, Cat -> Apple,
Ball -> Egg, Egg -> Ball, Ball -> Cat, Cat -> Ball)
如何把「 - >」放在(B,CE)之間,即(B - > CE) – rosy 2014-10-08 11:45:48
Plz幫助我...問題有som e改變... – rosy 2014-10-09 06:50:29
Thanku so much sir .... 1更多懷疑...其實它是一個規則類型,我們如何把每個項目分開.ie,c - > b,我希望c&b seperately..this下一個目的是否需要? – rosy 2014-10-09 08:35:43