Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problem of get_kth_combination #12

Open
noobOriented opened this issue Oct 27, 2018 · 0 comments
Open

problem of get_kth_combination #12

noobOriented opened this issue Oct 27, 2018 · 0 comments
Assignees

Comments

@noobOriented
Copy link
Contributor

noobOriented commented Oct 27, 2018

突然想到,若總組合數實在太大,k 無法用 int 表示出來,那這個方法也是不合用的
在此情況,可以使用下列的 code (總組合要遠大於 n ,不然一直沒加成功會很慢)

def gen_n_random_combinations(
        entities: List[List[str]],
        n: int,
    ) -> List[List[str]]:
    assert all(entities)

    ids_set = set()
    while len(ids_set) < n:
        ids_set.add(
            tuple(
                random.randint(0, len(e) - 1) for e in entities
            )
        )

    result = [[e[i] for e, i in zip(entities, ids)] for ids in ids_set]
    return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants