Epic Systems Interview Question
Software Engineer / DevelopersHow about this one? :P
#include <stdio.h>
#include <stdlib.h>
#define ANSWER "1478"
// 1178
int matrix[3][3] = {
{1,2,3},
{4, 5,6},
{7, 8, 9},
};
struct numPosition{
int row, col;
};
int getnumPosition( int num, struct numPosition *pos){
int i, j;
for( i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
if( matrix[i][j] == num){
pos->row = i;
pos->col = j;
return 1;
}
}
}
return 0;
}
//0: No
//1: Yes
int is_neighbour( int userVal, int val){
int
i,
posNeighbour = 0,
validRow=0, validCol=0,
nbr[4];
struct numPosition pos;
if( !getnumPosition(val, &pos) ){
//handle error
return -1;
}
//printf("position of %d: [%d, %d]\n", val, pos.row, pos.col);
//finding possible neighbours
if( pos.col-1 >= 0)
nbr[0] = matrix[pos.row][pos.col-1];
else
nbr[0] = 0;
if( pos.col+1 < 3)
nbr[1] = matrix[pos.row][pos.col+1];
else
nbr[1] = 0;
if( pos.row-1 >=0 )
nbr[2] = matrix[pos.row-1][pos.col];
else
nbr[2] = 0;
if( pos.row+1 < 3 )
nbr[3] = matrix[pos.row+1][pos.col];
else
nbr[3] = 0;
for( i = 0; i <= 3; i++){
//printf("neighbours: %d\n", nbr[i]);
if( userVal == nbr[i])
return 1;
}
if( i == 3)
return 0;
return 0;
}
int main( int argc, char *argv[]){
int
userValue = 0,
answer = 0,
i =0;
char *strCode = argv[1];
char *strAnsw = ANSWER;
char *tmpChar = NULL;
if( argc <2){
printf("Please give key with cmd..");
return 0;
}
printf("Entered Value: %s\n", strCode);
tmpChar = (char *) malloc( sizeof(char ) );
for( i = 0 ; i < 4; i++){
//printf("User enter: %c\n", strCode[i]);
*tmpChar = strCode[i];
userValue = atoi( tmpChar );
*tmpChar = strAnsw[i];
answer = atoi( tmpChar);
if( userValue == answer || is_neighbour(userValue ,answer ) ){
//printf("correct input..\n");
}else{
printf("Access Denied.. \n");
return -1;
}
}
if( i == 4){
printf("Welcome User");
return 0;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main (){
char const keypad[][3] = { {'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
char const sec_code[] = "1924";
char password[8] = "0";
char temp[2]="0";
int i, code_len, pwd_len, row, col;
//take input from user as a string
printf("Please enter the password\n");
scanf("%s",password);
if((code_len=strlen(sec_code))!=(pwd_len=strlen(password))) //if lengths unequal deny access
printf("\nAccess Denied");
else{
for(i=0; i < code_len; i++){ //check each character
if(password[i] == sec_code[i]) continue;
else{
//find row and col values for sec_code[i] in keypad
temp[0] = sec_code[i]; //converting to string for
temp[1] = "\0"; //direct conversion to int
row = (atoi(temp) - 1)/3;
col = (atoi(temp) - 1)%3;
//check if it's equal to any vertical or horizontal keys
if(col-1 >= 0 && password[i] == keypad[row][col-1]) continue;
else if(col+1 < 3 && password[i] == keypad[row][col+1]) continue;
else if(row-1 >= 0 && password[i] == keypad[row-1][col]) continue;
else if(row+1 < 3 && password[i] == keypad[row+1][col]) continue;
else break;
}
}
if(i == code_len) printf("\nLogging In...");
else printf("\nAccess Denied");
}
return 0;
}
Cant we directly compare the row and column of the current digit and expected digit like this
expected[0] = password[i];
expected[1] = "\0";
expected_row = (atoi(expected) - 1)/3;
expected_col = (atoi(expected) - 1)%3;
if(row == expected_row || col == expected_col )
This will reduce comparisions..
It will not work. suppose 7518 is security code and you entered 7538. because the test "if(row == expected_row || col == expected_col )" results in true the entered code is approved, which should not happen.
That's how I compare whether 2 digits are considered equivalent too. And if the security code is 7518 and user entered 7538, I think the user should be allowed to enter because the codes only differ in 1 digit and that digit is considered equivalent.
I guess this will work
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
bool check_keyword(string correct_keyword, string enter_keyword);
bool check_digit(char ch1, char ch2);
string keypad_mapping[9] = {"24", "135", "26", "157", "2468", "359", "48", "579", "68"};
int main()
{
string correct_keyword;
string enter_keyword;
cin >> correct_keyword;
cin >> enter_keyword;
cout << check_keyword(correct_keyword, enter_keyword);
}
bool check_keyword(string ckey, string ekey)
{
if(ckey != "" && ekey == "")
return false;
if(ckey == "" && ekey == "")
return true;
if(ckey == "" && ekey != "")
return true;
if(ckey.length() != ekey.length())
return false;
bool right = true;
bool one_digit_wrong = false;
for(int i = 0; i < ckey.length(); i++)
{
if(ckey[i] != ekey[i])
if(!one_digit_wrong)
{
right = right && check_digit(ckey[i], ekey[i]);
one_digit_wrong = true;
}
else
return false;
}
return right;
}
bool check_digit(char ch1, char ch2)
{
int num = atoi(&ch1);
string allowed = keypad_mapping[num-1];
for(int i = 0; i < allowed.length(); i++)
if(ch2 == allowed[i])
return true;
return false;
}
class SecuriyKiosk
{
int MAXUSERID;
int Keypad[3][3];
std::string password[5];
std::string Input;
bool validateInput();
int UID;
void GetRowCol(int digit,int * row,int * col);
public:
SecuriyKiosk();
void initialze();
void SetInput(std::string UInput){Input = UInput;};
std::string GetInput(){return Input;};
bool GetandValidatePWD(int ID,std::string Input);
};
bool SecuriyKiosk::validateInput()
{
if(UID == 0 || UID>MAXUSERID)
{
return false;
}
if ((password[UID-1]).length() != Input.length())
{
return false;
}
// check for digits tooo....
return true;
}
bool SecuriyKiosk::GetandValidatePWD(int ID,std::string UserInput)
{
bool bValid = false;
int InputLen = 0;
int pwdDigit = 0;
int StoredPwdDigit = 0;
std::string StoredPwd;
SetInput(UserInput);
UID = ID;
int irowd,icold; //row col for entered digit
int irows,icols;//row col for stroed;
int RowTol,ColTol;
if( true == validateInput())
{
StoredPwd = password[ID-1];
InputLen = UserInput.length();
if(UserInput.compare(StoredPwd) == 0)
bValid = true;
else
{
for(int i=0; i<InputLen;i++)
{
pwdDigit = atoi(UserInput.substr(i,1).c_str());
StoredPwdDigit = atoi(StoredPwd.substr(i,1).c_str());
if(pwdDigit != StoredPwdDigit)
{
//Check tolerence
GetRowCol(pwdDigit,&irowd,&icold);
GetRowCol(StoredPwdDigit,&irows,&icols);
RowTol = abs(irowd-irows);
ColTol = abs(icold-icols);
if(!bValid && (RowTol+ColTol) == 1)
{
bValid = true;
continue;
}
else
{
bValid = false;
break;
}
}
}
}
}
return bValid;
}
void SecuriyKiosk::GetRowCol(int digit,int * row,int * col)
{
int irow = 0;
int jCol = 0;
for(irow=0;irow<3;irow++)
{
for ( jCol=0;jCol<3;jCol++)
{
if(Keypad[irow][jCol] == digit)
{
*row = irow;
*col = jCol;
return;
}
}
}
return;
}
SecuriyKiosk::SecuriyKiosk()
{
int count = 1;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
Keypad[i][j]= count++;
}
}
password[0] = "1234";
password[1] = "4231";
password[2] = "3213";
password[3] = "9574";
password[4] = "9987";
UID = 0;
MAXUSERID = 5;
}
int _tmain(int argc, _TCHAR* argv[])
{
SecuriyKiosk sk;
if(!sk.GetandValidatePWD(1,"1237"))
{
cout<<"Not Entry";
}
else
{
cout<<"PLease enter";
}
return 0;
}
<pre lang="" line="1" title="CodeMonkey13854" class="run-this">using System;
using System.Collections;
namespace Tester
{
class Program
{
private static Hashtable adjacence = new Hashtable()
{
{'1', new ArrayList() {'2','4'}},
{'2', new ArrayList() {'1','3','5'}},
{'3', new ArrayList() {'2','6'}},
{'4', new ArrayList() {'1','5','7'}},
{'5', new ArrayList() {'2','4','6','8'}},
{'6', new ArrayList() {'3','5','9'}},
{'7', new ArrayList() {'4','8'}},
{'8', new ArrayList() {'5','7','9'}},
{'9', new ArrayList() {'6','8'}}
};
private static char[] validCode = { '1','6','3','4' };
static void Main(string[] args)
{
test();
ConsoleKeyInfo inf = Console.ReadKey();
while (inf.KeyChar != ' ')
{
test();
}
}
private static void test()
{
bool tolerated = false;
bool matched = false;
string invalidCode = "Code Not Accepted";
string validCode = "Code Accepted";
string input = Console.ReadLine();
if (input.Length != 4)
{
Console.Out.WriteLine(invalidCode);
}
else
{
char[] inputCode = input.ToCharArray();
for (int i = 0; i < inputCode.Length; i++)
{
if (validCode[i] == inputCode[i] || (!tolerated && checkAdjacent(inputCode[i], validCode[i])))
{
if (i == 3) matched = true;
continue;
}
else
{
matched = false;
break;
}
}
}
Console.Out.WriteLine(matched ? validCode : invalidCode);
}
private static bool checkAdjacent(char input, char expected)
{
if (((ArrayList)adjacence[expected]).Contains(input))
{
return true;
}
return false;
}
}
}
</pre>
Assuming the pin is of 4 digit, here is the code.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char code[]="1478";
int found = 0;
char match[5];
void matchcode(char* in,char* out,int recurlevel);
int main(){
int a,i=3;
char str[5],out[5];
printf("Enter the code:");
scanf("%d",&a);
while(a>0){
str[i] = '0'+ a%10;
a=a/10;
i--;
}
str[4]='\0';
out[4]='\0';
match[4]='\0';
matchcode(str,out,0);
if(!found)
printf("Access Denied\n");
else
printf("Access Granted with code=%s\n",match);
}
int getdigit(int digit,int pos){
switch(digit){
case 1:
if(pos==0)
return 1;
if(pos==1)
return 2;
if(pos==2)
return 4;
break;
case 3:
if(pos==0)
return 2;
if(pos==1)
return 3;
if(pos==2)
return 6;
break;
case 7:
if(pos==0)
return 4;
if(pos==1)
return 7;
if(pos==2)
return 8;
break;
case 9:
if(pos==0)
return 6;
if(pos==1)
return 8;
if(pos==2)
return 9;
break;
case 2:
if(pos==0)
return 1;
if(pos==1)
return 2;
if(pos==2)
return 3;
if(pos==3)
return 5;
break;
case 4:
if(pos==0)
return 1;
if(pos==1)
return 4;
if(pos==2)
return 5;
if(pos==3)
return 7;
break;
case 6:
if(pos==0)
return 3;
if(pos==1)
return 5;
if(pos==2)
return 6;
if(pos==3)
return 9;
break;
case 8:
if(pos==0)
return 5;
if(pos==1)
return 7;
if(pos==2)
return 8;
if(pos==3)
return 9;
break;
case 5:
if(pos==0)
return 2;
if(pos==1)
return 4;
if(pos==2)
return 5;
if(pos==3)
return 6;
if(pos==4)
return 8;
break;
}
}
void matchcode(char* in,char* out,int recurlevel){
int i,j,loop;
if(recurlevel==4){
if(!strcmp(out,code)){
found = 1;
strcpy(match,out);
}
return;
}
j=in[recurlevel]-'0';
if((j%2)==0)
loop = 4;
else if(j==5)
loop = 5;
else
loop = 3;
for(i=0;i<loop;i++) {
out[recurlevel] = getdigit(j,i)+ '0';
matchcode(in,out,recurlevel+1);
if(found)
return;
}
}
If Only single digit error is allowed, here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char code[]="1478";
int found = 0;
char match[5];
void matchcode(char* in,char* out,int recurlevel);
int main(){
int a,i=3;
char str[5],out[5];
printf("Enter the code:");
scanf("%d",&a);
while(a>0){
str[i] = '0'+ a%10;
a=a/10;
i--;
}
str[4]='\0';
out[4]='\0';
match[4]='\0';
for(i=0;i<4;i++){
matchcode(str,out,i);
if(found)
break;
}
if(!found)
printf("Access Denied\n");
else
printf("Access Granted with code=%s\n",match);
}
int getdigit(int digit,int pos){
switch(digit){
case 1:
if(pos==0)
return 1;
if(pos==1)
return 2;
if(pos==2)
return 4;
break;
case 3:
if(pos==0)
return 2;
if(pos==1)
return 3;
if(pos==2)
return 6;
break;
case 7:
if(pos==0)
return 4;
if(pos==1)
return 7;
if(pos==2)
return 8;
break;
case 9:
if(pos==0)
return 6;
if(pos==1)
return 8;
if(pos==2)
return 9;
break;
case 2:
if(pos==0)
return 1;
if(pos==1)
return 2;
if(pos==2)
return 3;
if(pos==3)
return 5;
break;
case 4:
if(pos==0)
return 1;
if(pos==1)
return 4;
if(pos==2)
return 5;
if(pos==3)
return 7;
break;
case 6:
if(pos==0)
return 3;
if(pos==1)
return 5;
if(pos==2)
return 6;
if(pos==3)
return 9;
break;
case 8:
if(pos==0)
return 5;
if(pos==1)
return 7;
if(pos==2)
return 8;
if(pos==3)
return 9;
break;
case 5:
if(pos==0)
return 2;
if(pos==1)
return 4;
if(pos==2)
return 5;
if(pos==3)
return 6;
if(pos==4)
return 8;
break;
}
}
void matchcode(char* in,char* out,int level){
int i,j,loop;
j=in[level]-'0';
if((j%2)==0)
loop = 4;
else if(j==5)
loop = 5;
else
loop = 3;
for(i=0;i<loop;i++) {
strcpy(out,in);
out[level] = getdigit(j,i)+ '0';
if(!strcmp(out,code)){
found = 1;
strcpy(match,out);
}
if(found)
return;
}
}
public class SecurityKeypad {
String[] keyPadErr = {"24", "135", "26", "157", "2468", "359", "48", "579", "68"};
String validKey = "3256";
public boolean testKey(String inpKey)
{
if (inpKey.equals(validKey))
return true;
else if (inpKey.length() != validKey.length())
return false;
else
{
int errCnt = 0;
boolean forgiveErr = false;
for (int i =0 ;i < inpKey.length(); i++)
{
if (!(inpKey.charAt(i) == validKey.charAt(i)))
{
errCnt++;
String errDig = keyPadErr[Integer.parseInt(""+validKey.charAt(i))-1];
for(int j =0 ;j < errDig.length(); j++)
{
if (errDig.charAt(j) == inpKey.charAt(i))
{
forgiveErr = true;
break;
}
}
}
if (errCnt > 1)
return false;
}
if (forgiveErr == false)
return false;
else
return true;
}
}
public static void main(String args[])
{
SecurityKeypad testKeyPad = new SecurityKeypad();
System.out.println(testKeyPad.testKey("3266"));
}
}
private int securePassword=1478;
public SecuredAccess(String passwordEntered){
if(verifyAccess(passwordEntered)){
System.out.println("Allowed Access");
}
else{
System.out.println("DENIED Access");
}
}
private boolean verifyAccess(String passwordEntered){
if(passwordEntered==null)
return false;
char[] enteredPasswordasChar= passwordEntered.toCharArray();
char[] securePasswordasChar= new Integer(securePassword).toString().toCharArray();
if(enteredPasswordasChar.length!=securePasswordasChar.length)
return false;
Integer temp=new Integer("0");
boolean allow=false;
for(int i=0;i<enteredPasswordasChar.length;i++){
allow=false;
if(enteredPasswordasChar[i]==securePasswordasChar[i]){
allow=true;
}
if((enteredPasswordasChar[i]-securePasswordasChar[i])==1 && (((int)securePasswordasChar[i])-48)%3>0){
allow=true;
}
if(enteredPasswordasChar[i]-securePasswordasChar[i]==-1 && (((int)securePasswordasChar[i])-48)%3!=1){
allow=true;
}
if(enteredPasswordasChar[i]-securePasswordasChar[i]==3 &&(((int)securePasswordasChar[i])-48)<7){
allow=true;
}
if(enteredPasswordasChar[i]-securePasswordasChar[i]==-3 &&(((int)securePasswordasChar[i])-48)>3){
allow=true;
}
if(!allow){
return false;
}
}
return true;
}
}
#include<stdio.h>
#include<iostream>
#define row 3
#define col 3
using namespace std;
void check(int,int);
bool validate(int,int);
int main()
{
check(2489,2459);
check(1379,1389);
system("PAUSE");
return 0;
}
void check(int acode,int ecode)
{
int i,j,cnt=0,dig1,dig2,num1,num2;
//find the error is in whcih digit and if its only one digit
j=1;
for(i=1;i<5;i++)
{
if(i==4)
{
dig1=acode/1000;
dig2=ecode/1000;
}else{
dig1=(acode%(j*10))/j;
dig2=(ecode%(j*10))/j;
}
j=j*10;
if(dig1!=dig2)
{
num1=dig1;
num2=dig2;
cnt++;
}
}
if(cnt==1)
{
if(validate(num1,num2))
printf("\nsecurity code is allowed");
else
printf("\nsecurity code is not allowed");
}
else if(cnt>1)
printf("\nonly one digit error allowed");
else
printf("\nnumbers entered are same");
}
bool validate(int dig1,int dig2)
{
int board[row][col];
int i,j,posi,posj,count=1;
//populate board
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{board[i][j]=count;
if(dig1==count)
{
posi=i;
posj=j;
}
count++;
}
}
if((posi+1)<row && board[posi+1][posj]==dig2)//down
return 1;
else if((posi-1)>0 && board[posi-1][posj]==dig2)//up
return 1;
else if((posj+1)<col && board[posi][posj+1]==dig2)//right
return 1;
else if((posj-1)>0 && board[posi][posj-1]==dig2)//left
return 1;
else
return 0;
}
import java.io.*;
class keypad
{
public static void main(String args[])
{
String original = "1578";
String entered = "1678";
int errors = 0;
int valid = 1;
if(original.length()!=entered.length())
{
return;
}
for(int i=0;i<original.length();i++)
{
int x = original.charAt(i);
int y = entered.charAt(i);
if(x==y) continue;
if(errors ==0)
{
int diff = x-y;
System.out.println("diff: "+diff);
if(diff%3==0){errors=1;continue;}
for(int three = 3;three<=9;three=three+3)
{
if((x<=three)!=(y<=three)){ valid = 0;break;}
}
errors=1;continue;
}
else{ valid = 0; break; }
}
System.out.println(valid);
}
}
Very good code.. but you just missed one thing..
if you enter 2968 for 2568 it should give error.. but it doesn't..
update this and it will work..
int x = original.charAt(i)-48;
int y = entered.charAt(i)-48;
Since most solutions here are so long I will try to post a shorter one:
// assuming arrays are the same length
static boolean allow(int[] code, int[] input) {
int len = code.length;
boolean seenError = false;
for(int i=0; i<len; i++) {
if(input[i] == code[i])
continue;
if(!similar(input[i], code[i]) || seenError)
return false;
else seenError = true;
}
return true;
}
static boolean similar(int i, int j) {
i--;
j--;
int ci = (i%3);
int ri = (i/3);
int cj = (j%3);
int rj = (j/3);
return (Math.abs(ci-cj) <= 1 || Math.abs(ri-rj) <= 1);
}
Python working code.
"""
8:45
@Python 2.7
There is a security keypad at the entrance of a building. It has 9 numbers 1 - 9 in a 3x3 matrix format.
1 2 3
4 5 6
7 8 9
The security has decided to allow one digit error for a person but that digit should be horizontal or vertical.
Example: for 5 the user is allowed to enter 2, 4, 6, 8 or for 4 the user is allowed to enter 1, 5, 7. IF the security code to enter is 1478 and if the user enters 1178 he should be allowed.
Write a function to take security code from the user and print out if he should be allowed or not
- Anonymous on May 25, 2010 Report Duplicate | Flag
"""
class SecurityCode(object):
def __init__(self, number = 1478):
if number is None:
print 'Invalid Inputs'
raise SystemExit
self._number = str(number)
self.matrix = [[1,2,3], [4,5,6], [7,8,9]]
def typoCheck(self, pos = None, num = None):
if pos is None or num is None:
return
correct = int(self._number[pos])
num = int(num)
if ((correct - 1) / 3 == (num - 1) / 3) or (abs(num - correct) % 3 == 0):
return True
return False
def validate(self, input):
input = str(input)
wrongPos = []
if len(input) != len(self._number):
print 'Invalid'
return False
for i in range(len(input)):
if self._number[i] != input[i]:
wrongPos.append(i)
if len(wrongPos) > 1:
print 'Invalid'
return False
elif len(wrongPos) == 1:
if self.typoCheck(wrongPos[0], input[wrongPos[0]]):
print 'Valid'
return True
else:
print 'Invalid'
return False
else:
print 'valid!'
return True
if __name__ == '__main__':
s = SecurityCode()
s.validate(1178)
package com;
import java.util.LinkedList;
public class password {
public static void findNeighbours(int[][] pass, String num,int len){
LinkedList<Integer> li = new LinkedList<>();
li.add(Integer.parseInt(num));
for(int i=0;i<len;i++){
String back = num.substring(0,i);
String digit = num.substring(i,i+1);
String front = num.substring(i+1,len);
LinkedList<Integer> dd = findallowed(pass, Integer.parseInt(digit));
//li.add(Integer.parseInt(back+digit+front));
for(int j=0;j<dd.size();j++){
li.add(Integer.parseInt(back+dd.get(j)+front));
}
}
for(int i=0;i<li.size();i++){
System.out.println(li.get(i));
}
}
public static LinkedList<Integer> findallowed(int[][] pass, int digit){
LinkedList<Integer> ret = new LinkedList<>();
boolean f = false;
int k=0,h=0;
for(int i=0;i<pass.length;i++){
for(int j=0;j<pass[0].length;j++){
//System.out.println(pass[i][j]);
if(pass[i][j]==digit){
f=true;
k=i;h=j;
}
}
}
if (f==false)
return null;
else{
if(k+1<pass.length&& k+1>=0&&h<pass[0].length&& h>=0)
ret.add(pass[k+1][h]);
if(k-1<pass.length&& k-1>=0&&h<pass[0].length&& h>=0)
ret.add(pass[k-1][h]);
if(k<pass.length&& k>=0&&h+1<pass[0].length&& h+1>=0)
ret.add(pass[k][h+1]);
if(k<pass.length&& k>=0&&h-1<pass[0].length&& h-1>=0)
ret.add(pass[k][h-1]);
return ret;
}
}
public static void main(String[] args) {
int[][] pass ={{1,2,3},{4,5,6},{7,8,9}};
findNeighbours(pass,1178+"",4);
}
}
#!/usr/bin/python
def testPW(pw):
key = '1478'
if len(key) != len(pw):
return 'fail'
count = 0
for i in range(len(pw)):
if key[i] != pw[i]:
count += 1
diff = abs(int(key[i]) - int(pw[i]))
if (diff != 1) and (diff != 3):
return 'fail'
if count > 1:
return 'fail'
else:
return 'success'
print testPW('1178')
Here is a working code in C++:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int x, y, flag=0, ct=0, num, code, i, j;
int str[3][3];
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
str[i][j]=(3*i)+(j+1);
}
}
cout<<"Enter the trial code\n";
cin>>num;
cout<<"Enter the original security code\n";
cin>>code;
while (num>0)
{
x=num%10;
y=code%10;
num/=10;
code/=10;
if (x==y)
{
continue;
}
else
{
i=y/3;
j=y%3;
if (j==0)
{
i--;
j=3;
}
j--;
if ((i==0 && j==0) && (str[0][1]==x || str[1][0]==x))
{
ct++;
}
else if ((i==0 && j==1) && (str[0][2]==x || str[0][0]==x || str[1][1]==x))
{
ct++;
}
else if ((i==0 && j==2) && (str[0][1]==x || str[1][2]==x))
{
ct++;
}
else if ((i==1 && j==0) && (str[0][0]==x || str[2][0]==x || str[1][1]==x))
{
ct++;
}
else if ((i==1 && j==1) && (str[0][1]==x || str[1][0]==x || str[1][1]==x || str[2][1]==x))
{
ct++;
}
else if ((i==1 && j==2) && (str[0][2]==x || str[1][1]==x || str[2][0]==x))
{
ct++;
}
else if ((i==2 && j==0) && (str[1][0]==x || str[2][1]==x))
{
ct++;
}
else if ((i==2 && j==1) && (str[2][0]==x || str[2][2]==x || str[1][1]==x))
{
ct++;
}
else if ((i==2 && j==2) && (str[2][1]==x || str[1][2]==x))
{
ct++;
}
else
flag=1;
}
if (flag==1)
{
break;
}
}
if (flag==1 || ct>1)
{
cout<<"Not Allowed"<<endl;
}
else
cout<<"Allowed"<<endl;
return 0;
}
public class SecurityKeypad{
static int[][] array = {{1,2,3},{4,5,6},{7,8,9}};
public static void main(String args[]){
int key = 13787;
int password = 1178;
if(Integer.toString(key).length() != Integer.toString(password).length()){
System.out.println("Invalid Length");
System.exit(0);
}else{
int count = 0;
for(int i = 0; i< Integer.toString(key).length();i++){
if(Integer.parseInt(Character.toString(Integer.toString(key).charAt(i))) != Integer.parseInt(Character.toString(Integer.toString(password).charAt(i)))){
count += 1;
}
if(Math.abs(Integer.parseInt(Character.toString(Integer.toString(key).charAt(i))) - Integer.parseInt(Character.toString(Integer.toString(password).charAt(i)))) != 0 && Math.abs(Integer.parseInt(Character.toString(Integer.toString(key).charAt(i))) - Integer.parseInt(Character.toString(Integer.toString(password).charAt(i)))) != 1 && Math.abs(Integer.parseInt(Character.toString(Integer.toString(key).charAt(i))) - Integer.parseInt(Character.toString(Integer.toString(password).charAt(i)))) != 3){
System.out.println("Invalid password");
break;
}
}
if(count>1){
System.out.println("Invalid password");
System.exit(0);
}
}
}
}
- Anon October 02, 2014