pritesh_ugrankar has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks,
I've been here since past few weeks and gleaned a lot of knowledge here. I recently started getting back to Perl, the only language that I know, for fun and may be more than fun in future. I had last week posted a question anonymously and got astounding replies and encouragement to post questions here with my login name. Hence this time, I have logged in with my name and asking questions.
I was asked to come up with the following without using sorting.
Given a random series of numbers, which may include hex numbers, write a script that will remove the duplicates, fill in the gaps if any, and print the numbers in a sequence. Do not use hash, do not use any existing libraries, but "do it yourself" as much as you can.
So I came up with this
Update: Please disregard the code below and refer to the Important Update section.
$ more /home/pritesh/nosort.pl use warnings; use strict; my @arr = (10,10,20,0x47,1,30,45,45); sub do_it_all { my $biggest = shift @_; foreach my $num (@_) { if ($num > $biggest) { $biggest = $num; } } my $smallest = shift @_; foreach my $smallnum (@_) { if ($smallnum < $smallest) { $smallest = $smallnum; } } my @unique = ($smallest..$biggest); print "@unique\n"; } &do_it_all(@arr);
Which prints the following.
$perl nosort.pl 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 2 +7 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 5 +0 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
I was given the following feedback.
That I did not try to address the issue, but circumvented the issue by simply taking a shortcut of finding the smallest number and then the biggest number and generating the numbers.
That I should have done more research and come up with a better solution which would check each number with the preceding one and done some better programming.
This is going to sound funny, but I was told not to use Linux Mint as it is not a "real Linux distro" and that people who do programming/scripting do not use Mint/Ubuntu as they are not "up to the mark". This was the remark that frankly made me suspicious of his line of thought.
I have not till date seen such remarks here by any of the esteemed monks. Can you please shed more light on any of the points mentioned above? Is this kind of shortcuts considered a "non programmer's approach" to problem solving?
Important Update
AnomalousMonk just made me understand the big flaw in my code. I was shifting @_ recklessly without proper checking. Hence if my array contained (1,2,3,4) it would print (I added the $smallest and $biggest lines just to highlight the error):
$smallest: 2 $biggest: 4 2 3 4
That's because I was shifting off @_, but what I should have done is used $_[0]. If I use that, then I get the right answer. So the code is now changed to:
use strict; use warnings; my @arr = (1, 2, 3, 4); sub do_it_all { my $biggest = $_[0] ; foreach my $num (@_) { if ($num > $biggest) { $biggest = $num; } } my $smallest = $_[0]; foreach my $smallnum (@_) { if ($smallnum < $smallest) { $smallest = $smallnum; } } print "\$smallest = $smallest\t\$biggest = $biggest\n"; my @unique = ($smallest..$biggest); print "@unique\n"; } &do_it_all(@arr);
And the output is:
$smallest = 1 $biggest = 4 1 2 3 4
If I now try it with the original array: my @arr = (10,10,20,0x47,1,30,45,45), that works as well.
$smallest = 1 $biggest = 71 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 2 +7 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 5 +0 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
I owe all the monks a sincere apology. I should have checked this as a basic safety measure and it should not have been missed. Thanks once again to Anomalous Monk.
|
|---|