in reply to add missing elements and remove duplicates....and some more questions.

Programming is problem solving. You solved the problem (in a way I like, in fact). If they wanted you to implement a particular algorithm, they should've told you so.

Note that your input list doesn't contain hex numbers, though. You included 0x47 to the list, but it's a literal, so Perl will translate it into number during compilation. Try to define the array with strings (as if coming from an input file):

my @arr = qw( 10 10 20 0x47 1 30 45 45 );

Can you translate this into a list of numbers with the same elegance and wit?

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: add missing elements and remove duplicates....and some more questions.
by pritesh_ugrankar (Monk) on Apr 22, 2017 at 22:49 UTC

    Hi Choroba,

    Thank you for your valuable input. I was reading Learning Perl book and used the logic given in one of the chapters therein to find the largest and smallest number :)

    I was given the array numbers by the trainer himself. I was told that my answer is right, but method is incorrect. The number 0x47 should resolve to decimal 71 and then I was asked to generate the list in a sequence.

    I am sorry but I did not understand your question. Could you please elaborate?

    Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!
      Congrats on coming up with a creative and correct solution.

      One improvement you could make is to get the largest and smallest number in a single pass "for" loop.

      The point that choroba was making is the subtle difference between the way "hex" representation of numbers is stored.

      In your code, perl will automatically convert 0x47 into it's internal binary representation, thereby losing the "original" representation.

      However, if you maintain the quoted string "0x47" as a string (which is what the "qw" operator would do), you could maintain the "original" representation.

      All this is "extra" since the teacher did not mention any requirement to maintain original representation.

      Also - Trying to maintain the original - will add a small amount of complexity - you will need to convert to binary, before making comparisons. The simplest way to do that is to add 0 to it.

              ...Disinformation is not as good as datinformation.               Don't document the program; program the document.

        Hi Choroba and Netwallah,

        So you are basically saying I should sort it as strings rather than numbers? I first thought you were saying that I should convert the numbers to binary, but that is what perl would automatically do as stated earlier in Netwallah's reply. So it will be same as what I had done earlier.

        $ more stringnum.pl use strict; use warnings; use v5.10.1; my @arr= qw(10 10 20 0x47 1 30 45 45); sub do_it_all { no warnings 'numeric'; my $biggest = @_; foreach my $bignum (@_) { if ($bignum > $biggest) { $biggest = $bignum; } } my $smallest = @_; foreach my $smallnum (@_) { if ($smallnum < $smallest) { $smallest = $smallnum; } } push my @unique, $smallest..$biggest; print "@unique\n"; } &do_it_all(@arr);

        This changes 0x47 to 0 though. Please see the output below.

        $ perl stringnum.pl 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 + 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 $

        If you meant that the strings should be converted to the corresponding decimal and hex equivalents, I am working on that. This is fun!! I thought solving puzzles was fun. This is way better. :)

        Thinkpad T430 with Ubuntu 16.04.2 running perl 5.24.1 thanks to plenv!!