This is just a procrastination project for me, rather than anything that I expect to be useful. Here's the deal: check whether a number is the same when rotated half a turn (180 degrees or pi radians, whichever you prefer). (At this point, it's more a string than a number, but let's keep things simple.)

I've taken a few minor liberties with what counts as what when rotated; it's pretty easy to see that an upside-down 6 is a 9 (and vice versa), and that 1 and 8 are basically the same thing when rotated (despite the serifs on the 1 and any size difference your font might have on the 8's loops), but the 2<->5 thing is more of a judgement call.

Anyways, here's my implementation. It's a brute-force thing, lacking in both elegance and hack value. Can you come up with something cleverer? (This smells like a good problem to golf.)

#!/usr/bin/perl -w use strict; my %rotated = ( 0 => 0, 1 => 1, 2 => 2, 3 => undef, 4 => undef, 5 => 5, 6 => 9, 7 => undef, 8 => 8, 9 => 6 ); sub is_rotateable { my ($n) = @_; my @num = split '', $n; my @mun = reverse @num; foreach (0..$#num) { my $d = $num[$_]; return 0 if $rotated{$d} != $mun[$_]; } return 1; } for (@ARGV) { print "$_ is rotateable\n" if &is_rotateable($_); }

Edit: Yes, zeros are digits too. (D'oh!) Thanks Zaxo. Hmm... just offhand, I don't think this would be terribly interesting with, say, Roman numerals.

Edit: Duh. Thanks claree0... reading the replies to this post has been fun, but not exactly great for my ego. :-)

--
F o x t r o t U n i f o r m
Found a typo in this node? /msg me
% man 3 strfry


In reply to (Challenge) Rotateable numbers by FoxtrotUniform

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.