Question

In: Computer Science

You are in charge of security at a casino, and there is a thief who is...

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.

Solutions

Expert Solution

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 !!


Related Solutions

Anyone who bets money in a casino is classified as a winner if he or she...
Anyone who bets money in a casino is classified as a winner if he or she wins more than he or she loses. Casino operators in Atlantic City, New Jersey, believe that the proportion of all players who go home a winner is 0.46. Suppose 75 Atlantic City gamblers are selected at random. a. [3 marks] Find the sampling distribution of the proportion of gamblers who go home winners. b. [2 marks] Find the probability that the sample proportion is...
You are a car thief. You steal late-model cars in a major U.S. city and drive...
You are a car thief. You steal late-model cars in a major U.S. city and drive them across the border to sell them for an average price of $10,000. You are enrolled in Ms. Smith’s managerial accounting class and you want to know if crime, indeed, does pay. So you calculate your costs thus: ~ on average, you drive 2,000 miles from start to destination. Your gas cost over the past year has averaged $3.00 a gallon, and most of...
Imagine that you are an analyst who is in charge of evaluating a convertible bond issued...
Imagine that you are an analyst who is in charge of evaluating a convertible bond issued by Alpha Airlines since last year. Last year, the price stock was increasing dramatically and at some point reached an all-time high but in recent months, it plummeted due to the coronavirus shutting down traveling. What would you do differently in your analysis of the convertible bond position this year compared to last year? Explain why?
Write 500 words on the following prompt: You are a scientist who is in charge of...
Write 500 words on the following prompt: You are a scientist who is in charge of a team that is investigating a claim that a very simple form of life has been discovered on another planet. Explain your plan for what you want to learn to perhaps determine if this life form is related to life on Earth, or perhaps it is a unique form of life that evolved independently. You must use and discuss in DETAIL 5 major biochemical...
Suppose that you are the manager of a casino and you collect data on one of...
Suppose that you are the manager of a casino and you collect data on one of your roulette tables. You find that among the last 10,000 bets, there were only 350 bets for which a red or black number did not win. On average, you expect the proportion of winning bets for black or red to equal p=18/19. a) Compute a 95% confidence interval for the proportion of winning black or red bets. b) Conduct a two-sided hypothesis test (α=0.05)for...
If you were in charge of Information Security for a financial services company how would you design the backup strategy?
If you were in charge of Information Security for a financial services company how would you design the backup strategy? Include a diagram to document your backup strategy. Include recovery steps in your diagram.
Forest City Casino is interest in determining the percentage of their weekend visitors who spend more...
Forest City Casino is interest in determining the percentage of their weekend visitors who spend more than $1,000 at their casino. Based on the sample below, establish a 95% confidence interval for the percentage of weekend customers who spend more than $1,000. Forest City Casino Weekend Customer Spend Survey $175 $775 $325 $100 $250 $300 $675 $750 $900 $725 $750 $400 $250 $250 $300 $675 $600 $750 $675 $575 $200 $925 $425 $675 $975 $550 $450 $800 $6,775 $400 $600...
Question 3 [10 marks] Anyone who bets money in a casino is classified as a winner...
Question 3 [10 marks] Anyone who bets money in a casino is classified as a winner if he or she wins more than he or she loses. Casino operators in Atlantic City, New Jersey, believe that the proportion of all players who go home a winner is 0.46. Suppose 75 Atlantic City gamblers are selected at random. a. [3 marks] Find the sampling distribution of the proportion of gamblers who go home winners. b. [2 marks] Find the probability that...
Who is in charge of the USA national debt?
Who is in charge of the USA national debt?
It is a database security assignment - Your team is in charge of writing a formal...
It is a database security assignment - Your team is in charge of writing a formal company policy to keep the company database assets secure.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT