标签: combinatorics

随机抽样的字符向量,没有相互加前缀的元素

考虑一个字符向量,pool其元素是(零填充)二进制数,最多为max_len数字.

max_len <- 4
pool <- unlist(lapply(seq_len(max_len), function(x) 
  do.call(paste0, expand.grid(rep(list(c('0', '1')), x)))))

pool
##  [1] "0"    "1"    "00"   "10"   "01"   "11"   "000"  "100"  "010"  "110" 
## [11] "001"  "101"  "011"  "111"  "0000" "1000" "0100" "1100" "0010" "1010"
## [21] "0110" "1110" "0001" "1001" "0101" "1101" "0011" "1011" "0111" "1111"
Run Code Online (Sandbox Code Playgroud)

我想品尝n这些元素,与约束,没有一个采样的元素是前缀的任何其他采样元素(即如果我们品尝1101,我们禁止1,11110,而如果我们品尝1,我们禁止这些元素开始用1,例如10,11,100等等).

以下是我尝试使用的while,但当n …

performance r combinatorics

44
推荐指数
8
解决办法
2423
查看次数

谷歌访谈:块的安排

给你N块高度1 ... N. 您可以通过多少种方式将这些块排成一排,这样当从左侧看时,您只能看到L个块(其余部分被较高的块隐藏),从右侧看时您只看到R块?给出的例子N=3, L=2, R=1只有一种安排,{2, 1, 3}N=3, L=2, R=2有两种方式{1, 3, 2}{2, 3, 1}.

我们应该如何通过编程解决这个问题?任何有效的方法?

language-agnostic algorithm combinatorics

40
推荐指数
3
解决办法
6089
查看次数

生成所有5张牌扑克牌

乍一看这个问题听起来很简单,但事实证明它看起来要复杂得多.这让我很难过.

有52c5 = 2,598,960种方法可以从52张牌中选择5张牌.然而,由于套装在扑克中是可以互换的,所以其中许多都是等同的 - 手2H 2C 3H 3S 4D相当于2D 2S 3D 3C 4H - 简单地换掉套装.根据维基百科,一旦你考虑到可能的套装重新着色,有134,459个不同的5张牌.

问题是,我们如何有效地生成所有这些可能的手?我不想生成所有的手,然后消除重复,因为我想将问题应用于更多的卡,以及评估快速螺旋失控的手的数量.我目前的尝试集中在生成深度优先,并跟踪当前生成的卡以确定哪些套装和等级对下一张卡有效,或者广度优先,生成所有可能的下一张卡,然后通过转换每个卡来删除重复通过重新着色来制作"规范"版本.这是我在Python中尝试广度优先的解决方案:

# A card is represented by an integer. The low 2 bits represent the suit, while
# the remainder represent the rank.
suits = 'CDHS'
ranks = '23456789TJQKA'

def make_canonical(hand):
  suit_map = [None] * 4
  next_suit = 0
  for i in range(len(hand)):
    suit = hand[i] & 3
    if suit_map[suit] is None:
      suit_map[suit] = next_suit
      next_suit += 1
    hand[i] = hand[i] & …
Run Code Online (Sandbox Code Playgroud)

python algorithm poker permutation combinatorics

38
推荐指数
1
解决办法
1万
查看次数

Code-golf:生成pascal的三角形

生成一个列表列表(或打印,我不介意)一个大小为N 的Pascal三角形,代码可能最少!

这是我的尝试(使用技巧python 2.6中的 118个字符):

c,z,k=locals,[0],'_[1]'
p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)]
Run Code Online (Sandbox Code Playgroud)

说明:

  • 列表理解的第一个元素(当长度为0时)是 [1]
  • 下一个元素是通过以下方式获得的:
  • 取前面的列表并制作两个列表,一个在开头填充0,在结尾填充另一个.
    • 例如,对于第二步,我们采取[1]并制造[0,1][1,0]
  • 按元素对两个新列表求和
    • 例如,我们制作一个新的列表[(0,1),(1,0)]并用sum来映射.
  • 重复n次,就是这样.

用法(漂亮的打印,实际上是代码-golf xD):

result = p(10)
lines = [" ".join(map(str, x)) for x in result]
for i in lines:
    print i.center(max(map(len, lines)))
