[Tutor] I'm attempting to code the barbershop problem...

Steven D'Aprano steve at pearwood.info
Wed Apr 25 08:14:18 EDT 2018


Hi Michael and welcome.

In future, please leave the Subject line as a BRIEF summary, and put the 
description of your problem in the body of the email.

You said:

> I'm attempting to code the barbershop problem in OS except
> with 3 barbers instead of one. Can anyone help rewrite my Barber1 and
> Barber2 classes so it just uses the functions already defined in the
> original Barber class.

What's the barbershop problem?

Why do you need three classes?


On Wed, Apr 25, 2018 at 11:29:23AM +0100, Michael Solan wrote:
> class Barber: barberWorkingEvent = Event() def sleep(self):
> self.barberWorkingEvent.wait() def wakeUp(self):
> self.barberWorkingEvent.set() def cutHair(self, customer): #Set barber as
> busy self.barberWorkingEvent.clear() print '{0} is having a haircut from
> barber\n'.format(customer.name) HairCuttingTime = random.randrange(0, 5)
> time.sleep(HairCuttingTime) print '{0} is done\n'.format(customer.name)

The formatting here is completely messed up. If you are posting using 
Gmail, you need to ensure that your email uses no formatting (no bold, 
no colours, no automatic indentation etc), or else Gmail will mangle the 
indentation of your code, as it appears to have done above.

My wild guess is that what you probably want is something like this:

import random
import time

class Barber(object):
    def __init__(self, name):
        self.workingEvent = Event()  # What is Event?
        self.name = name
    def sleep(self):
        self.workingEvent.wait()
    def wakeUp(self):
        self.workingEvent.set()
    def cutHair(self, customer):
        # Set this barber as busy.
        self.workingEvent.clear()
        template = '{0} is having a haircut from barber {1}\n'
        print template.format(customer.name, self.name)
        HairCuttingTime = random.randrange(0, 5)
        time.sleep(HairCuttingTime)
        print '{0} is done\n'.format(customer.name)


tony = Barber('Tony')
fred = Barber('Fred')
george = Barber('George')

# and then what?



Notice that we have *one* class and three instances of that class. I've 
given them individual names so they're easier to distinguish.


Please ensure you reply on the mailing list.


-- 
Steve


More information about the Tutor mailing list