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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |