python - 将算法从 Python 转换为 C : Suggestions for Using bin() in C?

标签 python c

基本上,我有一个 写入 的问题,而不是走简单的路线,我想我会实现一个小算法和一些编码实践来打动我的教授。题目是帮助我们拿起C(或复习它,前者是我的),题目告诉我们返回所有除以给定整数的整数(这样就没有余数)。

我在 中做了什么是创建一个is_prime() 方法、一个pool_of_primes() 方法和一个combinations() 方法。到目前为止,我已经用 C 完成了所有工作,直到 combinations() 方法。我现在遇到的问题是一些语法错误(即无法通过声明更改字符串),主要是 我使用的目的是为了将包含在我的组合列表中的内容。但是无法通过声明更改我的字符串,Python 代码有点损坏...

这是 代码:

def combinations(aList):
    '''
        The idea is to provide a list of ints and combinations will provide
    all of the combinations of that list using binary.
        To track the combinations, we use a string representation of binary
    and count down from there. Each spot in the binary represents an
    on/off (included/excluded) indicator for the numbers.
    '''
    length      =   len(aList)  #Have this figured out
    s       =   ""
    canidates   =   0
    nList       =   []
    if (length >=21):
        print("\nTo many possible canidates for integers that divide our number.\n")
        return False
    for i in range(0,length):
        s       +=  "1"
        canidates   +=  pow(2,i)
    #We now have a string for on/off switch of the elements in our
    #new list. Canidates is the size of the new list.
    nList.append(1)
    while (canidates != 0):
        x   =   1
        for i in range(0,length):
            if(int(s[i]) == 1):
                x   =   x*aList[i]
        nList.append(x)
        canidates   -=  1
        s       =   ''
        temp        =   bin(canidates)
        for i in range(2,len(temp)):
            s   =   s+temp[i]
        if (len(s) != length):
                #This part is needed in cases of [1...000-1 = 0...111]
            while( len(s) != length):
                s   =   '0'+s
    return nList

如果整个代码太长或没有针对特定的适合进行优化,我们深表歉意。但它有效,而且效果很好:)

同样,我目前拥有 aList 拥有的一切,作为单链表存储在 中(我可以打印/使用)。我在 C 中也有一个小宏,用于将二进制转换为整数:

#define B(x) S_to_binary_(#x)
static inline unsigned long long S_to_binary_(const char *s)
{
    unsigned long long i = 0;
    while (*s) {
        i <<= 1;
        i += *s++ - '0';
    }
    return i;
}

这可能是 Coder 的 Block 设置,但我没有看到如何以与在 Python 中所做的相同的方式更改二进制文件...任何帮助将不胜感激!另外,请注意,在 C 语言中返回最终代码的最佳方式通常是什么?

编辑:

不小心占了上面宏的功劳。

更新

我刚刚完成代码,我uploaded it onto Github .我要感谢@nneonneo 提供了我用示例代码完成它所需的步骤。如果有人对代码有任何进一步的建议,我很乐意在 [Github] 上看到这些想法!

最佳答案

为什么要使用字符串?保持简单:使用整数,并使用按位数学来处理数字。然后您不必来回进行任何转换。它还会加载得更快。

您可以使用 uint32_t 来存储“位”,这足以容纳 32 位(因为您最多只能使用 21 位,所以这应该很好用)。

例如,您可以使用如下循环遍历设置的位:

uint32_t my_number = ...;

for(int i=0; i<32; i++) {
    if(my_number & (1<<i)) {
        /* bit i is set */
    }
}

关于python - 将算法从 Python 转换为 C : Suggestions for Using bin() in C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839469/

相关文章:

python - South django 不支持 mysql

Python scikit学习MLPClassifier "hidden_layer_sizes"

c - 为什么我们必须避免使用 conio.h?它过时了吗?

c - 基本指针 C 编程

c - realloc 删除 C 数组中已有的数据

python - NLTK 提取文本中存在的类别并映射到分类法

python - 为什么 60GB 内存在 MySQL 连接器 fetchall() 上消失?

python - 要求在 Ruffus 管道中运行函数之前创建一组文件

c - 动态分配和Matrix.Help me

iOS int类型默认值问题