Converting C code to Python print problems

This is the python algorithm:

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

The print for the C code looks like this:

Generated distance is 27 X coordinate of the 229 cobs that are supposed to be planted: 1 25 34 40 41 61 75 78 83 121 129 140 144 149 169 169 180 180 191 193 196 204 217 220 232 237 251 259 261 265 272 276 277 281 297 301 301 313 323 336 339 346 358 363 376 378 385 388 390 390 395 395 401 406 416 425 439 453 465 466 501 502 504 526 529 530 598 617 617 618 624 634 655 659 664 675 682 695 721 736 744 745 746 804 807 814 814 825 825 842 855 862 866 874 874 894 897 903 903 922 940 969 982 992 992 1000 1005 1031 1039 1041 1058 1068 1072 1075 1078 1082 1098 1111 1141 1172 1194 1197 1201 1206 1213 1227 1229 1235 1236 1243 1248 1250 1263 1265 1270 1289 1294 1301 1306 1307 1313 1318 1360 1362 1363 1380 1403 1432 1432 1458 1468 1472 1487 1488 1500 1527 1530 1538 1561 1568 1600 1601 1613 1614 1623 1625 1633 1633 1650 1658 1663 1677 1685 1717 1720 1729 1745 1750 1755 1765 1765 1789 1797 1810 1812 1817 1819 1820 1821 1836 1839 1848 1872 1883 1887 1899 1900 1903 1928 1945 1948 1957 1963 1969 1975 1977 1990 1990 1992 1993 2002 2007 2013 2019 2025 2078 2096 2100 2100 2167 2182 2183 2187 2214 2239 2264 2265 2265 2277

X coordinate of the cobs that will be kept: 1 34 75 121 149 180 217 251 281 313 346 376 406 439 501 529 598 634 664 695 736 804 842 874 903 940 969 1000 1031 1068 1098 1141 1172 1201 1229 1263 1294 1360 1403 1432 1468 1500 1530 1561 1600 1633 1663 1717 1745 1789 1817 1848 1883 1928 1957 1990 2019 2078 2167 2214 2264

And the python print code like this:

Generated distance is 27

X coordinate of the 229 cobs that are supposed to be planted:

1 25 34 40 41 61 75 78 83 121 129 140 144 149 169 169 180 180 191 193 196 204 217 220 232 237 251 259 261 265 272 276 277 281 297 301 301 313 323 336 339 346 358 363 376 378 385 388 390 390 395 395 401 406 416 425 439 453 465 466 501 502 504 526 529 530 598 617 617 618 624 634 655 659 664 675 682 695 721 736 744 745 746 804 807 814 814 825 825 842 855 862 866 874 874 894 897 903 903 922 940 969 982 992 992 1000 1005 1031 1039 1041 1058 1068 1072 1075 1078 1082 1098 1111 1141 1172 1194 1197 1201 1206 1213 1227 1229 1235 1236 1243 1248 1250 1263 1265 1270 1289 1294 1301 1306 1307 1313 1318 1360 1362 1363 1380 1403 1432 1432 1458 1468 1472 1487 1488 1500 1527 1530 1538 1561 1568 1600 1601 1613 1614 1623 1625 1633 1633 1650 1658 1663 1677 1685 1717 1720 1729 1745 1750 1755 1765 1765 1789 1797 1810 1812 1817 1819 1820 1821 1836 1839 1848 1872 1883 1887 1899 1900 1903 1928 1945 1948 1957 1963 1969 1975 1977 1990 1990 1992 1993 2002 2007 2013 2019 2025 2078 2096 2100 2100 2167 2182 2183 2187 2214 2239 2264 2265 2265 2277

X coordinate of the cobs that will be kept:

1 34 75 121 149 180 217 251 281 313 346 376 406 439 501 529 598 634 664 695 736 804 842 874 903 940 281 313 346 376 406 439 501 529 598 634 664 695 736 804 842 874 903 940 406 439 501 529 598 634 664 695 736 804 842 874 903 940 501 529 598 634 664 695 736 804 842 874 903 940 695 736 804 842 874 903 940 804 842 874 903 940 842 874 903 940 894 922 969 1000 1031 1068 1098 1141 940 969 1000 1031 1068 1098 1141 1068 1098 1141 1098 1141 1111 1141 1141 1172 1201 1229 1263 1294 1360 1403 1263 1294 1360 1403 1289 1318 1301 1360 1403 1360 1403 1318 1360 1403 1403 1403 1432 1468 1500 1530 1561 1600 1633 1663 1717 1745 1789 1817 1848 1883 1928 1957 1990 2019 2078 2167 2214 2264

As you can see in the python version at the coordinates that will be kept they just keep repeating over and over again till it somehow gets to the right end and i can’t figure out why. The code is kind of exactly the same with the C version. Any ideas?

void algoritm(int v[], int n, int d, int *l)
{
	int i, p = 0, j = -3, aux, u, k = 0;
	for (i = 0; i < n - 1; i++)

	{
		if (j >= n - 2) break;
		if (p == 1)
		{
			printf("%d ", v[j + 1]);
			u = k = v[j + 1];
			i = j + 1;
			*l = *l + 1;
			p = 0;
		}
		else u = -1;
		j = i;
		aux = v[i] + d + 1;
		if (aux <= v[i + 1] && u == -1)
		{
			printf("%d ", v[i]);
			k = v[i];
			*l = *l + 1;
		}
		else
		{
			if (((v[i] - v[i - 1] >= d) && u != v[i]) || (i == 0))
			{
				k = v[i];
				printf("%d ", v[i]);
				*l = *l + 1;
			}

			while (aux > v[j + 1] && j < n - 2)
			{
				j++;
				p = 1;
			}
		}
	}

	if (v[n - 1] - k >= d + 1) printf("%d", v[n - 1]);

}

This is the C code. Any help will be appreciated. Thanks