In: Computer Science
You are in charge of security at a casino, and there is a thief
who is trying to steal the casino's money! Look over the security
diagrams to make sure that you always have a guard between the
thief and the money!
There is one money location, one thief, and any number of guards on
each floor of the casino.
Task in Java, Python and CPP:
Evaluate a given floor of the casino to determine if there is a
guard between the money and the thief, if there is not, you will
sound an alarm.
Input Format:
A string of characters that includes $ (money), T (thief), and G
(guard), that represents the layout of the casino floor.
Space on the casino floor that is not occupied by either money, the
thief, or a guard is represented by the character x.
Output Format:
A string that says 'ALARM' if the money is in danger or 'quiet' if
the money is safe.
Java-
import java.util.Scanner;;
public class Casino {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter layout of casino");
String casinoLayout = sc.nextLine();
// Find position of money and thief. Set guardPresent=false initially
int moneyPosition = casinoLayout.indexOf('$');
int thiefPosition = casinoLayout.indexOf('T');
boolean guardPresent = false;
// Search from thief position to money position for guard
if(thiefPosition > moneyPosition){
for(int currentPosition = thiefPosition; currentPosition > moneyPosition; currentPosition--){
if(casinoLayout.charAt(currentPosition) == 'G'){
guardPresent = true;
break;
}
}
}
else{
for(int currentPosition = thiefPosition; currentPosition < moneyPosition; currentPosition++){
if(casinoLayout.charAt(currentPosition) == 'G'){
guardPresent = true;
break;
}
}
}
// Print appropriate message.
if(guardPresent){
System.out.println("Quiet");
}
else{
System.out.println("ALARM");
}
}
}
Python Code -
casinoLayout = input("Enter layout of casino : ")
# Find positions of money and thief.
money_position = casinoLayout.index("$")
thief_position = casinoLayout.index("T")
guard_position = -1
# Try finding position of guard in between thief and money, if guard not found catch ValueError
# Thrown by index() function.
if(money_position > guard_position):
try:
guard_position = casinoLayout.index("G", guard_position, thief_position)
except ValueError:
pass
else:
try:
guard_position = casinoLayout.index("G", thief_position, guard_position)
except ValueError:
pass
if(guard_position == -1):
print("ALARM")
else:
print("Quiet")
Output-
CPP code -
#include<iostream>
using namespace std;
int main()
{
string casinoLayout;
cout<<"Enter layout of casino : ";
cin>>casinoLayout;
int moneyPosition;
int thiefPosition;
for(int i=0;i<casinoLayout.size();i++){
if(casinoLayout[i] == 'T'){
thiefPosition = i;
break;
}
}
for(int i=0;i<casinoLayout.size();i++){
if(casinoLayout[i] == '$'){
moneyPosition = i;
break;
}
}
bool guardPresent = false;
if(moneyPosition > thiefPosition){
for(int i=thiefPosition; i<moneyPosition; i++){
if(casinoLayout[i] == 'G'){
guardPresent = true;
break;
}
}
}
else{
for(int i=thiefPosition; i>moneyPosition; i--){
if(casinoLayout[i] == 'G'){
guardPresent = true;
break;
}
}
}
if(guardPresent){
cout<<"Quiet"<<endl;
}
else{
cout<<"ALARM"<<endl;
}
return 0;
}
Output-
I have added comments for your understanding
I would love to resolve any queries in the comments. Please consider dropping an upvote to help out a struggling college kid :)
Happy Coding !!