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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Challenge) Rotateable numbers
by BrowserUk (Patriarch) on Sep 16, 2004 at 02:05 UTC | |
|
Re: (Challenge) Rotateable numbers
by tachyon (Chancellor) on Sep 16, 2004 at 08:00 UTC | |
by BrowserUk (Patriarch) on Sep 17, 2004 at 02:00 UTC | |
by tachyon (Chancellor) on Sep 17, 2004 at 02:51 UTC | |
by dragonchild (Archbishop) on Sep 16, 2004 at 13:26 UTC | |
by tachyon (Chancellor) on Sep 17, 2004 at 02:47 UTC | |
by dragonchild (Archbishop) on Sep 17, 2004 at 13:21 UTC | |
|
Re: (Challenge) Rotateable numbers
by belg4mit (Prior) on Sep 16, 2004 at 01:15 UTC | |
|
Re: (Challenge) Rotateable numbers
by dragonchild (Archbishop) on Sep 16, 2004 at 01:44 UTC | |
by belg4mit (Prior) on Sep 16, 2004 at 02:37 UTC | |
|
Re: (Challenge) Rotateable numbers
by claree0 (Hermit) on Sep 22, 2004 at 07:09 UTC | |
by ysth (Canon) on Sep 22, 2004 at 07:32 UTC |