We can fix this failure by "thinking like a J programmer" :

   ((*/ .^ * %~)~:&0) 2 0 8 3
24 0 6 16

If our input is guaranteed non-negative we could shorten ~:&0 to * .

-Dan

> On Mar 27, 2014, at 5:06 PM, Roger Hui <[email protected]> wrote:
> 
> It fails when x has a 0 but is not all 0 :
> 
>   (*/ % ]) 2 0 8 3
> 0 0 0 0
> 
> 
> 
> 
>> On Thu, Mar 27, 2014 at 1:10 PM, Dan Bron <[email protected]> wrote:
>> 
>> Oh, I like this game!
>> 
>>           (*/ % ]) 4 3 2 8
>>        48 64 96 24
>> 
>>           (*/@#~1-=@i.) 4 3 2 8
>>        48 64 96 24
>> 
>> -Dan
>> 
>> ----- Original Message ---------------
>> 
>> Subject: Re: [Jprogramming] J as an interviewing tool
>>   From: Roger Hui <[email protected]>
>>   Date: Thu, 27 Mar 2014 12:44:59 -0700
>>     To: Programming forum <[email protected]>
>> 
>> Suppose you did not have the outfix operator, but still have array ops.
>> 
>>   x=: 4 3 2 8
>>   I=: =i.#x
>>   I
>> 1 0 0 0
>> 0 1 0 0
>> 0 0 1 0
>> 0 0 0 1
>>   (-.I)*/ .(^~) x
>> 48 64 96 24
>> 
>> 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to