2013-07-01 56 views
0

我试图构造具有唯一数字的所有N位数长整数列表, 但我无法弄清楚如何推广它,作为较大的一部分问题,为此我需要具有唯一数字的所有(1到N)数字长号的列表。具有唯一数字的所有N位数长整数列表

这里是n的手写代码= 4:

for { 
    x1 <- 1 to 9 
    x2 <- 1 to 9 
    x3 <- 1 to 9 
    x4 <- 1 to 9 
    if (x1 != x2 && x2 != x3 && x3 != x4 && x1 != x3 && x1 != x4 && x2 != x4) 
    num4 = x1 + x2 * 10 + x3 * 100 + x4 * 1000 
} yield num4 
+2

此代码将无法生成(例如)1234.所有四个范围的下限需要为1. – gzm0

回答

0

我的方法是过滤掉你不感兴趣的结果
Filtering使用设置的财产,他们只。有不同的价值。
n> 10的情况与n = 10的情况不会产生不同的结果,所以我们可以默认回到那个。

def generateUniqueList(n: Int) = { 
    val sanitizedInput = if(n>10) 10 else n 
    0 to math.pow(10, sanitizedInput).toInt-1 filter(num => num.toString.toSeq.size == num.toString.toSet.size) 
} 

这不会产生最快的解决方案,但其概念简单易懂。

例子:

val result = generateUniqueList(2) 
//> result : scala.collection.immutable.IndexedSeq[Int] = Vector(0, 1, 2, 3, 4, 
//| 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 23, 24, 25, 26, 
//| 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 
//| 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 68, 
//| 69, 70, 71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 89, 
//| 90, 91, 92, 93, 94, 95, 96, 97, 98) 
+0

我认为如果返回“12”,那么您不应该返回“21”等? –

+0

是的,我想你是对的。你将如何解决答案中的0-s?例如。 10有效,但123(3位)vs 0123(4位数)会导致冲突? –

+0

使用字符串解决0问题(我认为?) –

7
scala> "1234".combinations(2).toList 
res9: List[String] = List(12, 13, 14, 23, 24, 34) 

随着范围:

scala> (1 to 9).combinations(4).toList 
res12: List[scala.collection.immutable.IndexedSeq[Int]] = List(Vector(1, 2, 3, 4), Vector(1, 2, 3, 5), Vector(1, 2, 3, 6), Vector(1, 2, 3, 7), Vector(1, 2, 3, 8), Vector(1, 2, 3, 9), Vector(1, 2, 4, 5), Vector(1, 2, 4, 6), Vector(1, 2, 4, 7), Vector(1, 2, 4, 8), Vector(1, 2, 4, 9), Vector(1, 2, 5, 6), Vector(1, 2, 5, 7), Vector(1, 2, 5, 8), Vector(1, 2, 5, 9), Vector(1, 2, 6, 7), Vector(1, 2, 6, 8), Vector(1, 2, 6, 9), Vector(1, 2, 7, 8), Vector(1, 2, 7, 9), Vector(1, 2, 8, 9), Vector(1, 3, 4, 5), Vector(1, 3, 4, 6), Vector(1, 3, 4, 7), Vector(1, 3, 4, 8), Vector(1, 3, 4, 9), Vector(1, 3, 5, 6), Vector(1, 3, 5, 7), Vector(1, 3, 5, 8), Vector(1, 3, 5, 9), Vector(1, 3, 6, 7), Vector(1, 3, 6, 8), Vector(1, 3, 6, 9), Vector(1, 3, 7, 8), Vector(1, 3, 7, 9), Vector(1, 3, 8, 9), Vector(1, 4, 5... 

为int的列表:

"123456789".combinations(4).map(_.toInt).toList 
res37: List[Int] = List(1234, 1235, 1236, 1237, 1238, 1239, 1245, 1246, 1247, 1248, 1249, 1256, 1257, 1258, 1259, 1267, 1268, 1269, 1278, 1279, 1289, 1345, 1346, 1347, 1348, 1349, 1356, 1357, 1358, 1359, 1367, 1368, 1369, 1378, 1379, 1389, 1456, 1457, 1458, 1459, 1467, 1468, 1469, 1478, 1479, 1489, 1567, 1568, 1569, 1578, 1579, 1589, 1678, 1679, 1689, 1789, 2345, 2346, 2347, 2348, 2349, 2356, 2357, 2358, 2359, 2367, 2368, 2369, 2378, 2379, 2389, 2456, 2457, 2458, 2459, 2467, 2468, 2469, 2478, 2479, 2489, 2567, 2568, 2569, 2578, 2579, 2589, 2678, 2679, 2689, 2789, 3456, 3457, 3458, 3459, 3467, 3468, 3469, 3478, 3479, 3489, 3567, 3568, 3569, 3578, 3579, 3589, 3678, 3679, 3689, 3789, 4567, 4568, 4569, 4578, 4579, 4589, 4678, 4679, 4689, 4789, 5678, 5679, 5689, 5789, 6789) 

如果你实际上感兴趣的各种排列(问题已经改变了一点因为我写了上面的答案)

scala> "1234".combinations(2).toList.flatMap(_.permutations).map(_.toInt) 
res51: List[Int] = List(12, 21, 13, 31, 14, 41, 23, 32, 24, 42, 34, 43) 
+0

但我们想得到一个整数列表。更好地作出新的答案或编辑你的? – NightRa

+0

修改答案返回一个列表[Int] –

+0

非常接近,但我相信这个问题也要求每个选择的数字(如'1243')的所有排列组合。所以完整的代码应该是'(1到9).combinations(4)flatMap {_.permutations} map {_.mkString(“”)。toInt} toList' – Kane

1

你正在寻找被称为单子排序什么。当使用for理解如

for(x <- Set(...) 
    y <- Set(...)) 
    yield (x, y) 

可以查看它具有一对一元计算(Set(...), Set(...))的并将其转换成一个单子保持一对结果Set((...,...), (...,...), ...)的。

这可以概括为任意长度的序列。该库定义特征Traversable,其表示我们可以定义这种操作的序列。这是traverse操作是更普遍的,我们可以用它来定义一元排序:

import scalaz._ 
import scalaz.Scalaz._ 
import scala.collection.immutable._ 

def sequence[M[_]: Applicative, T[_], A](seq: T[M[A]]) 
     (implicit t: Traverse[T]): M[T[A]] = 
    t.traverse(identity[M[A]], seq); 

在我们的情况下,MSetTSeq。因此,考虑的选择序列Seq[Set[Int]]类型(表示为套),sequence将得到我们Set[Seq[Int]]这是所有可能的组合,从选择采摘:

// a helper function 
def set[A](seq: Seq[A]): Set[A] = Set(seq : _*); 

// prints all sequences where the first number is from 1 to 9 
// and the second number from 2 to 9: 
println(sequence(List(set(1 to 9), set(2 to 9)))) 

然后,您可以过滤序列,其中某些数字会多次出现。

如果你想在这个过程中包含这种过滤,它会变得更加复杂。你需要在单计算过程中保持一些状态,这将保持可用的数字。让我们来定义

type Choices = Set[Int] 
type DigitChoose[A] = StateT[Set,Choices,A] 

所以DigitChoose是具有不确定性的结果(每一个操作产生一组可能出现的结果,像以前一样),而且还使Choices型状态的单子,它告诉我们的数字是什么仍可用。接下来,我们定义一些帮助函数来帮助编译器稍微(一般类型对它太多:))。

// See http://stackoverflow.com/q/7782589/1333025 why we need this: 
implicit val applicativeDC: Applicative[DigitChoose] 
    = implicitly[Monad[DigitChoose]] 

def sequenceDC[A](seq: Seq[DigitChoose[A]]): DigitChoose[Seq[A]] 
    = sequence(seq); 

所以现在我们对于序列定义了DigitChoose的单子序列。

使用DigitChoose我们可以定义选择一个位从可得的那些中的一个操作,并更新状态(删除从该组中的选择的数字):

// Pick any of the available digits: 
def choose: DigitChoose[Int] = 
    stateT((s: Choices) => for(i <- s) yield (s - i, i)); 

和一些限制变体限制的数字的选择:

// Pick an available digit that is also in the given set of choices: 
def choose(c: Choices): DigitChoose[Int] = 
    stateT((s: Choices) => for(i <- s intersect c) yield (s - i, i)); 
// Pick an available digit that is also in the given set of choices: 
def choose(cs: Seq[Int]): DigitChoose[Int] 
    = choose(cs.toSet); 

最后,我们做这样的事情:

// First digit is unrestricted 
// Second digit is from 1 to 3 
// Third digit is from 1 to 2 
// ... and no digits will be the same! 
val choices = Seq(choose, choose(1 to 3), choose(1 to 2)); 
// Sequence the choices, given that we start with digits 1 to 9 
println(sequenceDC(choices) ! Set(1 to 9 : _*)); 

The complete code is available on gist

相关问题