【问题标题】:Porting Python algorithm to C++ - different solution将 Python 算法移植到 C++ - 不同的解决方案
【发布时间】:2010-04-14 14:49:32
【问题描述】:

感谢大家的帮助。在这篇文章的下方,我放置了两个脚本的更正版本,它们现在产生了相同的输出。


你好,

我在 python 中编写了一个粗略的字符串生成脚本来生成给定长度内的所有可能的字母组合。它工作得很好,但出于我不想让它更快的原因,我尝试将它移植到 C++。

问题是我的 C++ 代码为一个单词创建了太多的组合。 这是我在 python 中的示例:

./test.py

给我

aaa
aab
aac
广告
啊
阿坝
....

./test(c++ 程序给了我)

aaa
啊啊啊
啊啊啊
啊啊啊
啊

在这里我也得到了所有可能的组合,但我得到它们的频率更高。

这是两个程序的代码:

 #!/usr/bin/env python
 import sys
 #Brute String Generator
 #Start it with ./brutestringer.py 4 6 "abcdefghijklmnopqrstuvwxyz1234567890" ""
 #will produce all strings with length 4 to 6 and chars from a to z and numbers 0 to 9
 def rec(w, p, baseString):
    for c in "abcd":
        if (p<w - 1):
            rec(w, p + 1, baseString + "%c" % c)
         print baseString

 for b in range(3,4):
     rec(b, 0, "")

这里是 C++ 代码

 #include <iostream>
 using namespace std;
 string chars="abcd";

 void rec(int w,int b,string p){
    unsigned int i;
    for(i=0;i<chars.size();i++){
        if(b < (w-1)){
            rec(w, (b+1), p+chars[i]);
        }
        cout <<  p << "\n"; 
    }
 }


 int main ()
 {
    int a=3, b=0;
    rec (a+1,b, "");
    return 0;
 }

有人看出我的错吗?我对 C++ 没有太多经验。

真的很感谢


这里是更正的版本:

C++

#include <iostream>
using namespace std;
string chars="abcd";

void rec(int w,int b,string p){
    unsigned int i;
    for(i=0;i<chars.size();i++){
        if(b < (w)){
            rec(w, (b+1), p+chars[i]);
        }
    }
    cout << p << "\n";
}


int main ()
{
    rec (3,0, "");
    return 0;
}

Python

#!/usr/bin/env python
import sys

def rec(w, b, p):
    for c in "abcd":
        if (b < w - 1):
            rec(w, b + 1, p + "%c" % c)
    print p

rec(4, 0, "")

等量输出:

$ ./test > 1
$ ./test.py 3 3 "abcd" "" > 2
$ diff 1 2
$ 

【问题讨论】:

    标签: c++ python


    【解决方案1】:

    我认为 Python 代码也有问题,但您可能没有注意到,因为 print 缩进了太多空格(嘿,现在我看到了一个带有一次性错误的 Python 程序!)

    输出不应该只发生在else 的情况下吗?而输出出现频率更高的原因是你调用了print/cout 4次。我建议更改代码:

    def rec(w, p, baseString):
        if w == p:
            print baseString
        else:
            for ...
    

    【讨论】:

    • 谢谢,我没注意到python也坏了。并且输出也放错了位置:(它应该只打印一次字符串,而不是在每次迭代中(谢谢)。
    【解决方案2】:

    只是出于好奇,这够快吗?

    import itertools, string
    alphabet = string.lowercase + string.digits
    for numchars in (3, 4):
        for x in itertools.product(alphabet, repeat=numchars):
            print ''.join(x)
    

    (并确保将输出重定向到文件;在屏幕上滚动大量文本可能会非常慢)。

    【讨论】:

    • 我不知道 itertools.procuct。我尝试了 itertools.permutations 但这(当然)不是我需要的。这个解决方案非常快,但不如 C++ 代码快。谢谢大佬
    【解决方案3】:

    rec 中,字符串p 在循环的每次迭代中都会打印出来:

    for(i=0;i<chars.size();i++){
        // ...
        cout <<  p << "\n"; 
    }
    

    您发布的 Python 代码似乎也是如此,但可能与那里的缩进混在一起?您是否可能在 Python 文件中混合了制表符和空格,导致了令人惊讶的结果?

    【讨论】:

    • 感谢这一点。我没有看到我在循环语句中调用输出。这很有帮助。
    【解决方案4】:

    你说...:

    ./test.py

    给我

    aaa aab

    (等),但您发布的代码并非如此:您得到的是

    aa
    aa
    aa
    aa
    a
    

    前导aa 重复四次,等等。当然可以:print baseString 语句位于for c in "abcd": 的循环中,因此它必须执行四次。我想你想要循环中的print out —— 同样对于 C++ 代码,你也将输出语句放在循环中,所以它会重复。

    【讨论】:

    • 也谢谢你。循环内的打印是我的错误。我想知道为什么 test.py 会给你不同的输出。我再次对其进行了测试,我得到了 aaa、aab 等……我正在运行 OS X、Python 2.6.4
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 2014-10-11
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多