Convert C code to python online free code example
Example 1: c code to python code converter online
// Defining MAX size to 10
using namespace std;
typedef struct Edge
{
int source;
int destination;
int weight;
}Edge;
void bellman_ford_algo(int nodevertex,Edge edge[],int source_graph,int nodeedge)
{
int u,v,weight,i,j=0;
int distance[MAX];
for(i=0;i<nodevertex;i++)
{
distance[i]=999;
}
// distance of source vertex
distance[source_graph]=0;
// free all the edges nodevertex - 1 times
for(i=0;i<nodevertex-1;i++)
{
for(j=0;j<nodeedge;j++)
{
u=edge[j].source;
v=edge[j].destination;
weight=edge[j].weight;
if(distance[u]!=999 && distance[u]+weight < distance[v])
{
distance[v]=distance[u]+weight;
}
}
}
// checking for negative cycle
for(j=0;j<nodeedge;j++)
{
u=edge[j].source;
v=edge[j].destination;
weight=edge[j].weight;
if(distance[u]+weight < distance[v])
{
cout<<"\n\nNegative Cycle present..!!\n";
return;
}
}
cout<<"\nVertex"<<" Distance from source";
for(i=1;i<=nodevertex;i++)
{
cout<<"\n"<<i<<"\t"<<distance[i];
}
}
int main()
{
int nodevertex,nodeedge,source_graph;
Edge edge[MAX];
cout<<"Enter the number of vertices you want : ";
cin>>nodevertex;
printf("Enter the source vertex of the graph: ");
cin>>source_graph;
cout<<"\nEnter no. of edges you want : ";
cin>>nodeedge;
for(int i=0;i<nodeedge;i++)
{
cout<<"\nEdge Number "<<i+1<<"=";
cout<<"\nEnter source vertex here :";
cin>>edge[i].source;
cout<<"Enter destination vertex here:";
cin>>edge[i].destination;
cout<<"Enter weight here :";
cin>>edge[i].weight;
}
bellman_ford_algo(nodevertex,edge,source_graph,nodeedge);
return 0;
}
Example 2: python code to c code converter
class Buffer:
def __init__(self,size_max):
self.max = size_max
self.data = []
class __Full:
def append(self, x):
self.data[self.cur] = x
self.cur = (self.cur+1) % self.max
def get(self):
return self.data[self.cur:]+self.data[:self.cur]
def append(self,x):
self.data.append(x)
if len(self.data) == self.max:
self.cur = 0
self.__class__ = self.__Full
def get(self):
return self.data
if __name__=='__main__':
n=input('Enter the occupide size of the buffer : ')
x=Buffer(int(n))
x.append(1); x.append(2); x.append(3); x.append(4)
print (x.__class__, x.get( ))
l=len(x.data)
print(f"The free buffer is :{l}")
print(f"The left size buffer is :{int(n)-int(l)}")
x.append(5)
print (x.__class__, x.get( ))
x.append(6)
print (x.data, x.get( ))
x.append(7); x.append(8); x.append(9); x.append(10)
print (x.data, x.get( ))