Answers for "python to c++ transpiler"

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)
Posted by: Guest on June-16-2020
0

python to c++ transpiler

test = int(input())
a = []
for i in range(test):
    S1 = input()
    S2 = input()
    X = input()
    a.append([S1,S2,X])

for i in a:
    count = 0
    for j in range(len(i[0])+1):
        if i[2].find(i[0][:j])==-1:
             break
        for k in range(len(i[1])+1):
            if i[2].find(i[0][:j]+i[1][:k])==-1:
                break
            else :
                count = count+1
    print(count)
Posted by: Guest on October-18-2021
0

python to c++ transpiler

a = input ()
b = input ()
if a % b == 0:
	print (a, 'и', b, 'делится без остатка')
else:
	print (a, 'и', b, 'делится с остатком')
Posted by: Guest on May-12-2021
0

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)
Posted by: Guest on April-15-2021
0

python to c++ transpiler

def __init__(self, n, connections):
    self.servers = set()
    for a, b, c in connections:
        self.servers.add(a)
        self.servers.add(b)
    self.parents = {x: x for x in self.servers}
    self.ranks = {x: 1 for x in self.servers}

def find(self, u):
    if u != self.parents[u]:
        self.parents[u] = self.find(self.parents[u])
    return self.parents[u]

def union(self, u, v):
    p1, p2 = self.find(u), self.find(v)
    if p1 == p2:
        return False
    if self.ranks[p2] > self.ranks[p1]:
        p1, p2 = p2, p1
    self.parents[p2] = p1
    self.ranks[p1] += self.ranks[p2]
    return True
class Solution:

def connect(self, num, connection):
    connection.sort(key = lambda x : x[2])
    uf = UnionFind(num, connection)
    min_cost = 0
    res = []
    for server1, server2, cost in connection:
        if uf.union(server1, server2):
            res.append([server1,server2,cost])
    return res
Posted by: Guest on August-31-2021
0

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))
Posted by: Guest on May-07-2021
0

python to c++ transpiler

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)
Posted by: Guest on December-30-2020
0

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)
Posted by: Guest on February-13-2021
0

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()
Posted by: Guest on January-15-2021
0

python to c++ transpiler

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)
Posted by: Guest on January-07-2021

Python Answers by Framework

Browse Popular Code Answers by Language