python to c++ transpiler code example

Example 1: 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)

Example 2: python to c++ transpiler

a = input ()
b = input ()
if a % b == 0:
	print (a, 'и', b, 'делится без остатка')
else:
	print (a, 'и', b, 'делится с остатком')

Example 3: python to c++ transpiler

ask=int(input())

print((abs(ask)+10-(int(abs(ask)%10))+(int(abs(ask)/ask)-1)*5)*int(abs(ask)/ask))

Example 4: python to c++ transpiler

#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)

Example 5: python to c++ transpiler

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)

Example 6: python to c++ transpiler

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

Example 7: python to c++ transpiler

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()

Example 8: python to c++ transpiler

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

Example 9: python to c++ transpiler

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()

Example 10: python to c++ transpiler

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)

Tags:

Cpp Example