wingchuihk
BAN USER- 0of 0 votes
AnswersWrite all the possible numbers returned from a calculator pad where a start number move in a L direction in any directions(1-2moves)
- wingchuihk in United States
ie. From calculator pad. Start from 8 --> go in L shape (2up, 1right), and it returns 3
ie. Start from 2, (move 2 down, 1 left), it will be 7
ie. Start from 2(move 2 down, 1 right), it will be 9
ie. Start from 7(move 1 left, 2 up), it will be 2
ie. Start from 7(move 1 up, 2 left), it will be 6| Report Duplicate | Flag | PURGE
Google SDET Java
import java.util.*;
import org.junit.*;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
/**
* |1,2,3|
* |4,5,6|
* |7,8,9|
* |0|
*/
public class CalculatorExample extends TestCase{
int[][] calculator= {{1,2,3},{4,5,6},{7,8,9},{0}};
int[][] LMove= {{-1,-2},
{-1,2},
{1,2},
{1,-2},
{-2,-1},
{-2,1},
{2,-1},
{2,1}};
public int[] getXYByNumber(int number) {
int[] returnArray = new int[2];
if (number==0) {
returnArray[0]=3;
returnArray[1]=0;
return returnArray;
}
for (int i=0; i <3; i++) {
for (int j=0; j<3; j++) {
if (calculator[i][j]==number) {
returnArray[0]=i;
returnArray[1]=j;
break;
}
}
}
return returnArray;
}
public int getNumberByXY(int x, int y) {
try {
if ((x==3)&&(y<0)&&(y>3))
return 0;
if ((x<0)&&(x>4))
return -1;
if ((y<0)&&(y>3))
return -1;
return calculator[x][y];
}catch(Exception e) {
return -1;
}
}
public Set getAllLMoveNumber(int start) {
Set<Integer> set = new HashSet();
int[] xy=getXYByNumber(start);
for (int[] move : LMove) {
int x=xy[0]+move[0];
int y=xy[1]+move[1];
if (getNumberByXY(x,y)!=-1){
//System.out.println("x:"+x+",y:"+y);
//System.out.println("x:"+x+",y:"+y+",number:"+getNumberByXY(x,y));
set.add(getNumberByXY(x,y));
}
}
return set;
}
public void inputNumber(int input) {
//System.out.println("input:"+input);
Iterator itr = getAllLMoveNumber(input).iterator();
while(itr.hasNext())
System.out.println("Input ["+input+"]"+itr.next());
}
public void test_allPossibleNumber() {
inputNumber(1);
inputNumber(2);
inputNumber(3);
inputNumber(4);
inputNumber(5);
inputNumber(6);
inputNumber(7);
inputNumber(8);
inputNumber(9);
inputNumber(0);
}
}
Output:
Input [1]6
Input [1]8
Input [2]7
Input [2]9
Input [3]4
Input [3]8
Input [4]3
Input [4]9
Input [5]0
Input [6]1
Input [6]7
Input [7]2
Input [7]6
Input [8]1
Input [8]3
Input [9]0
Input [9]2
Input [9]4
Input [0]5
Input [0]9
RepDo you need dua for controlling husband? Contact Guru ji right now. He provides the best and simple dua to ...
RepSpent high school summers donating toy monkeys in Minneapolis, MN. At the moment I'm building glue in Edison, NJ ...
Rep
Repnicolealove786, Apple Phone Number available 24/7 for our Customers at Argus
I am Nicole from Beverly Hills, CA. I am working as a manager in Liberty Wealth Planner company. I also ...
RepDaniel Wattss, Cloud Support Associate at A9
Spent 2001-2007 getting to know pogo sticks for no pay. Enthusiastic about researching deodorant in Los Angeles, CA. Prior to ...
@Aditya
- wingchuihk June 02, 2018The phone number actually have 0 under the 8 like below. And 5 does not fulfilled the rule. Therefore it has none.
|1,2,3|
|4,5,6|
|7,8,9|
| ,0, |