Saturday, 5 January 2019

[ Hemant Kurmi ] Robot Room Cleaner - JAVA

Given a robot cleaner in a room modeled as a grid.
Each cell in the grid can be empty or blocked.
The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees.
When it tries to move into a blocked cell, its bumper sensor detects the obstacle and it stays on the current cell.
Design an algorithm to clean the entire room using only the 4 given APIs shown below.
interface Robot {
  // returns true if next cell is open and robot moves into the cell.
  // returns false if next cell is obstacle and robot stays on the current cell.
  boolean move();

  // Robot will stay on the same cell after calling turnLeft/turnRight.
  // Each turn will be 90 degrees.
  void turnLeft();
  void turnRight();

  // Clean the current cell.
  void clean();
}
Example:
Input:
room = [
  [1,1,1,1,1,0,1,1],
  [1,1,1,1,1,0,1,1],
  [1,0,1,1,1,1,1,1],
  [0,0,0,1,0,0,0,0],
  [1,1,1,1,1,1,1,1]
],
row = 1,
col = 3

Explanation:
All grids in the room are marked by either 0 or 1.
0 means the cell is blocked, while 1 means the cell is accessible.
The robot initially starts at the position of row=1, col=3.
From the top left corner, its position is one row below and three columns right.
Notes:
  1. The input is only given to initialize the room and the robot's position internally. You must solve this problem "blindfolded". In other words, you must control the robot using only the mentioned 4 APIs, without knowing the room layout and the initial robot's position.
  2. The robot's initial position will always be in an accessible cell.
  3. The initial direction of the robot will be facing up.
  4. All accessible cells are connected, which means the all cells marked as 1 will be accessible by the robot.
  5. Assume all four edges of the grid are all surrounded by wall.

We use a backtrack recursive method to solve this problem. Maintaining a hash set to contain the position that we have visited. Clean it up and add it into the set. Then for the four directions, move forward and continue backtrack, after that, return to original position and maintain original direction. then turn to another direction.

--------------------- 


We don't know the x,y co-ordinate. But we have to mark if we have reached a position before to avoid processing same state again and again (as we mark in standard DFS).
So, lets say current position is 0,0.
Then for every up move: x--
For every down move: x++
For every right move: y++
For every left move: y--
Most tricky part is:
when our recursive call will go back to last state, x,y,dir will be as before but robot's position is updated already. So, we have to move back the robot to earlier position with earlier direction!
class Solution {
    private Set<String> hasVisited;
    //private int direction; //0=UP, 1=RIGHT, 2=DOWN, 3=LEFT
    public void cleanRoom(Robot robot) {
        hasVisited = new HashSet<>();
        //assume current co-ordinate as (0,0)
        int x = 0;
        int y = 0;
        hasVisited.add(getCoOrdinate(x, y));
        //direction = 0;
        cleanRoomRecursively(robot, x, y, 0);
    }
    
    private void cleanRoomRecursively(Robot robot, int x, int y ,int direction){
        //System.out.println("x: " + x + " y: " + y + " d: "+ direction);
        robot.clean();
        //4 moves
        //go to same direction
        //System.out.println("straingt");
        goNextBlock(robot, direction, x, y);
        //go to 90
        //System.out.println("right");
        robot.turnRight();
        direction = (direction + 1) % 4;
        goNextBlock(robot, direction, x, y);
        
        //go to 180
        //System.out.println("back");
        robot.turnRight();
        direction = (direction + 1) % 4;
        goNextBlock(robot, direction, x, y);
        
        //go to 270
        //System.out.println("left");
        robot.turnRight();
        direction = (direction + 1) % 4;
        goNextBlock(robot, direction, x, y);
        
        //now it will go to the callee, which will have x, y and direction using which this 
        //function was called. But the robot has moved 270 degree and 1 block forward
        //So, put it back 1 block and face to the correct direction turn two time left or right in last it will turn 180
        robot.turnLeft();
        robot.move();
        robot.turnRight();
        robot.turnRight();
        //System.out.println("returning");
    }
    
