Python Forum

Full Version: C to Python code conversion print problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello! I need to convert this C code I made to Python:
https://gist.github.com/anakk1n/dadf2216...10c3839af5

My python code looks like this:
def algoritm2(v, n, d, c):
    p = 0
    j = -3
    k = 0
    for i in range(n-1):
        if (j >= n - 2):
            break
        if (p == 1):
            print(v[j+1], " ", end='')
            u = k = v[j + 1]
            i = j + 1
            c = c + 1
            p = 0
        else:
            u = -1
        j = i
        aux = v[i] + d + 1
        if (aux <= v[i + 1] and u == -1):
            print(v[i], " ", end='')
            k = v[i]
            c = c + 1
        else:
            if (((v[i] - v[i - 1] >= d) and u != v[i]) or (i == 0)):
                k = v[i]
                print(v[i], " ", end='')
                c = c + 1
            while (aux > v[j + 1] and j < n - 2):
                j = j + 1
                p = 1
    if (v[n - 1] - k >= d + 1):
        print(v[n-1], " ", end='')
    return c
Problem is that it doesn't list 100% the same thing. Did i do anything wrong in the conversion? Also, I changed the 'l' variable in the C code with the 'c' variable in python and in Python I made it return 'c', but it shouldn't affect my result
It is difficult for someone to diagnose your problem when the only thing you provide is code that doesn't work the way you want. What is it supposed to do? This code would be difficult to analyze even if I knew what it is supposed to do. Names like v, n, d, c, p, j and k don't provide a lot of information. What should the output be? How does your output differ?

If I am forced to guess the first interesting thing I see is:
for i in range(n-1)
Is n the length of v? If so, the loop does not process the last element in v.