查找按长度分组匹配的曲目

发布于 2024-11-01 17:33:13 字数 925 浏览 7 评论 0原文

有人可以用所选的编程语言(最好是Python,但我猜任何东西都可以)提出这个问题的解决方案:

我有各种轨道长度组,比方说:

10:03
24:23
...

和源轨道本身:

1:03
9:00
4:24
...

我需要找到实际上哪些曲目属于上述长度组。如示例所示,前两首曲目属于第一组,因为它们的总长度等于组长度

提前感谢

编辑:这不是我的作业,因为那段时间已经过去了(我已经超过 30 岁了),但这是我遇到的问题,我我不是程序员。我会看看 itertools,谢谢

edit2:谢谢你的建议。我制作了 Python 脚本,并且对我来说运行良好且快速。它肯定没有优化,但这是骨架:

from itertools import combinations

tracks = [1,2,3,4,5,6,7,8,9]
group = 7

d_key, valid_tracks, possible_group =0, [], {}

for i in sorted(tracks):
    if i < group: valid_tracks.append(i)

for j in range(len(valid_tracks) - 2):
    for k in combinations(valid_tracks, len(valid_tracks) - 1 - j):
        if sum(k) <= group:
            if sum(k) == group:
                d_key += 1
                possible_group[d_key] = k

print possible_group

我很高兴我解决了这个问题,因为手动跟踪这个问题将花费我比我一生更多的时间,哈哈

can someone please come up with a solution to this problem, in programming language of choice (preferably Python, but anything could be fine I guess):

I have various track length groups, let's say:

10:03
24:23
...

and source tracks themselves:

1:03
9:00
4:24
...

and I need to find pragmatically which tracks belongs to above length group. As in example first two tracks belong to first group as their sum length is equal to group length

Thanks in advance

edit: It's not my homework as that time is long gone (I'm over 30) but it's a problem I have, and I'm not a programmer. I'll have a look at itertools, thanks

edit2: Thanks for you suggestions. I made Python script and if works fine and fast for me. It's sure not optimized, but here is skeleton:

from itertools import combinations

tracks = [1,2,3,4,5,6,7,8,9]
group = 7

d_key, valid_tracks, possible_group =0, [], {}

for i in sorted(tracks):
    if i < group: valid_tracks.append(i)

for j in range(len(valid_tracks) - 2):
    for k in combinations(valid_tracks, len(valid_tracks) - 1 - j):
        if sum(k) <= group:
            if sum(k) == group:
                d_key += 1
                possible_group[d_key] = k

print possible_group

I'm glad I solved this, as tracking this by hand would take me more then my life-time, ha-ha

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-11-08 17:33:13

查看Python的itertools模块:

http://docs.python.org/library/itertools.html

它支持计算曲目所有可能的排列()和组合()。

剩下的就取决于你了(不做你真正的作业)。

Look at the itertools module of Python:

http://docs.python.org/library/itertools.html

It supports to calculate all possible permutations() and combinations() of the tracks.

The rest is up to you (not doing your real homework).

提笔落墨 2024-11-08 17:33:13

这是一个困难的一般NP-complete问题,称为背包问题。您可以查看维基百科文章以了解其解决方案的常见方法,但在一般情况下它不会那么容易或快速。您还可以在此处检查 knapsack-problem 标签。

This is a hard general NP-complete problem, known as Knapsack problem. You can check the wikipedia article for common approaches to its solution, but it would not be that easy or fast in general case. You could also check the knapsack-problem tag here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文