Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: add missing elements and remove duplicates....and some more questions.

by pritesh_ugrankar (Monk)
on Apr 22, 2017 at 22:49 UTC ( [id://1188640]=note: print w/replies, xml ) Need Help??


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

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!!
  • Comment on Re^2: add missing elements and remove duplicates....and some more questions.
  • Download Code

Replies are listed 'Best First'.
Re^3: add missing elements and remove duplicates....and some more questions.
by NetWallah (Canon) on Apr 23, 2017 at 00:54 UTC
    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!!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1188640]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found