Run Code Online (Sandbox Code Playgroud)

输出:

             1             
            1 1            
           1 2 1           
          1 3 3 1          
         1 4 6 4 1         
       1 5 10 10 5 1       
      1 6 15 20 …
Run Code Online (Sandbox Code Playgroud)

algorithm code-golf combinatorics discrete-mathematics pascals-triangle

37
推荐指数
6
解决办法
7103
查看次数

在C++中创建n个项目的所有可能k组合

有n个人编号从.1n.我必须编写一个代码,用于生成和打印k这些人的所有不同组合n.请解释用于此的算法.

c++ algorithm math combinations combinatorics

37
推荐指数
5
解决办法
11万
查看次数

这个列表在Haskell中的排列实现到底是做什么的?

我正在研究Data.List模块中的代码,并不能完全围绕这种排列实现:

permutations            :: [a] -> [[a]]
permutations xs0        =  xs0 : perms xs0 []
  where
    perms []     _  = []
    perms (t:ts) is = foldr interleave (perms ts (t:is)) (permutations is)
      where interleave    xs     r = let (_,zs) = interleave' id xs r in zs
            interleave' _ []     r = (ts, r)
            interleave' f (y:ys) r = let (us,zs) = interleave' (f . (y:)) ys r
                                     in  (y:us, f (t:y:us) : zs)
Run Code Online (Sandbox Code Playgroud)

有人可以详细解释这些嵌套函数如何相互连接/相互作用?

haskell list permutation combinatorics

37
推荐指数
1
解决办法
7636
查看次数

生成一些向量元素的所有可能组合(笛卡尔积)

我想生成给定数量的向量的元素的所有可能组合.

例如,对于[1 2],[1 2][4 5]我想要生成的元素:

[1 1 4; 1 1 5; 1 2 4; 1 2 5; 2 1 4; 2 1 5; 2 2 4; 2 2 5]

问题是我不知道我需要计算组合的向量的数量.在这种情况下可能有3个,或者可能有10个,我需要一个概括.你能帮我在MATLAB中帮助我吗?是否已有可以执行此任务的预定义功能?

matlab combinatorics cartesian-product

33
推荐指数
3
解决办法
5万
查看次数

排列 - 所有可能的数字集

我有数字,从0到8.我想在结果中,所有可能的那些数字组,每组应该使用所有数字,每个数字只能在一组中出现一次.

我想在PHP中看到可以打印出结果的解决方案.或者,至少,我想在组合学理论上有一些更新,因为我早就忘记了它.计算有多少排列的公式是什么?

示例集:

  • 0-1-2-3-4-5-6-7-8
  • 0-1-2-3-4-5-6-8-7
  • 0-1-2-3-4-5-8-6-7
  • 0-1-2-3-4-8-5-6-7
  • 0-1-2-3-8-4-5-6-7
  • 0-1-2-8-3-4-5-6-7
  • 等等...

php permutation combinatorics

33
推荐指数
6
解决办法
5万
查看次数

在Python中设置分区

我有一个数组 [1,2,3]

我想使用数组的所有元素进行所有可能的组合:

结果:

[[1], [2], [3]]
[[1,2], [3]]
[[1], [2,3]]
[[1,3], [2]]
[[1,2,3]]
Run Code Online (Sandbox Code Playgroud)

python arrays combinatorics

32
推荐指数
3
解决办法
1万
查看次数

用于确定可以从序列中移除一组值的所有可能方式的算法

我试图确定有多少种不同的方法可以从序列中删除一组值,保留原始序列的顺序(稳定),并确保从原始序列中只删除1个实例值.例如,如果我有 [1,2,1,3,1,4,4],我想删除[1,4,4]我得到的组合将是:

[1,2,1,3,1,4,4] \ [1,4,4] = [ [2,1,3,1], [1,2,3,1], [1,2,1,3] ]

要么 [1,2,1,3,1,4,4] \ [1,1] = [ [2,3,1,4,4], [1,2,3,4,4], [2,1,3,4,4] ]

我有javascript代码我写的所有数组值的组合没有删除和删除部分似乎应该很容易但我没有看到算法需要多次可能删除多个值.

javascript algorithm combinations set-theory combinatorics

32
推荐指数
4
解决办法
927
查看次数