ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Programming Challenge

August 10, 2008 6:48am

Subscribe [3]
  • #1 / Aug 10, 2008 6:48am

    Michael Wales

    2070 posts

    xwero posted a programming challenge not to long ago that was a bit of fun for everyone involved - I thought I would post another one here. I’ll post my solution tomorrow.

    Given a list (A) with n elements, generate a list (B) with n elements such that the ith element of B is equal to the product of all elements in A except for the ith element.

    Test sets:

    A = [1, 2, 3]
    f(A) = [6, 3, 2]

    A = [4, 5, 6]
    f(A) = [30, 24, 20]

    A = [32, 0, -6]
    f(A) = [0, -192, 0]

  • #2 / Aug 10, 2008 7:04am

    Seppo

    483 posts

    Hey Michael, shouldn’t the first f(A) be [8, 4, 2] ? (or the A array [1, 2, 3]).

  • #3 / Aug 10, 2008 8:02am

    Michael Wales

    2070 posts

    ah yeah - my bad, fat-fingered it. Original post change - correct array is [1, 2, 3]

  • #4 / Aug 10, 2008 8:58am

    m4rw3r

    647 posts

    $a = array(32, 0, -6);
    
    foreach($a as $k => $v)
    {
        $b = $a;
        unset($b[$k]);
        $f[] = array_product($b);
    }
    
    print_r($f);
  • #5 / Aug 10, 2008 9:29am

    Alex.

    29 posts

    $originals = array(1, 2, 3);
    $products = array();
    
    foreach($originals as $key => $val)
    {
     $test = $originals;
     unset($test[$key]);
     $products[$key] = array_product($test);
    }

    EDIT: AAAH You beat me to it m4rw3r :p

  • #6 / Aug 10, 2008 10:18am

    m4rw3r

    647 posts

    This is a bit shorter

    $a = array(4, 5, 6);
    
    $b = array();
    while($b[] = array_shift($a))
    {
        $f[] = array_product(array_merge($a, array_slice($b, 0, -1)));
    }
    
    print_r($f);
  • #7 / Aug 11, 2008 9:59pm

    Sally D

    129 posts

    Given a list (A) with n elements, generate a list (B) with n elements such that the ith element of B is equal to the product of all elements in A except for the ith element.

    for some reason the above is not clicking I read it a few times. What does ith mean I don’t understand the ith element. So where is the solution it’s been a day, and why is ith italicized?

    given an array of array(4,5,6)
    print_r would return Array ( [0] => 30 [1] => 24 [2] => 20 )

    I’m not seeing anything please explain I am seeing something there 5*6=30, 6*4=24 5*4=20. I don’t get it.

    Are you saying to times every number against every other number besides the first number against itself together and then store their products into an array?

    i feel so dumbfounded by your challenge don’t leave me this way no matter how I tried I could not make sense of it?

  • #8 / Aug 11, 2008 10:31pm

    Pascal Kriete

    2589 posts

    Raymond, they’re not arrays.  You have to start at 1, not zero.

    You go through the list from 1 to i, and take away element i for every iteration - then multiply all remaining values in the list.

    So for:
    A = [1, 2, 3]

    Step 1 (i=1):
    f(A, 1) = prod[2, 3]

    2*3 = 6

    Step 2(i=2):
    f(A, 2) = prod[1, 3]

    1*3 = 3

    Step 3(i=3):
    f(A, 3) = prod[1, 2]

    1*2 = 2

    So then you get f(A) = [6, 3, 2]

    Make more sense now?

  • #9 / Aug 12, 2008 5:14am

    Michael Wales

    2070 posts

    m4rw3r had the best solution, in my opinion (plus, it’s very similar to my solution):

    $a = array(32, 0, -6);
    
    foreach($a as $k => $v)
    {
        $b = $a;
        unset($b[$k]);
        $f[] = array_product($b);
    }
    
    print_r($f);

    Runs very quickly and the code is simple to understand, you know exactly what it does just by reading it.

  • #10 / Aug 12, 2008 5:20am

    wiredesignz

    2882 posts

    ...you know exactly what it does just by reading it.

    :lol: Michael


    m4rw3r ‘s 2nd solution is far superior.

    $a = array(4, 5, 6);
    
    while($b[] = array_shift($a))
    {
        $f[] = array_product(array_merge($a, array_slice($b, 0, -1)));
    }
  • #11 / Aug 12, 2008 5:23am

    m4rw3r

    647 posts

    I suggested the one that Michael posted here.

    And the other one was just to see if i could get it any shorter 😛

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases