in reply to (Challenge) Rotateable numbers

37

sub is_rotatable{ #234567890123456789012345678901234567 $_=$_[0];y/2569347/5296/;pop==reverse }

cheers

tachyon

Replies are listed 'Best First'.
Re^2: (Challenge) Rotateable numbers
by BrowserUk (Patriarch) on Sep 17, 2004 at 02:00 UTC

    The reason tachyon's version doesn't work is not because of removing the 347, but because he golfed away the localisation of $_.

    P:\test>type junk.pl sub is_rotatable{ #234567890123456789012345678901234567 $_=$_[0];y/2569347/5296/;pop==reverse } printf "%s %s rotatable\n", $_, is_rotatable( $_ ) ? 'is' : 'not' for 33, 131, 141, 171, 25696925, 101, 88; P:\test>junk Modification of a read-only value attempted at P:\test\junk.pl line 3.

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      As noted at Re^3: (Challenge) Rotateable numbers the tr does not remove the digits 347 it replaces them with the digit 6. Not that it matters either way. Localisation does not matter either unless you happen to use $_ in your loop test case.....Nice algorithm BTW.

      cheers

      tachyon

Re^2: (Challenge) Rotateable numbers
by dragonchild (Archbishop) on Sep 16, 2004 at 13:26 UTC
    No good. 33 isn't rotatable, but yours reports it as being so. (You can't strip the 347 out and compare the rest for rotatability.)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Given that it does not report 33 as rotatable I don't see your point. Also the digits 347 are not stripped, they are replaced with the digit 6, see the man for tr. It would not actually matter if they were stripped or replaced the original value will not be equal to the reversed value.

      sub is_rotatable{ #234567890123456789012345678901234567 $_=$_[0];y/2569347/5296/;pop==reverse } for my $i(3,33,131,141,171,25696925,101,88) { printf "%s %s rotatable\n", $i, is_rotatable( $i ) ? 'is' : 'not' } __DATA__ 3 not rotatable 33 not rotatable 131 not rotatable 141 not rotatable 171 not rotatable 25696925 is rotatable 101 is rotatable 88 is rotatable

      cheers

      tachyon

        I found the problem. It had to do with the fact that your version doesn't respect $_, while mine does.
        sub r2{ $_=$_[0];y/2569347/5296/;pop==reverse } my @tests = (0..9); print "$_: ",(r2($_)?'Yes':'No'), "\n" for @tests; __DATA__ 0: Yes 1: Yes 2: Yes 3: Yes 4: Yes 5: Yes 6: Yes 7: Yes 8: Yes 9: Yes

        Because of the way I was calling your function, I saw something wrong. However, I mistook what I saw wrong and I apologize for not knowing tr/// as well as I should have.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested