0

On a Call - Please send help I'm going insane.

So I can't grasp why the hell this attempt doesn't work. Base code (i filled the list so we don't need the add input) class CallCenter: def __init__(self): self.customers = ['general','general','technical','general'] def is_empty(self): return self.customers == [] def add(self, x): self.customers.insert(0, x) def next(self): return self.customers.pop() c = CallCenter() so the solution attempt is: time = 0 while not c.is_empty(): item = c.next() if item == 'general': time += 5 if item == 'technical': time += 10 print(time) so far so good, now my attempt before was: time = 0 while not c.is_empty(): if c.next() == 'general': time += 5 if c.next() == 'technical': time += 10 print (time) And i noticed that without the reassignment of the method c.next() to any kind of variable my attempt doesn't work. Any explanation?

5th Apr 2023, 9:29 AM
Genji Shimada
Genji Shimada - avatar
7 Answers
+ 4
In second, before attempt : c.next() is called 2 times. First c.next() will return 'general' and passes 1st if condition, Again in 2nd if condition will get 'general' but does not match with 'technical' so it causes adding time value. You need a single call but previous call is calling 2 times in single cycle. But correct solution, call next() only once in loop cycles...
5th Apr 2023, 9:42 AM
Jayakrishna 🇼🇳
+ 3
Bingo Genji. Each loop of yours calls the input twice.
5th Apr 2023, 10:57 AM
Ausgrindtube
Ausgrindtube - avatar
+ 2
I guess i understood what you said. so in my attempt i call the next() method 2 times where in the solution it is just called 1 time and the returned result from the method is compared in the if statements. Is that it?
5th Apr 2023, 10:02 AM
Genji Shimada
Genji Shimada - avatar
+ 2
Genji Shimada Yes. And your returned value should be checked either is it 'general' Or 'technical'..? But your wrong code checks only one, calls next() without going to check for other value. See each next() call will pop an item from list and returns that value..
5th Apr 2023, 2:58 PM
Jayakrishna 🇼🇳
+ 1
Check it by adding a print statement before you time += parts. I think you'll see it.
5th Apr 2023, 9:36 AM
Ausgrindtube
Ausgrindtube - avatar
0
I did that and noticed that in my attempt the last addition is missing like it skips the last queue item and idk why
5th Apr 2023, 9:38 AM
Genji Shimada
Genji Shimada - avatar
0
For anyone who wants to try it with the full code I'm using to find the flaw: class CallCenter: def __init__(self): self.customers = ['general','general','technical','general'] def is_empty(self): return self.customers == [] def add(self, x): self.customers.insert(0, x) def next(self): return self.customers.pop() c = CallCenter() time = 0 while not c.is_empty(): if c.next() == 'general': print(time, "before .pop") time += 5 print(time, "after .pop") if c.next() == 'technical': print(time,"before .pop") time += 10 print(time, "after .pop") print (time) print("\nHere comes the new round \n------------------------\n") c.add('general') c.add('technical') c.add('general') c.add('general') time = 0 while not c.is_empty(): item = c.next() if item == 'general': print(time, "before .pop") time += 5 print(time, "after .pop") if item == 'technical': print(time, "before .pop") time += 10 print(time, "after .pop") print(time)
5th Apr 2023, 9:41 AM
Genji Shimada
Genji Shimada - avatar