Example 1: c to python converter
import sys
def mergesort_2ndstep(l, r):
len_l = len(l)
len_r = len(r)
sorted_array = []
i,j = 0,0
while i < len_l:
num1 = l[i]
for x in range(j,len_r):
num2 = r[x]
if num2 < num1 :
sorted_array.append(num2)
j += 1
sorted_array.append(num1)
i += 1
if len(sorted_array) != len_l + len_r:
sorted_array[i+j:] = r[j:]
return sorted_array
def mergesort_1ststep(L,start,stop):
if stop - start > 1:
l = mergesort_1ststep(L,start,start + (stop-start)//2)
r = mergesort_1ststep(L,start + (stop-start)//2,stop)
L[start:stop] = mergesort_2ndstep(l,r)
return L[start:stop]
List_of_nums = []
file_to_open = "input1.txt"
try:
read_file = open(file_to_open,"r")
write_file = open("sameeraz.txt","w")
if read_file != None:
for line in read_file:
line = int(line)
List_of_nums.append(line)
mergesort_1ststep(List_of_nums,0, len(List_of_nums))
k = List_of_nums.pop()
for num in List_of_nums:
write_file.write(f"{num}\n")
write_file.write(f"{k}")
read_file.close()
write_file.close()
except:
print("file not found")
Example 2: python to c converter
import sys
def mergesort_2ndstep(l, r):
len_l = len(l)
len_r = len(r)
sorted_array = []
i,j = 0,0
while i < len_l:
num1 = l[i]
for x in range(j,len_r):
num2 = r[x]
if num2 < num1 :
sorted_array.append(num2)
j += 1
sorted_array.append(num1)
i += 1
if len(sorted_array) != len_l + len_r:
sorted_array[i+j:] = r[j:]
return sorted_array
def mergesort_1ststep(L,start,stop):
if stop - start > 1:
l = mergesort_1ststep(L,start,start + (stop-start)//2)
r = mergesort_1ststep(L,start + (stop-start)//2,stop)
L[start:stop] = mergesort_2ndstep(l,r)
return L[start:stop]
List_of_nums = []
file_to_open = "input1.txt"
try:
read_file = open(file_to_open,"r")
write_file = open("output.txt","w")
if read_file != None:
for line in read_file:
line = int(line)
List_of_nums.append(line)
mergesort_1ststep(List_of_nums,0, len(List_of_nums))
k = List_of_nums.pop()
for num in List_of_nums:
write_file.write(f"{num}\n")
write_file.write(f"{k}")
read_file.close()
write_file.close()
except:
print("file not found")
Example 3: convert python code to c online
def main():
csr_values = [2, 3, 1, 4,5]
col_idx = [1, 2, 0, 1,1]
row_ptr = [0, 2, 4,5]
csr_matrix = [
csr_values,
col_idx,
row_ptr
]
dense_matrix = [
[0, 3, 0],
[1, 4, 5],
[2, 0, 0],
]
res = [
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
]
n = len(dense_matrix)
csr_row = 0
for i in range(n):
start, end = row_ptr[i], row_ptr[i + 1]
for j in range(start, end):
col, csr_value = col_idx[j], csr_values[j]
for k in range(n):
dense_value = dense_matrix[k][csr_row]
res[k][col] += csr_value * dense_value
csr_row += 1
print(res)
if __name__ == '__main__':
main()