Python Forum
C to Python code conversion print problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C to Python code conversion print problem
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print command in super() akbarza 5 507 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  problem with spliting line in print akbarza 3 335 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  problem with print lists MarekGwozdz 4 613 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,035 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Problem with print variable in print.cell (fpdf) muconi 0 633 Dec-25-2022, 02:24 PM
Last Post: muconi
  I need to add data types to cython conversion python to c Good_AI_User 1 973 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  Problem with importing Python file in Visual Studio Code DXav 7 4,912 Jun-15-2022, 12:54 PM
Last Post: snippsat
  Conversion of Oracle PL/SQL(packages, functions, procedures) to python modules. DivyaKumar 2 6,417 Jul-09-2020, 04:46 PM
Last Post: srikanth7482
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,682 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python uppercase conversion conditions Jaypeng 7 2,893 Apr-29-2020, 11:24 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020