    private void goNextBlock(Robot robot, int direction, int x, int y){
            switch (direction){
                case 0: x--;
                    break;
                case 1: y++;
                    break;
                case 2: x++;
                    break;
                case 3: y--;
                    break;
            };
            if(!hasVisited.contains(getCoOrdinate(x, y))){
                hasVisited.add(getCoOrdinate(x, y));
                if(robot.move()){
                    cleanRoomRecursively(robot, x, y, direction);        
                }
                
            }
        }
    
    private String getCoOrdinate(int x, int y){
        String coOrdinate = x + "," + y;
        //System.out.println(coOrdinate);
        return coOrdinate;
    }
}

Other solution
  1. To track the cells the robot has cleaned, start a index pair (i, j) from (0, 0). When go up, i-1; go down, i+1; go left, j-1; go right: j+1.
  2. Also use DIR to record the current direction of the robot
    public void cleanRoom(Robot robot) {
        // A number can be added to each visited cell
        // use string to identify the class
        Set<String> set = new HashSet<>();
        int cur_dir = 0;   // 0: up, 90: right, 180: down, 270: left
        backtrack(robot, set, 0, 0, 0);
    }

    public void backtrack(Robot robot, Set<String> set, int i, 
       int j, int cur_dir) {
     String tmp = i + "->" + j;
     if(set.contains(tmp)) {
            return;
        }
        
     robot.clean();
     set.add(tmp);

     for(int n = 0; n < 4; n++) {
        // the robot can to four directions, we use right turn
      if(robot.move()) {
       // can go directly. Find the (x, y) for the next cell based on current direction
       int x = i, y = j;
       switch(cur_dir) {
        case 0:
         // go up, reduce i
         x = i-1;
         break;
        case 90:
         // go right
         y = j+1;
         break;
        case 180:
         // go down
         x = i+1;
         break;
        case 270:
         // go left
         y = j-1;
         break;
        default:
         break;
       }

       backtrack(robot, set, x, y, cur_dir);
                       // go back to the starting position
   robot.turnLeft();
       robot.turnLeft();
       robot.move();
       robot.turnRight();
       robot.turnRight();

      } 
      // turn to next direction
      robot.turnRight();
      cur_dir += 90;
      cur_dir %= 360;
     }

    }




#hemant #kurmi 
#hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

Saturday, 12 September 2015

[Hemant Kurmi] - Encapsulation in Practical.

Encapsulation is one of the four fundamental OOP concepts. It is a technique used to protect a program's internal characteristics and behavior by creating private fields within a class and providing access to them through public methods. When a field is declared private, it cannot be accessed by anyone outside the class, effectively hiding the fields within the class. Encapsulation thus protects the code and data from unauthorized access by creating a protective wrapper around them.

Encapsulation can be described as a protective barrier that prevents code and data from being randomly accessed by other code defined outside the class. The main benefit of encapsulation is the ability to modify implemented code without breaking the code of others who use it. This feature provides maintainability, flexibility, and extensibility to the code.

For example, consider a data class with two fields: name and remark:

public class Result {
   private String name;
   private String remark;

   public String getName() {
      return name;
   }
   public void setName(String newName) {
      name = newName;
   }
   public String getRemark() {
      return remark;
   }

   public void setRemark(int remarkPoints) {
      if (remarkPoints >= 5) {
         remark = "good";
      }

      if (remarkPoints < 5) {
         remark = "poor";
      }
   }

}

In the example above, we set remarks according to remark points. However, if we need to add another remark ("very good") for students who get 10 points, we don't need to ask the user of the code to change it; instead, we can modify the setter: 

public void setRemark(int remarkPoints) { if (remarkPoints == 10) { remark = "very good"; } if (remarkPoints >= 5 && remarkPoints < 10) { remark = "good"; } if (remarkPoints < 5) { remark = "poor"; } }

Another example is a class Phone with a field weight. If it is not encapsulated, someone could modify it to have a negative weight. It's better to encapsulate the field using a private variable and provide a setter method where you can add logic for no negative and not over 100 grams.

Think of a real-life example of a car, which is a well-defined composite object with numerous subsystems like transmission, music system, air cooler, or heater system, and others based on the car's price. Each system has its protective layer, so the transmission system does not interfere with the music system or air cooler system. However, there is some integration, such as the transmission charging the battery, which helps to play music. Still, no one can directly modify the behavior of another system. This is how encapsulation hides the complexity of a program.

Benefits of Encapsulation:

  1. The fields of a class can be made read-only or write-only.
  2. A class can have total control over what is stored in its fields.
  3. The main benefit of encapsulation is the ability to modify implemented code without breaking the code of others who use it. Users of a class do not know how the class stores its data. A class can change the data type of a field, and users of the class do not need to change any of their code.










#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant 

Monday, 7 April 2014

[Hemant Kurmi] - Deploy war on web-logic server by web-logic console

In this post we will see how to deploy a war on weblogic by using weblogic console.

1)First start the admin server of your weblogic server  for this browse to the path of domain of your weblogic server .it will be some what like

D:\Oracle\Middleware\user_projects\domains

here you will get all the domains created
so in

D:\Oracle\Middleware\user_projects\domains\Domain_name\bin

you will get  startWebLogic.cmd or startWebLogic.sh
depending on your OS Just run it and you will see a cmd console

2)Now If your weblogic server is started succssfully it will display running in console




3) After starting weblogic server succssfully . Open the weblogic console from the below linkhttp://localhost:7001/console





where localhost is the machine address on which weblogic server is running.And 7001 is the port on which weblogic admin server is running.
4)Now it will ask for username or password just enter your domain username and password
and it will redirect to a screen.




5) Now click on deployment on the left side pannel and you will se a screen asking to browse
the war file.



6)Just browse to the war file want to deploy on weblogic server.





7) Now click next and it will ask to chose the server on which you want to deploy the war from
here you can select Admin server or any other managed server(If you chose other managed server
then you have to start it seprately from nodemanager or manully)





8) Now click next and select radio button which says copy the war on every target to uplaod
war on weblogic server



9) Now if deployment is completed then it will display that sataus of deployment as Active.

#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

Sunday, 2 March 2014

Some Advanced Ant Tasks (ANT Part-5)

In our last posts we have seen 

Sleep: If you want some gap between two tasks or want some pause then  this task is used to sleep the build process for a while. We can use milliseconds,seconds and hours also in aatribute. 

<sleep milliseconds="10"/> 

 Sql: To execute some query or after finishing a task you want to update some column in any table or want to fire any type of query then this task is used to execute SQL commands using Java JDBC connectivity. 
<sql 
 driver="jdbc driver name" 
 url="jdbc url" 
 userid="ui" 
 password="pass" 
 > 
insert 
insert into aots_users values (1, "aots1"); 
</sql> 

FTP: After creating any patch or folder you want to copy it on nay FTP to allow others to access it from another location then this task is used to ftp files to a ftp server 
 <ftp server="ftp.tcs.org" 
 remotedir="incoming" 
 userid="hemant12345" 
 password="hk@123" 
 depends="yes"> 
 <fileset dir="d:mypatch/currentpatch"/> 
 </ftp>

Sound: If you are running a build or any other task and do not want to sit and watch screen but you want to raise an alarm after finishing the task then you can use this task is to create sound during the build process for specific tasks. 
 <sound> 
 <success source="alert.wav"/> 
</sound>



Attrib: This task is used to set the properties of a file 
 <attrib file="username.properties" readonly="true" hidden="true"/> 

Sync:  To synchronize two folders we can use this task. This file will overwrite the data and also it will remove the files from the todir folder. 
<sync todir="Server"> 
 <fileset dir="local"/> 
</sync> 

LoadProperties: This task is used to load the contents of the properties file as ant properties.Means you do not need to rewrite the property file for ant in separately. 
The properties should be as key=value format 

 <loadproperties srcFile="file.properties"/>







#hemant #kurmi 
#hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi


What is the maximum ram for 32 bits and 64 bits computer?


Some Amazing fact about memory (petabyte,exabyte,zettabyte) And How much data exists on the Web?

Saturday, 1 March 2014

Some Core Ant Tasks (ANT Part-4)

In our last post we have seen 
Now we will see some core task which can be performed by using ANT. Below are some of the most useful tasks .

GUnzip: This task is used to unzip and expand the tar files with gz
extensions. These files are unziped in the current folder.

<gunzip src="abc.tar.gz" dest="aaa.tar"/> 



Gzip: This task is used to zip the source files using GZip  algorithms. The source(src) and the destination (destfile or zipfiles) attributes are mandatory 
<gzip src="aaa.tar" destfile="abc.tar.gz"/> 


Copy: Copy a file to a new file or to a new folder. 
<copy file="copythis.txt" tofile="tothis.txt"/> 


Delete: This task is used to delete a folder , file and folder's entire sub directories. the properties used with delete are includes, includesfile, excludes, excludesfile or defaultexcludes to add or 
remove files from deleting. But all these tasks are depreciated and it’s recommended to use 
resource collections. 
 <delete> 
 <fileset dir="." includes="**/*.bak"/> 
</delete> 

Ear: This task is used to create ear file for Enterprise Deployment. 
 <ear destfile="build/myproject.ear" appxml="src/metadata/application.xml"> 
 <fileset dir="build" includes="*.jar,*.war"/> 
 </ear>



Echo: This task is used to echo message during the build process. We can write the message in the console or to a specified file. We have options to setup the level of logging 
as error, warning, info, verbose and debug. 
<echo file="mylogfile.lgo" level="error"> 
build filed!!!!!!!!!!!!!!!! 
</echo> 
<echo message="Hello copied folders "/>

Jar: This task is used to create jar file from a specific folder. The includes or excludes 
properties can be used to add or prevent few files adding in the jar file 
 <jar destfile="${dist}/lib/myfirstjar.jar" 
 basedir="${build}/classes" 
 includes="com/abc/**" 
 excludes="**/TestDemo.class" 
/>

Mail: This task is used to send mail using SMTP.
<mail mailhost="smtp.gmail.com" mailport="1076" subject="Testing Mail.."> 
 <from address="hemant2422@gmail.com"/> 
 <replyto address="hemant2422@gmail.com"/> 
 <to address="myreaders@gmail.com"/> 
 <message>type your message here </message
 <attachments
 <fileset dir="thisfolder"> 
 <include name="nameoffiletobeincluded"/> 
 </fileset> 
 </attachments
</mail

Move: This task is used to move or remane a file to a new file or to a folder 
<move file="abc.txt" tofile="xyz.txt"/> - this can be also used for renaming the file

Retry: If you want to repeate a task which is faild or want to run it sevral time when it fails then you have to use this 
<retry retrycount="5"> 
 <get src="myjar.jar" 
 dest="/com/build/myjar.jar" /> 
</retry> 



You may also like:-

Using JConsole in java And creating memory dump by it


Heap analysis by Memory Analyzer (MAT) - Eclipse java


Main causes of Out-Of-Memory Errors in java


Five main types of Memory leaks in java


How to Reduce Garbage-Collection Pause Time?


4 Mains causes of Long Garbage Collection Pause?


How Application Performance get impacted by Garbage Collection?


Java Performance Tuning options Java heap


Why Memory leak in Java ?


What is a Memory Leak in java?


What is Garbage collector in java and how it works?


What is Garbage Collector Compaction in Java?


What are Java heap Young, Old and Permanent Generations?


What is difference between Web Server and Application Server ?


What is the maximum ram for 32 bits and 64 bits computer?


Some Amazing fact about memory (petabyte,exabyte,zettabyte) And How much data exists on the Web?







#hemant #kurmi 
#hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant#hemant #kurmi #hemant #kurmi #hemant #kurmi
#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi#hemant #kurmi

Hemant Kurmi - 2021 - Some Amazing Facts About Memory: Petabyte, Exabyte, and Zettabyte

Some Amazing Facts About Memory: Petabyte, Exabyte, and Zettabyte As technology advances, our ability to store and process data has grown ...