Thank you, Raul for the nice code.

My quick reation to any code including control words(especially for
and while) in J has been "oh my goodness, I am not using J mind." I
still consider myself as a J novice with this puristic idealism.

Following is my version for (a) and (b):

bas=:|. <"_1 arv ,. srv
a=: 13 : 'y,~({:x) + ({.x) >. {. y'
2}.|.>a each/ bas , <0 0

b=: 13 : 'y,~({:x) + ({.x) >. <./ 2 {. y'
2}.|.>b each/ bas , <0 0

I haven't tried (c) with this same structure yet.

2006/8/26, Miller, Raul D <[EMAIL PROTECTED]>:
June Kim wrote:
> arv=: 12 31  63  95  99 154 198 221 304 346 411 455 537
> srv=: 40 32  55  48  18  50  47  18  28  54  40  72  12
>  (a) Determine the departure times of these 13 customers

  (]-"1 0:<.{.-_1 |.!.0{:)^:_ arv([,:+)srv

> (b) Repeat (a) when there are two servers and a customer can
> be served by either one

It seems to me that the desired result here has only a loose
functional relationship to the underlying model.  Rather
than fight that, I'd just go with an explicit computation:

b=:3 :0
 r=.''
 sfree=. 0 0
 for_j.i.#arv do.
 server=. (i.<./)sfree
 r=.r,depart=. (j{srv) + (server{sfree)>.j{arv
 sfree=. depart server} sfree
 end.r
)
  b''

I'm sure Jose would solve this one tacitly, but I'm not
at all sure that a tacit solution would be any more obvious.

> (c) Repeat (a) under the new assumption that when the server
> completes a service, the next customer to enter services
> is the one who has been waiting the least time

This seems to be the same result as b.

However, for the general case (assuming arrival times
are arranged in increasing order):

c=:3 :0
 sfree=. 0 0
 r=. 0*arv
 while.0 e. r do.
 server=. (i.<./)sfree
 time=. sfree >.&(<./) (0=r)#arv
 custom=. 1 i:~ (0=r) *. time>:arv
 depart=. (custom{srv) + (server{sfree)>.custom{arv
 r=.depart custom} r
 sfree=. depart server} sfree
 end.r
)
  c''

As an aside, I've made no attempt whatsoever to optimize
any of this code.  Not only is premature optimization the
root of all evil in programming, but the size of the data
set here is comparable to the size of a variable header.
In other words, the cost of a calculation on all data, here,
is roughly the same as the cost of using a variable.

--
Raul

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to