P.I. Works Technical Question

package pyramid; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { String fileName; // name of the file which contains the pyramid StringBuffer sbr= new StringBuffer(); // used to read the file int arr [][]; Pyramid pyr; fileName = "/media/husnu/storage1/lessons/second_year/data_structures/hw/intern_interview/test.txt"; arr= readFile(fileName,sbr); pyr = new Pyramid(arr); printArr(arr); System.out.println(pyr.findMaxSum(pyr.getHead())); return; } public static void printArr(int arr[][]){ System.out.println("File content is being printed..."); for (int i = 0; i < arr.length; ++i) { for (int j = 0; j < arr[i].length; ++j) { System.out.print(arr[i][j]+" "); } System.out.println(); } System.out.println("File content is printed."); } /** * Reads the given file return line number, and fill the bufferedReader with content of the file * @param fileName // name of pyramid file * @param sbr // string buffer to keep file content reader * @return int // line number of the file */ public static int[][] readFile(String fileName, StringBuffer sbr){ int lineNum=0; int arr[][]; String line[]; Path path = Paths.get(fileName); File file =new File(path.toString()); BufferedReader br=null; try{ lineNum = (int)(Files.lines(path).count()); } catch (IOException e){ e.printStackTrace(); } arr = new int [lineNum][lineNum]; try{ String temp; int i=0; br = new BufferedReader(new FileReader(file)); while((temp=br.readLine())!=null){ line=temp.split(" "); for(int j=0; j<line.length; ++j){ arr[i][j]=Integer.parseInt(line[j]); } ++i; } } catch (FileNotFoundException fn){ fn.printStackTrace(); } catch (IOException ie){ ie.printStackTrace(); } return arr; } } class Pyramid{ private Node head; /** * Takes a two dimensional array and construct a linked list to represent pyramid * @param arr */ public Pyramid(int[][] arr) { head=fillPyramid(head, 0,0,arr); // printPyramid(head); } /** * Initialize pyramid with a number. * @param data Number that will be stored by head */ public Pyramid(int data){ head =new Node(data); } public Node getHead() { return head; } public void setHead(Node head) { this.head = head; } /** * Find maximum sum over the pyramid according to following rules. * 1. You will start from the top and move downwards to an adjacent number as in below. * 2. You are only allowed to walk downwards and diagonally. * 3. You can only walk over NON PRIME NUMBERS. * 4. You have to reach at the end of the pyramid as much as possible. * 5. You have to treat your input as pyramid. */ int findMaxSum(Node curr){ int mSum=0, leftSum,rightSum; if(curr==null){ return 0; } else if(curr.isPrime()==true){ return 0; } else if(curr.isPrime()==false){ leftSum=findMaxSum(curr.leftNode); rightSum=findMaxSum(curr.rightNode); if(leftSum>rightSum) return curr.data + leftSum; else return curr.data + rightSum; } return mSum; } /** * Fills pyramid as binary tree, because of this adjustment inside elements of pyramid * will be referenced twice. * @param currNode // current node (head, root) of binary tree * @param r // row of the two dimensional array * @param c // column of the two dimensional array * @param arr // the two dimensional array that keeps pyramid * @return Returns current head at each iteration and at the end returns head of binary tree */ public Node fillPyramid(Node currNode, int r, int c, int[][] arr){ if(arr.length>r && arr[r].length>c){ currNode=new Node(arr[r][c]); currNode.leftNode= fillPyramid(currNode.leftNode, r+1, c, arr); currNode.rightNode= fillPyramid(currNode.rightNode, (r+1), (c+1), arr); } return currNode; } /** * Prints binary tree * @param currNode */ public void printPyramid(Node currNode){ if(currNode!=null){ System.out.print(currNode.data+" "); // curr node's data is printed // if there is more node a parentheses is added to clarify leaf nodes if(currNode.leftNode!=null) System.out.print("{"); printPyramid(currNode.leftNode); printPyramid(currNode.rightNode); // if there is more node a parentheses is added to clarify leaf nodes if(currNode.leftNode!=null) System.out.print("}"); } } class Node { private int data; private Node leftNode = null, rightNode = null; public Node(int data) { this.data = data; } public Node() { /*Intentionally empty */} /** * Tells if the node contains a prime number or not. * @return true if data is prime, false if data is not prime */ boolean isPrime(){ boolean prime=true; if(data==2) { return true; } else if(data<2) { return false; } else { for(int i=2; i*i<=data && prime==true; ++i){ if(data%i==0) prime=false; } } return prime; } } }
To compile the code on your own computer, you only need to change fileName field. Also the txt file could not contain extra lines and extra spaces at the beginning.
File content example:

(* : border of text file)
*****************
* *
*
*
*****************

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.