How to calculate the missing number from an array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ton 't Lam

    How to calculate the missing number from an array?

    Suppose I have an array
    my @arr = ( 1,4);

    I need to have the missing numbers:
    my @res = ( 2,3,5);

    How to calculate this easy from @arr ?

    Best Regards,
    Ton
  • bdj

    #2
    Re: How to calculate the missing number from an array?

    Hi Ton!
    Write an function that convert your array to an hash and compare the hashes.
    /Greetings
    Bjørn

    "Ton 't Lam" <tonl@nospam.nl > skrev i en meddelelse
    news:vMudnREQLJ fx_dTeRVnyvQ@ca sema.nl...[color=blue]
    > Suppose I have an array
    > my @arr = ( 1,4);
    >
    > I need to have the missing numbers:
    > my @res = ( 2,3,5);
    >
    > How to calculate this easy from @arr ?
    >
    > Best Regards,
    > Ton[/color]


    Comment

    • Ton 't Lam

      #3
      Re: How to calculate the missing number from an array?

      Thanks for the pointer.

      my @tmp = ( 1, 3, 4);
      my %hash = map { $_ => 1 } @tmp;
      foreach $i ( 1..5) {
      if ( ! defined @hash{$i} ) {
      # print $i,"\n";
      push @res, $i;
      }
      }
      print @res,"\n";

      Best Regards,
      Ton

      bdj wrote:[color=blue]
      > Hi Ton!
      > Write an function that convert your array to an hash and compare the hashes.
      > /Greetings
      > Bjørn
      >
      > "Ton 't Lam" <tonl@nospam.nl > skrev i en meddelelse
      > news:vMudnREQLJ fx_dTeRVnyvQ@ca sema.nl...
      >[color=green]
      >>Suppose I have an array
      >>my @arr = ( 1,4);
      >>
      >>I need to have the missing numbers:
      >>my @res = ( 2,3,5);
      >>
      >>How to calculate this easy from @arr ?
      >>
      >>Best Regards,
      >>Ton[/color]
      >
      >
      >[/color]

      Comment

      Working...