Question

1 Problems (PY 1. Write a program to simulate a very busy TA in office hours, according to the following specifications. The
(i) When a student comes in when a spot at the table is open and the TA is busy, the student takes a seat and waits for the T
have other files if you wish. You should match the output format of ofhmatch.txt. If necessary, include a README file to prov
Average wait of resource line: # secs Number of students remaining in resource line: # Average wait of table: # secs Number o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''

Ensure correct tab spacing before you run the program

'''

import time

from threading import Thread, Lock

class Clock:

    #currentTime = 0;

    def run(self):

                    self.currentTime = 0;

                    while(True):

                                    time.sleep(1)

                                    self.currentTime += 1

    def now(self):

                    return self.currentTime

clock = Clock()

class Assistant:

    def __init__(self, pStart, pEnd):

                    self.StartTime = pStart

                    self.EndTime = pEnd

                    self.Speed = 5

                    self.Table = [ None, None, None ]

                    self.Arrivals = []

                    self.CurrentlyHelping = None

                    self.myIdleTime = 0

                   

                    self.mutex = Lock()

    def run(self):

                    self.helpStudent()

                    

    def helpStudent(self):   

                    while(clock.now() < self.EndTime):

                                    if (len(self.Arrivals) ==0 ):

                                                    time.sleep(1)

                                                    self.myIdleTime += 1

                                    else:

                                                    earliest = self.Arrivals.pop(0)

                                                    self.CurrentlyHelping = self.Table[earliest]

                                                    while(self.CurrentlyHelping.getTimeRequired() > 0):

                                                                    time.sleep(1)

                                                   

                                                    self.CurrentlyHelping = None

                                   

                                    

    def getTable(self):

                    return self.Table

                   

    def getEmptySlot(self):

                    try:

                                    slot = self.Table.index(None)

                    except (ValueError):

                                    slot = None

                    return slot

    def getCurrentlyHelping(self):

                    return self.CurrentlyHelping

                   

    def fillSlot(self, pSlot, pStudent):

                    self.mutex.acquire()

                    if (self.Table[pSlot] is None):

                                    self.Table[pSlot] = pStudent

                                    self.Arrivals.append(pSlot)

                                    self.mutex.release()

                                    return True

                    else:

                                    self.mutex.release()

                                    return False

    def getIdleTime(self):

                    return myIdleTime

    def getArrivals(self):

                    return self.Arrivals

                               

#End of TA

resourceCentre = []

waitingResources = 0

totalWaitTime = 0

tableWaitTime = 0

waitingAtTable = 0

class Student:

    def __init__(self, pID, pTimeRequired, pSeekHelpAt,pTA):

                    self.ID = pID

                    self.TimeReq = pTimeRequired

                    self.ApproachTA = pSeekHelpAt

                    self.TA = pTA

                    self.myArrivalTime = None

                    self.mySlot = None

                    self.myWaitTime = 0

                    self.waiting = False

                    

    def run(self):

                    while(clock.now() < self.ApproachTA):

                                    time.sleep(1)

                    self.seekHelp()

    def waitInResourceQueue(self):

                    global waitingResources

                    self.waiting = True;

                    waitingResources += 1

                    while(resourceCentre.index(self) > 0):

                                    time.sleep(1)

                                    self.myWaitTime += 1                

    def seekHelp(self):

                    if (self.TA.getEmptySlot() is None):

                                    resourceCentre.append(self)

                                    self.waitInResourceQueue()

                                   

                    self.mySlot = self.TA.getEmptySlot()

                    while(self.mySlot is None or not self.occupySlot()):

                                    time.sleep(1)

                                    self.mySlot = self.TA.getEmptySlot()

                    

                    if(self.TA.getCurrentlyHelping() != self):

                                    self.solveOnYourOwn()

                   

                    while(self.TimeReq > 0):

                                    time.sleep(1)

                                   self.TimeReq -= 1

                    self.leaveSlot()

  

                    

    def solveOnYourOwn(self):

                    global waitingAtTable

                    global tableWaitTime

                    val = 1

                    waitingAtTable += 1

                    while (self != self.TA.getCurrentlyHelping()):

                                    time.sleep(1)

                                    tableWaitTime += 1

                                    if (val == 0):

                                                    self.TimeReq -= 1

                                                    if (self.TimeReq == 0):

                                                                    return

                                    val = (val + 1)%3

    def occupySlot(self):

                    global totalWaitTime

                    if (not self.TA.fillSlot(self.mySlot, self)):

                                    self.mySlot = None

                                    return False

                    else:

                                    if (self.waiting):

                                                    resourceCentre.pop(0)

                                                    totalWaitTime += self.myWaitTime

                                    self.myArrivalTime = clock.now()

                                    return True

    def getArrivalTime(self):

                    return self.myArrivalTime

                   

    def getTimeRequired(self):

                    return self.TimeReq

                   

    def leaveSlot(self):

                    tbl = self.TA.getTable()

                    tbl[self.mySlot] = None

                   

    def getWaitTime(self):

                    return self.myWaitTime

                               

#End of Student

def main():

    tClock = Thread(name="clock", target=clock.run)

    tClock.start()

    print("Please wait 10 seconds for the output")

    TA = Assistant(0, 10)

    tTA = Thread(name="ta", target=TA.run)

    tTA.start()

    myClass = []

    for i in range(0,25):

                    #pID, pTimeRequired, pSeekHelpAt,pTA

                    obj = Student(i, 4+i%5, 3+i+i, TA)

                    student = Thread(target = obj.run)

                    student.start();

                    myClass.append(student);

    tTA.join()

    #for(student in myClass):

    #             student.join()

    global totalWaitTime, waitingResources, resourceCentre, waitingAtTable, tableWaitTime, resourceCentre

    print("Average wait of resource line: ", totalWaitTime/waitingResources)

    print("Total students remaining in resource Line: ", len(resourceCentre))

    print("Average wait of tables: ", tableWaitTime/waitingAtTable)

    print("Number of students TA was not able to help: ", len(resourceCentre) + len(TA.getArrivals()))

main()

Add a comment
Know the answer?
Add Answer to:
1 Problems (PY 1. Write a program to simulate a very busy TA in office hours,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

  • Art 250 Choose the best answer of both hands at the same time is A. flow process chart simultaneo...

    art 250 Choose the best answer of both hands at the same time is A. flow process chart simultaneous-motion (SIMO) chart 2 The best chart used to analyze the Body is called B. Operations charts D. None of the above 3. In a stopwatch time study, the average time it takes a given worker to perform a task a certain number of times is the: A. Observed time B. Normal time C. Standard time D. Performance rating time Labor Standards...

  • write up an essay on the problems in budgeting derived from the articles (i do Upvote...

    write up an essay on the problems in budgeting derived from the articles (i do Upvote the answers ) Why Budgeting Kills Your Company HBSWK Pub. Date: Aug '1 1, 2003 Why doesn't the budget process work? Read what experts say about not only changing your budgeting process, but whether your company should dispense with budgets entirely. by Loren Gary The average billion-dollar company spends as many as 25,000 person-days per year putting together the budget. If this all paid...

  • Needs Help with Java programming language For this assignment, you need to write a simulation program...

    Needs Help with Java programming language For this assignment, you need to write a simulation program to determine the average waiting time at a grocery store checkout while varying the number of customers and the number of checkout lanes. Classes needed: SortedLinked List: Implement a generic sorted singly-linked list which contains all of the elements included in the unsorted linked list developed in class, but modifies it in the following way: • delete the addfirst, addlast, and add(index) methods and...

  • Read the case and Summarizes the situation as you see it. Describes what Organizational Development is and its core valu...

    Read the case and Summarizes the situation as you see it. Describes what Organizational Development is and its core values Good morning. Northern County Legal Services," Christina said. "How can I help you? Yes, I see. Okay, why don't I schedule a time for you to stop by and talk with one of us about your situation and we can see how we can help? I'm free on the 12th at 3:30 p.m. Does that work for you? Excellent. And...

  • Research Paper Help - Urgent please

    ***Please help with this Research Paper. Following points are to be covered in the research paper with as much detail as possible:1. Case Overview - Concise summary of the main issues of the case. Please ensure all issues are identified.2. Analysis - External and internal analysis of the case issues. Application of evidence based development of course concepts, research and applicable legislation.3. Implementation & Recommendations - Detailed application of appropriate legislation to form recommendations. ***Case Study: "Found: An Unsigned Card"Tara...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • Question 1: Wendy's Happy Homes Inc manufactures Home Appliances. Monthly sales of Wendy's Washer...

    Question 1: Wendy's Happy Homes Inc manufactures Home Appliances. Monthly sales of Wendy's Washers and Dryer Sets for a nine month period were as follows: MONTH Washer and Dryer Sales 490 480 450 500 480 470 490 520 530 January February March April May June July August September Forecast October sales using 1) A four-month moving average 2) a six-month moving average 3. Compute the MAD for each forecast method you used Actual October sales were 320 nits. 4) Which...

  • Only need help with question 5 2 CASE The Human Resource Function of Harrison Brothers Corporation...

    Only need help with question 5 2 CASE The Human Resource Function of Harrison Brothers Corporation COMPANY HISTORY Harrison Brothers Corporation n was founded in upstate New York on September 15, 1898, by Aubrey and William Harrison. Harrison Brothers is a multi-line traditional department store tha t cares mainly men's, wome expanded to include household furnishings and other items for the home. The long-term goal of the company is to become the leading chain of department stores in the Northeast,...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT