-
bakerytles [TS]
on
09 Aug 14 @ 11:54 PM #
Hindi ko alam kung tama yung ginawa ko. Gagamit ako ng abstract data type (list, set, stack, queue, map, etc) para i-present yung data?
Bale ang gusto ko i-present dito is parang bounded queueing. May 10 food lang pero 15 yung workers.
def lunch(food, money, worker):
if money > 50:
print \"Here\'s your food!\"
food = food - 1
worker = worker -1
elif food < 0:
print \"Sorry!\"
return food
return worker
money = 55
food = 10
worker = 15
food = lunch(food, money, worker)
print \"Food / Worker:\", food
food = lunch(food, money, worker)
print \"Food / Worker:\", food
food = lunch(food, money, worker)
print \"Food / Worker:\", food
food = lunch(food, money, worker)
print \"Food / Worker:\", food
-
jcmpix
on
10 Aug 14 @ 11:55 AM #
Python ba \'to? Is it legal to have two return statements?
-
sevenstring
on
10 Aug 14 @ 01:44 PM #
it looks like gusto mong ireturn yung food at worker. easiest way is to combine it into a tuple and send it back.
<click here for link>
you can also use a list, dictionary or emulate a C struct using class.
edit: use www.codepad.org in the future if you want to share source code. para makita yung indentation
-- edited by sevenstring on Aug 10 2014, 02:12 PM
-
bakerytles [TS]
on
10 Aug 14 @ 06:37 PM #
@jcmpix
Yes sir. Hindi yata pwede yung 2 return statements.
@sevenstring
Salamat sir! Ganun pala gagawin kapag gusto 2 or more ang return statements.
Also wala pa akong alam na language, gusto ko lang ipakita yung idea na \'canteen scenario\' using bounded queue(limited slots only?)
tama po ba? medyo naguguluhan ako
-
bakerytles [TS]
on
10 Aug 14 @ 09:11 PM #
up
-
sevenstring
on
11 Aug 14 @ 12:20 AM #
With the result in mind, maybe you could help us by posting the required output. Then from there, maraming makakapagbigay ng inputs regarding your problem.
It seems to me that you just need to further elaborate/extend your logic statements, but maybe di rin clear yung understanding ko sa problem na ito.
-
ram2010
on
11 Aug 14 @ 05:10 AM #
bakerytles Send Message View User Items on August 09, 2014 11:54 PM
Hindi ko alam kung tama yung ginawa ko. Gagamit ako ng abstract data type (list, set, stack, queue, map, etc) para i-present yung data?
Bale ang gusto ko i-present dito is parang bounded queueing. May 10 food lang pero 15 yung workers.
This is python, a function can return 1 or more values. Try this.
LIMIT = 4
def lunch(food, money, worker):
if money > 50:
print \"Here\'s your food!\"
food = food - 1
worker = worker -1
elif food < 0:
print \"Sorry!\"
return (food, worker)
money = 55
food = 10
worker = 15
cnt = LIMIT
while cnt:
food, worker = lunch(food, money, worker)
print \"Food / Worker:\", food, \"/\", worker
cnt -= 1
Here is the output.
Here\'s your food!
Food / Worker: 9 / 14
Here\'s your food!
Food / Worker: 8 / 13
Here\'s your food!
Food / Worker: 7 / 12
Here\'s your food!
Food / Worker: 6 / 11
-
ram2010
on
11 Aug 14 @ 05:53 AM #
This one uses a class. See the code and output here.
<click here for link>
-
bakerytles [TS]
on
11 Aug 14 @ 06:28 AM #
@sevenstring
With the result in mind, maybe you could help us by posting the required output. Then from there, maraming makakapagbigay ng inputs regarding your problem.
It seems to me that you just need to further elaborate/extend your logic statements, but maybe di rin clear yung understanding ko sa problem na ito.
Bale ang gusto ko palabasin is, pag yung food=0 parang mag sstop yung function. Parang sa canteen, pipila yung mga worker pero 15 lang yung food. Gusto ko lang ipresent na queueing yung data. But then parang vague yata?
@ram2010
Thanks sa input sir!
-
sevenstring
on
11 Aug 14 @ 01:58 PM #
Bale ang gusto ko palabasin is, pag yung food=0 parang mag sstop yung function. Parang sa canteen, pipila yung mga worker pero 15 lang yung food. Gusto ko lang ipresent na queueing yung data. But then parang vague yata?
i see. ok, I hope this is what you mean
<click here for link>
what I did is I placed 11 workers. Once the food is exhausted (food=0), food is not served to the next worker. Then I implemented a reload food function to replenish the food supply. Once replenished, food is served to the person next in line.
-
ram2010
on
11 Aug 14 @ 04:47 PM #
Here is another alternative solution to your abstract problem :)
This is code only.
<click here for link>
This is more dynamic, user is allowed to enter dynamically the food, money, workers and the limit of money to process lunch.
Sample output 1.
input money: 55
input food: 10
input worker: 16
minimum money to process lunch: 40
Warning food supply is lower than workers!!
Here\'s your food: worker 1
Status: Food=9, Worker=15, Money=54
Here\'s your food: worker 2
Status: Food=8, Worker=14, Money=53
Here\'s your food: worker 3
Status: Food=7, Worker=13, Money=52
Here\'s your food: worker 4
Status: Food=6, Worker=12, Money=51
Here\'s your food: worker 5
Status: Food=5, Worker=11, Money=50
Here\'s your food: worker 6
Status: Food=4, Worker=10, Money=49
Here\'s your food: worker 7
Status: Food=3, Worker=9, Money=48
Here\'s your food: worker 8
Status: Food=2, Worker=8, Money=47
Here\'s your food: worker 9
Status: Food=1, Worker=7, Money=46
Here\'s your food: worker 10
Status: Food=0, Worker=6, Money=45
Sorry food supply is now 0!!
Sample output 2.
input money: 50
input food: 20
input worker: 10
minimum money to process lunch: 60
Status: Food=20, Worker=10, Money=50
Sorry we stop serving, money is below 60!!
Sample output 3.
input money: 100
input food: 6
input worker: 5
minimum money to process lunch: 50
Here\'s your food: worker 1
Status: Food=5, Worker=4, Money=99
Here\'s your food: worker 2
Status: Food=4, Worker=3, Money=98
Here\'s your food: worker 3
Status: Food=3, Worker=2, Money=97
Here\'s your food: worker 4
Status: Food=2, Worker=1, Money=96
Here\'s your food: worker 5
Status: Food=1, Worker=0, Money=95
Stop serving, worker(s) already got their lunche(s)!!
-- edited by ram2010 on Aug 12 2014, 04:47 PM
-
bakerytles [TS]
on
11 Aug 14 @ 10:26 PM #
@sevenstring & ram2010
Grabe sir ang galing niyo. Salamat mga master!