python to c++ transpiler
# Note: The last time I tested something was missing so I couldn't work
import pathlib
import transpyle
path = pathlib.Path('my_script.py')
code_reader = transpyle.CodeReader()
code = code_reader.read_file(path)
from_language = transpyle.Language.find('Python 3.6')
to_language = transpyle.Language.find('Fortran 95')
translator = transpyle.AutoTranslator(from_language, to_language)
fortran_code = translator.translate(code, path)
print(fortran_code)
4.17
6
ask=int(input())
print((abs(ask)+10-(int(abs(ask)%10))+(int(abs(ask)/ask)-1)*5)*int(abs(ask)/ask))
Thank you!
6
0
4
8
#is_Sorted method
def is_Sorted(li):
#for loop will iterate len(li)-1 times
for i in range(len(li) - 1):
#if li[i] > li[i+1]
if(li[i] > li[i+1]):
#return False
return False
#return True
return True
#initializing array
list1 = [16.1, 12.3, 22.2, 14.4]
#initializing array
list2 = [1.5, 4.3, 7.0, 19.5, 25.1, 46.2]
#calling is_Sorted method
is_Sorted(list1)
#calling is_Sorted method
is_Sorted(list2)
Thank you!
8
0
4
1
from mpi4py import MPI
from scipy import misc
import numpy as np
import math
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
newImage = []
kernel = np.array([
[0, -1, 0],
[-1, 4, -1],
[0, -1, 0]
])
def multiplication(FinalImage):
for i in range(start + 1, end + 1):
inner_arr = []
for j in range(1, width + 1):
val= FinalImage[i - 1, j - 1] * kernel[0, 0] + FinalImage[i - 1, j] * kernel[0, 1] + \
FinalImage[i - 1, j + 1] * kernel[0, 2]
val+= FinalImage[i, j - 1] * kernel[1, 0] + FinalImage[i, j] * kernel[1, 1] + FinalImage[
i, j + 1] * kernel[1, 2]
val+= FinalImage[i + 1, j - 1] * kernel[2, 0] + FinalImage[i + 1, j] * kernel[2, 1] + \
FinalImage[i + 1, j + 1] * kernel[2, 2]
inner_arr.append(val)
newImage.append(inner_arr)
if rank == 0:
image= misc.imread('nn.png')
num_OF_rows= image.shape[0]
num_OF_columns= image.shape[1]
for i in range (size-1):
comm.send([num_OF_rows, num_OF_columns], dest=i+1 , tag=1)
# tag 2 for gray scale
GrayImage= []
for i in range (size-1):
comm.send(image , dest=i+1 , tag=2)
for i in range (size-1):
GrayImage+= comm.recv(source=i+1 , tag=2)
misc.imsave("grayImage.png", GrayImage)
# padding
Paded_image= np.zeros((num_OF_rows + 2, num_OF_columns + 2))
Paded_image[1:num_OF_rows + 1, 1:num_OF_columns + 1] = image[0:num_OF_rows ,0:num_OF_columns, 0]
# tag 3 for HPF
for i in range ( size-1):
comm.send(Paded_image, dest=i+1, tag=3)
HPF_image= []
for i in range (size-1):
HPF_image+= comm.recv(source=i+1, tag=3)
misc.imsave("output.png", HPF_image)
print("OUTPUT IS DONE")
else:
Image_dimensions= comm.recv(source=0, tag=1)
height= Image_dimensions[0]
width= Image_dimensions[1]
num_rows= math.ceil(height / (size - 1))
start= (num_rows * (rank - 1))
end= start + num_rows
#end of the image reached
if end > height:
end= height
Colored_image = comm.recv(source=0, tag=2)
#GRAY STEP
new_img= []
for i in range(start, end):
inner_arr= []
for j in range(width):
value= Colored_image[i,j,0]
inner_arr.append(value)
new_img.append(inner_arr)
comm.send(new_img, dest=0, tag=2)
FinalImage= comm.recv(source=0, tag=3)
multiplication(FinalImage)
# Result
new_img= []
for i in range(len(newImage)):
inner_arr= []
for j in range(len(newImage[0])):
#ideal_highpass(highpass+domainsample)
#value=math.sqrt((newImage[i][j])*(newImage[i][j]))
#highpass
value = (newImage[i][j])
inner_arr.append(value)
new_img.append(inner_arr)
comm.send(new_img, dest=0, tag=3)
Thank you!
1
0
4.13
5
def fitness(cand):
candidato=[]
for ia in range (75):
soma1=0
soma2=0
soma3=0
ib=cand*12 #endereço do individuo dentro da matriz B
for j in range (nv):
soma1 = out[ia][ib+j] + soma1 #soma elementos do cluster 1
soma2 = out[ia][ib+j+nv] + soma2 #soma elementos do cluster 2
soma3 = out[ia][ib+nv*2] + soma3 #soma elementos do cluster 3
candidato.append(min(np.sqrt(soma1),np.sqrt(soma2),np.sqrt(soma3)))
return np.sum(candidato)
Thank you!
5
0
3.88
8
a = input ()
b = input ()
if a % b == 0:
print (a, 'и', b, 'делится без остатка')
else:
print (a, 'и', b, 'делится с остатком')
Thank you!
8
0
0
0
def find_zero_sum(list1,list2):
for loop1 in range(len(list1)):
for loop2 in range(len(list2)):
if(list1[loop1] != 999 or list2[loop2] != 999):
if(list1[loop1] + list2[loop2] == 0):
print('Required number in list1: ',list1[loop1])
print('Required number in list2: ',list2[loop2])
break
Thank you!
0
0
3.5
4
def out_write(lst):
for k in range(len(lst)):
if len(lst[k])>0:
# out.write("\n")
for i in lst[k]:
out.write(str(k+2)+' ')
for j in i:
out.write(str(j)+' ')
out.write("\n")
f=open("e_many_teams.in",'r')
open("e_many_teams.out","w").close()
out=open("e_many_teams.out",'a')
l=[]
base=[]
ingredients=set()
pizza_list_distinct={}
for i in f.readlines():
t=i[:len(i)]
pizza=t.split()
if len(l)==0:
base=pizza.copy()
l.append(pizza)
length=len(l)
del l[0]
for i in range (len(l)):
pizza_list_distinct[i]=set(l[i][1:])
for j in range(len(base)):
base[j]=int(base[j])
total_pizzas=base[0]
total_participants=(base[1]*2)+(base[2]*3)+(base[3]*4)
diff=total_participants-total_pizzas
if diff<0:
diff=total_participants
t2=[]
t3=[]
t4=[]
counter=diff
if total_participants>total_pizzas:
while diff>0:
if diff>=4 and base[3]>0:
base[3]-=1
diff-=4
elif diff>=3 and base[2]>0:
base[2]-=1
diff-=3
elif diff>=1 and base[1]>0:
base[1]-=1
diff-=2
counter=base[1]*2+base[2]*3+base[3]*4
count=0
counter1=counter
all_pizzas=[]
for j in l:
t=set(j[1:])
all_pizzas.append(t)
while counter>0:
if base[3]>0:
temp=set()
i=1
val=[]
while len(val)!=4:
for key,value in pizza_list_distinct.items() :
if len(val)==0:
if all_pizzas[0]==value:
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
del all_pizzas[0]
counter-=1
break
else:
if len(temp.union(value)) >= (len(temp) + len(value) -1):
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
all_pizzas.remove(value)
counter-=1
break
base[3]-=1
t4.append(val)
val=[]
if base[2]>0:
temp=set()
i=1
val=[]
while len(val)!=3:
for key,value in pizza_list_distinct.items() :
if len(val)==0:
if all_pizzas[0]==value:
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
del all_pizzas[0]
counter-=1
break
else:
if len(temp.union(value)) >= (len(temp) + len(value) -1):
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
all_pizzas.remove(value)
counter-=1
break
base[2]-=1
t3.append(val)
val=[]
if base[1]>0:
temp=set()
i=1
val=[]
while len(val)!=2:
for key,value in pizza_list_distinct.items() :
if len(val)==0:
if all_pizzas[0]==value:
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
del all_pizzas[0]
counter-=1
break
else:
if len(temp.union(value)) >= (len(temp) + len(value) -1):
temp=temp.union(pizza_list_distinct[key])
val.append(key)
del pizza_list_distinct[key]
all_pizzas.remove(value)
counter-=1
break
base[1]-=1
t2.append(val)
val=[]
else:
continue
count=(len(t2)+len(t3)+len(t4))
output=[t2,t3,t4]
out.write(str(count))
out.write("\n")
out_write(output)
Thank you!
4
0
3.75
4
def isEven(number):
if number%2==0: node 4
print(f'{number} is even')
else: node 5
print(f'{number} is odd')
def isPrime(number):
if number > 1: node 6
for i in range(2,number):
if (number % i) == 0:
print(number,"is not a prime number")
print(i,"times",number//i,"is",number)
break
else: node 7
print(number,"is a prime number") node 8
break
else:
print(number,"is not a prime number")
print('Please enter a number between 1-10')
number = int(input()) node 1
print('Which operation you want to perform?')
print('1- Check if Even or ODD \n2- Check if Prime\n')
choice = int(input())
if choice == 1: node 2
isEven(number)
if choice == 2: node 3
isPrime(number)
Thank you!
4
0
4.14
7
from tkinter import*
def btnClick(numbers):
global operator
operator = operator + str(numbers)
text_Input.set(operator)
def btnClearDisplay() :
global operator
operator=""
text_Input.set("")
def btnEqualsInput() :
global operator
sumup=str(eval(operator))
text_Input.set(sumup)
operator=""
cal = Tk()
cal.title("Calculator")
operator = ""
text_Input = StringVar()
txtDisplay = Entry(cal, font=("arial", 20, "bold"), textvariable=text_Input, bd=30, insertwidth=4,
bg="black", justify="right", fg="white").grid(columnspan=4)
btn7 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="7", command=lambda: btnClick(7)).grid(row=1, column=0)
btn8 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="8", command=lambda: btnClick(8)).grid(row=1, column=1)
btn9 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="9", command=lambda: btnClick(9)).grid(row=1, column=2)
Addition = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="+", command=lambda: btnClick("+")).grid(row=1, column=3)
btn4 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="4", command=lambda: btnClick(4)).grid(row=2, column=0)
btn5 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="5", command=lambda: btnClick(5)).grid(row=2, column=1)
btn6 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="6", command=lambda: btnClick(6)).grid(row=2, column=2)
Subtraction = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="-", command=lambda: btnClick("-")).grid(row=2, column=3)
btn1 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="1", command=lambda: btnClick(1)).grid(row=3, column=0)
btn2 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="2", command=lambda: btnClick(2)).grid(row=3, column=1)
btn3 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="3", command=lambda: btnClick(3)).grid(row=3, column=2)
Multiplication = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="*", command=lambda: btnClick("*")).grid(row=3, column=3)
btn0 = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="0", command=lambda: btnClick(0)).grid(row=4, column=0)
btnClear = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="C", command=btnClearDisplay).grid(row=4, column=1)
btnEquals = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="=", command=btnEqualsInput).grid(row=4, column=2)
Division = Button(cal, padx=16, pady=10, bd=8, fg="black", font=("arial", 20, "bold"),
text="/", command=lambda: btnClick("/")).grid(row=4, column=3)
cal.mainloop()
Thank you!
7
0
Are there any code examples left?
New code examples in category C++
-
C++ 2023-04-28 17:44:25
-
C++ 2022-03-27 19:20:39 lists occurrences of characters in the string c++
-
C++ 2022-03-27 18:00:14 variabili in c++
-
C++ 2022-03-27 17:10:08 repeat character n times c++
-
C++ 2022-03-27 15:50:07 delete an array c++
-
C++ 2022-03-27 15:40:12 C++ pointer to base class
-
C++ 2022-03-27 12:15:21 find the graph is minimal spanig tree or not
-
C++ 2022-03-27 11:30:15 multi variable assignment cpp
-
C++ 2022-03-27 11:05:17 c++ pi float
-
C++ 2022-03-27 10:20:12 why exceptions can lead to memory leaks