Can you convert my java code to python? Code: import java.util.*; class Building { int x; int y[]; // add u to y only if the path length between u and y is 2. int count; Building(int x,int[] y,int count) { this.x=x; this.y = new int[y.length]; for(int i=0;i"); for(int j=0;j max) { max = build[i].count; } } System.out.println(max); // display count and buildings with max count for(int i=0;i
Can you convert my java code to python?
Code:
import java.util.*;
class Building
{
int x;
int y[]; // add u to y only if the path length between u and y is 2.
int count;
Building(int x,int[] y,int count)
{
this.x=x;
this.y = new int[y.length];
for(int i=0;i<y.length;i++)
{
this.y[i] = y[i];
}
this.count=count;
}
void display()
{
System.out.println("\nBuilding "+x+"->");
for(int j=0;j<y.length;j++)
{
System.out.print(y[j]+" ");
}
System.out.println("Count: "+count);
System.out.println();
}
}
public class BuildingDriver {
public static void main(String args[]) {
int n; // no. of buildings
int m; // no. of buses
Scanner in = new Scanner(System.in);
n = in.nextInt();
m = in.nextInt();
int[][] routes = new int[m][2];
Building[] build = new Building[n];
// read the m routes
for(int i=0;i<m;i++)
{
routes[i][0] = in.nextInt();
routes[i][1] = in.nextInt();
}
// determine the max no of buildings from which it is reachable
// and the buildings from which it is reachable
for(int i=0;i<n;i++)
{
int[] reachable = new int[n];
int nreach=0;
for(int j=0;j<n;j++)
{
if(i!=j)
{
// check whether there is direct route from j to i
for(int k=0;k<m;k++)
{
if(routes[k][0]==j && routes[k][1]==i)
{
reachable[nreach] = j;
nreach++;
break;
}
}
// check for route with path length 2
for(int k=0;k<m;k++)
{
if(routes[k][0]==j)
{
int adj = routes[k][1];
for(int l=0;l<m;l++)
{
if(routes[l][0]==adj && routes[l][1]==i)
{
boolean flag=false;
for(int p=0;p<nreach;p++)
{
if(reachable[p]==j)
{
flag=true;
break;
}
}
if(flag==false)
{
reachable[nreach] = j;
nreach++;
break;
}
}
}
}
}
}
}
reachable[nreach] = i;
Building b = new Building(i,reachable,nreach+1);
build[i] = b;
}
// display details
/*for(int i=0;i<n;i++)
build[i].display();*/
// find buildings that are max reachable and the count
int max=0;
for(int i=0;i<n;i++)
{
if(build[i].count > max)
{
max = build[i].count;
}
}
System.out.println(max);
// display count and buildings with max count
for(int i=0;i<n;i++)
{
if(build[i].count == max)
{
System.out.print(i+" ");
}
}
}
}
Step by step
Solved in 4 steps with 4 images