【问题标题】:Finding Kth lexicographic permutation when repetition is allowed using Factoradic system使用Factoradic系统在允许重复时找到第K个字典排列
【发布时间】:2017-12-10 15:44:08
【问题描述】:

在允许重复的情况下,是否可以使用Factoradic Base System 查找第 k 个排列?
为了找到不重复的第 K 个排列,我可以在 python 中做这样的事情:

def factorial(n):
    if n == 0: return 1
    return n*factorial(n-1)

def unrank(S, k, i):
    S = list(S)   # make a copy to avoid destroying the list
    n = len(S)
    nb = factorial(n) // factorial(n-k)
    print (nb)
    if i >= nb:
        raise IndexError
    res = []
    while k > 0:
        nb = nb // n
        pos = i // nb   # the factoradic digits
        i = i % nb      # the remaining digits
        res.append(S[pos])
        del S[pos]
        k = k-1
        n = n-1
    return res

res = unrank(list('ABCDEFGHJKLMNPQRSTUVWXYZ0123456789'),3, 2222)
print (res) 

见原文post

【问题讨论】:

  • 我不明白这个问题。在“允许重复时”找到kth 排列是什么意思?你的意思是你有一个可能有重复的列表,你想按这些项目的字典顺序计算它是哪个排列? k 和 i 在你的unrank 函数中是什么意思? S 必须在同一函数中按排序顺序排列吗?
  • 允许重复的意思是我可以多次使用同一个字符。因此,如果 S={ABC} 且 k(单词中允许的字符数)为 3,则此渗透是有效的:AAA、AAC、ACC、BBB 等... S:是我可以选择的字符集。 K:是字母的个数。 i : 是排列索引的字典顺序
  • AAC 和 ACA 和 CAA 被认为是相同还是不同? S 是集合还是列表,如果列表必须是未排序的?
  • AAC 和 ACA 和 CAA 会被认为是不同的。 S 是一个顺序无关紧要的集合。它表示您可以从中选择的允许的字符集。

标签: python algorithm math permutation factorial


【解决方案1】:

简短回答:不,我看不到使用 Factoradic 基础系统做你想做的事的方法,但有一种更简单的方法可以做到这一点。只需使用通常的数字基数即可。

您的术语令人困惑,因为您写的是“排列”但允许重复。让我们将它们称为序列,其中给函数一个测试序列来检查和包含可以使用的字符的基本序列。您想使用基本序列中的字符在所有可能的相同长度序列的字典列表中查找测试序列的计数。

为方便起见,我们假设基本序列按递增顺序排列并且没有重复,如您的示例代码所示。

对于序列中的每个字符,我们想知道它在基本序列中出现的位置。如果基本序列和序列都很长,那么执行此操作的简单方法可能会很耗时,即对长度的乘积进行排序。有一种方法可以通过对长度的总和进行排序:首先对基本序列进行预处理以获得一个字典,将每个字符映射到其在基本序列中的位置,然后将我们测试序列中的每个字符转换为其在碱基序列。我们现在有一个基本序列中的字符位置列表。

这个列表就像一个以 N 为基数的数字,其中 N 是基序列的长度。然后我们用通常的方法将其转换为标准整数,这是我们想要的结果。

这里有一些代码来完成这一切。当然,还有其他方法可以做到这一点。

def sequence_position(test_seq, base_seq):
    """Return the count of the test sequence in the lexicographical
    listing of all possible sequences of the same length using the 
    items in the base sequence. Repetition of items is allowed and the 
    order of the items in the list matters.

    This function assumes the base sequence is in increasing order and
    has no repetitions.
    """
    # Create a dictionary mapping items in the base sequence to
    #   their positions in the base sequence.
    item_pos_dict = {item:pos for pos,item in enumerate(base_seq)}
    # Create a list of positions of the characters in the test sequence.
    positions = [item_pos_dict[item] for item in test_seq]
    # Convert this list of positions to its count in the lexicographical
    #   sequence of all such sequences of this length
    base = len(base_seq)
    result = 0
    for pos in positions:
        result = result * base + pos
    return result

print(sequence_position('ABC', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'))

【讨论】:

    【解决方案2】:

    只需在基数|S| 中写出数字(即,如果可能的字母集的大小)。以(某些)顺序使用集合中的元素作为“数字”。

    作为一个例子,考虑来自list('0123456789') 的三个元素的列表序列。请注意,正是从 0 到 999 的数字,必要时用前导零写成三位数。

    更新

    下面是一些说明这个想法的 Python 代码:

    def unrank(src, reqlen, pos):
        size = len(src)
        # it is easier to fill result from the end, so pre-create an array of the required size
        res = [src[0]] * reqlen
        for i in xrange(reqlen - 1, -1, -1):
            res[i] = src[pos % size]
            pos /= size
        if pos > 0: # this means original pos was beyond (size ** reqlen) i.e. more than the max possible index
            raise IndexError
        return res
    
    res = unrank(list('0123456789ABCDEF'), 3, 2222)
    print (res)
    

    打印出来的

    ['8', 'A', 'E']

    您可以轻松检查 0x8AE 的十六进制值实际上是 2222。

    【讨论】:

    • Rici,我冒昧地用一些代码扩展了你的答案。随意编辑它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多