Question

In: Computer Science

please write simple python3 program and also explain the logic of program because i am not...

please write simple python3 program and also explain the logic of program because i am not understanding and crying so much

Problem Statement

There is a very famous old restaurant in ByteLand city,which can serve only 1 customer at any given time.It has B chairs outside for waiting customers.The restautants manager has a list of people who will be coming in today.The i-th customer will arrive at t[i] (all unique) time and will eat for d[i] unit of time.The restaurant attends to each customer as per the following rules:

1) if there is no customer being served currently,a new customer will be served immediately

2) if one customer is being served and there is at least one empty chair in the waiting area,a new customer will go and occupy one empty chair.

3)if the waiting area is full,a new customer will be rejected and will never be served .

4)Waiting area works like a queue and the first to enter will be served first.

Based on the list,the restaurant manager wants to know:

1.How many customers will be rejected today so that she can plan the food quantity accordingly and also 2 .v by what time will she finish serving the last customer.

Input format:

The first line of the input contains two integers N and B(1<=N,B<=100,000)-- The number of customers and the number of chairs in the waiting area.

Then follow N lines with queries descriptions (in chronological order ). Each description consists of two integers t[i] and d[i] ( 1 <= t[i] , d[i]<= 10^9 ) , where t[i] is the moment of time when the i-th customer appears and d[i] is the time customer needs for eating. it is gauranteed that t[i-1] < t[i] for all i >1

Output Format:

Print one integer corresponding to the number of customers who will be rejected and time when the last customer will be finish her meal.

Sample Input1:

5 1

2 9

4 8

10 9

15 2

19 1

Sample Output1

1 22

Explanation

1st customer comes at moment 2,no one is being served hence she will be served immediately.

2nd customer comes at moment 4 , 1rst one is still being served hence she will go and take chair in waiting area

3rd customer comes at moment 10 1rst one is still being served and no chair is empty in waiting area.Hence she will be rejected.

1st customer finishes at moment 11, 2nd customer will come from waiting area to get served and waiting area becomes empty.

4th customer comes at moment 15,2nd customer is being served hence she will move to waiting area.

2nd customer finishes at moment 19, 4th customer will move from waiting area to get served and waiting area becomes empty.

5th customer comes at moment 19,4th customer is being served hence she will move to waiting area

4th customer finishes at moment 21 , 5th customer will move from waiting area to get served and waiting area becomes empty.

5th customer finishes at moment 22

Sample input2

5 1

2 1

3 6

4 5

6 4

7 2

Sample putput 2:

2 14

Solutions

Expert Solution

ANSWER:

I have provided the properly commented  and indented code so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE

# fetching first input line n and seats
inp = input()
# input is form of string, fetching spaced number from string in integer format
# using string method split and using list comprehesion to fetch input in int
n, seats = [int(ele) for ele in inp.split()]

# looping n times to take n line inputs
# storing inputs in arrival and delay time lists
arrival = []
delay = []
for i in range(n):
    # similar to above input method, fetching inputs
    inp = input()
    # arr and del time
    arr, dly = [int(ele) for ele in inp.split()]
    # adding value to lists
    arrival.append(arr)
    delay.append(dly)
    
# Algorithm,
# arrival | delay | isEmpytSeat |In Rest Till | EmptySeats | Removed
#    2    |   9   |    True     |   11        |     1      | 0
#    4    |   8   |    True     |11 + 8 = 19  |     0      | 0
#    10   |   9   |    False    | Removed Imm.|     0      | 1
#    At 11 - EmptySeats ++ = 1
#    15   |   2   |    True     | 19+2 = 21   |      0     | 0
# At 19 - EmptySeats ++ = 1
#    19   |   1   |    True     | 21+1 = 22   |      0     | 0

# Hence, Removed = 1
# last customer in rest till => 22

# code
# defining and initializing variables
# inRestTill init to first customer arrival
inRestTill = arrival[0]
# list index counter
i = 0
# removed customer counter
removed = 0
# empty seats counter, initialy waiting seats + one serving seats empty
empty = seats

# looping for all elements in arrival and delay
while i < n:
    # ith customer delay addition to inRestTill
    inRestTill += delay[i]
    # looping to check, till inRestTill time how many customers coming
    # removing k-empty seats customers
    # loop counter, from i+1
    j = i+1
    while (j<n) and (arrival[j]<inRestTill):
        # customer j came to rest when serving i
        # if seats are fulled removing customer j from list of arrivals
        # and delays
        # using pop method of list to do so
        if empty == 0:
            # removing customer from list
            arrival.pop(j)
            delay.pop(j)
            # incrementing removed counter
            removed +=1
            # decrementing n(size) value by one
            n -= 1
            print(j)
        else:
            # if seats present decrementing seat
            empty -= 1
            # incrementing j counter
            j += 1
            
        
            
    # initializing seats to empty again, or checking same for next customer
    empty = seats
    # incrementing i counter
    i += 1

print()
print("Removed Customers:",removed)
print("Last Customer left Rest:",inRestTill)
    

OUTPUT IMAGE


Related Solutions

Whenever I am attempting to write a simple program on C++ I get an error message...
Whenever I am attempting to write a simple program on C++ I get an error message that reads "cout was not declared in this scope". Literally every time. This has become frustrating because I have even written my code the exact same way as some of my classmates who got theirs to compile and run with no sign of this error at all, and yet min gives this answer. I will leave an example of a code where this error...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
Please explain your answer thoroughly because I want to understand it well and also please include...
Please explain your answer thoroughly because I want to understand it well and also please include a diagram if possible Two objects which have mass: m1 = 10kg and m2 = 20kg. Both of them are moving at the velocity of: v1 = 20^i ms and v2 = 10^j ms and then the two objects collide completely inelastically. In what direction do the two objects go after the collision? After the collision, how much kinetic energy was lost?
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
This needs to be a python3 code Write a program that prompts the user like this:...
This needs to be a python3 code Write a program that prompts the user like this: “Currency to convert to U.S. dollars: e = Euros, c= Chinese Yuan, r = Indian Rupees, b = Bitcoin: ”. Then depending on which letter the user enters, the program displays “Amount of Euros/Yuan/Rupees/Bitcoin to convert: ”. (Note: the second prompt should only name the one currency the user asked to convert, not all four currencies.) After the user enters the amount, the program...
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE...
I am using a photoshop character that I drew. Please provide AS SIMPLE OF A PROCEDURE AS POSSIBLE!! 1. How do you create a sprite in Unity? 2. How do you create a sprite sliced in Unity? Thank you :)
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
USE C++ and please keep program as simple as possible and also comment so it is...
USE C++ and please keep program as simple as possible and also comment so it is easy to understad Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT