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
On Thu, Mar 27, 2014 at 12:37 PM, Dan Bron <[email protected]> wrote:
> A question on StackExchange [1]:
>
> "
> I was asked the following interview question over the phone:
> Given an array of integers, produce an array whose values
> are the product of every other integer excluding the current index.
>
>
> Example:
> [4, 3, 2, 8] -> [3*2*8, 4*2*8, 4*3*8, 4*3*2] -> [48, 64, 96, 24]
>
> "
>
> His answer is given in the postscript. It spans 3 kilobytes over 86 lines.
> The community's responses aren't much of an improvement (by that metric;
> by other metrics - Java-specific ones, they decent advancements).
>
> The J answer?
>
> 1 */\. 4 3 2 8
> 48 64 96 24
>
> Could've answered the interviewer in a sentence, right over the phone.
>
> -Dan
>
>
> [1] Code Review StackExchange question
> "Array whose values are the product of every other integer":
>
>
> http://codereview.stackexchange.com/questions/45498/array-whose-values-are-the-product-of-every-other-integer
>
>
>
> import java.math.BigInteger;
> import java.util.Arrays;
>
> public class ProductOfAnArray {
>
> public static void main(String[] args) {
>
> try {
> System.out.println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4, 3, 2, 8 })));
> System.out.println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4, 0, 2, 8 })));
> System.out.println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4, 0, 2, 0 })));
> System.out.println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] {})));
> System.out
> .println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6,
> 7,
> 3, 2, 4 })));
> System.out
> .println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4, 3, 2, 8, 3, 2, 4, 6,
> 7,
> 3, 2, 4 })));
> System.out.println(Arrays.toString(ProductOfAnArray
> .calcArray(new int[] { 4432432, 23423423, 34234,
> 23423428,
> 4324243, 24232, 2342344, 64234234, 4324247,
> 4234233, 234422, 234244 })));
> } catch (Exception e) {
> // debug exception here and log.
> }
> }
>
> /*
> * Problem: Given an array of integers, produce an array whose values
> are
> * the product of every other integer excluding the current index.
> *
> * Assumptions. Input array cannot be modified. input is an integer
> array
> * "produce an array" - type not specified for output array
> *
> * Logic explanation:
> *
> * Assume we have array [a,b,c,d] Let multiple be multiple of each
> element
> * in array. Hence multiple = 0 if one of the element is 0; To produce
> the
> * output. Ans at i -> multiple divided by the value at i. if 2 numbers
> are
> * 0 then entire output will be 0 because atleast one 0 will be a
> multiple
> * if 1 number is 0 then every thing else will be 0 except that index
> whole
> * value is to be determined
> *
> */
> public static BigInteger[] calcArray(final int[] inp) throws Exception
> {
> if (inp == null)
> throw new Exception("input is null");
>
> int cnt = 0;
> BigInteger multiple = new BigInteger("1");
> boolean foundZero = false;
>
> for (int i : inp) {
> if (i == 0) {
> cnt++;
> foundZero = true;
> if (cnt < 2)
> continue;
> else
> break;
> }
> multiple = multiple.multiply(BigInteger.valueOf(i));
> }
>
> BigInteger ans[] = new BigInteger[inp.length];
>
> for (int i = 0; i < inp.length; i++) {
> if (foundZero) {
> if (cnt < 2) {
> ans[i] = (inp[i] == 0) ? multiple : new
> BigInteger("0");
> } else {
> ans[i] = new BigInteger("0");
> }
> } else {
> ans[i] = multiple.divide(BigInteger.valueOf(inp[i]));
> }
> }
> return ans;
> }
>
> }
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm