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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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+" ");
        }
    }
    }
}

Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Time complexity
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education