Ruby,405207字节
207字节
->a{i,n=0,"#{a}";[/ontag|sa/,/(he|mo)n/,/di|do|ic/,/tri/,/tet|q/,/pe/,/hex/,/hep/,/oc/,/enn/,/^d/,/1d/,/2d/].each{|r|n.gsub!(r,i.to_s);i+=1;};n=n.gsub(/ide/,'!').gsub(/[a-z]/,'');n='1'+n[0] if /!/=~n;n.to_i}
未打高尔夫球207
sides = ->a{
i,n=0,"#{a}";
# Match patterns for zero, one, two, etc.
# Each regex corresponds to a digit.
# Special patterns for 10, 11 and 12.
[/ontag|sa/,/(he|mo)n/,/di|do|ic/,/tri/,
/tet|q/,/pe/,/hex/,/hep/,/oc/,/enn/,
/^d/,/1d/,/2d/].each {|r| n.gsub!(r, i.to_s);i+=1;};
n=n.gsub(/ide/,'!'). # Change part of the teens to exclamation
gsub(/[a-z]/,''); # Remove remaining unmatched letters
n='1'+n[0] if /!/=~n; # Fixup the teens
n.to_i
}
405个字节:
->a{d=%w(on hen d tr te p hex hep oc e);case a when/de/ then %w(de hen do tr te p hex hep o e ).index(a[/^(he|t|d)?./]||:de)+10;when/^([mdtspoe]|he[xp]).{,7}$/ then %w(z m d t s p hex hep o e).index($1);when/^i.*[ks]a.([dep]|on|oc|tr|te|hen|hex|hep)/ then d.index($1)+20;when/^((he|t)?[^ht]).*nta(kai)?([gdpoe]|tr|te|hen|hex|hep)/ then (3+%w(tr te p hex hep o e).index($1))*10+(d.index($4)||0)else 0;end}
未打高尔夫球405
def sides(a)
d=%w(on hen d tr te p hex hep oc e);
case a
when /de/ then %w(de hen do tr te p hex hep o e ).index(a[/^(he|t|d)?./]||:de)+10;
when /^([mdtspoe]|he[xp]).{,7}$/ then %w(z m d t s p hex hep o e).index($1);when/^i.*[ks]a.([dep]|on|oc|tr|te|hen|hex|hep)/ then d.index($1)+20;
when /^((he|t)?[^ht]).*nta(kai)?([gdpoe]|tr|te|hen|hex|hep)/ then (3+%w(tr te p hex hep o e).index($1))*10+(d.index($4)||0)
else 0;
end
end
也许不是最好的高尔夫项目,但它可能会赢得一场混乱的代码竞赛!
测试
polygons = %w(
gone
monogon digon
triangle square
pentagon hexagon
heptagon octagon
enneagon decagon
hendecagon dodecagon
triskaidecagon tetrakaidecagon
pentakaidecagon hexakaidecagon
heptakaidecagon octakaidecagon
enneakaidecagon icosagon
icosikaihenagon icosikaidigon
icosikaitrigon icosikaitetragon
icosikaipentagon icosikaihexagon
icosikaiheptagon icosikaioctagon
icosikaienneagon triacontagon
triacontakaihenagon triacontakaidigon
triacontakaitrigon triacontakaitetragon
triacontakaipentagon triacontakaihexagon
triacontakaiheptagon triacontakaioctagon
triacontakaienneagon tetracontagon
tetracontakaihenagon tetracontakaidigon
tetracontakaitrigon tetracontakaitetragon
tetracontakaipentagon tetracontakaihexagon
tetracontakaiheptagon tetracontakaioctagon
tetracontakaienneagon pentacontagon
pentacontakaihenagon pentacontakaidigon
pentacontakaitrigon pentacontakaitetragon
pentacontakaipentagon pentacontakaihexagon
pentacontakaiheptagon pentacontakaioctagon
pentacontakaienneagon hexacontagon
hexacontakaihenagon hexacontakaidigon
hexacontakaitrigon hexacontakaitetragon
hexacontakaipentagon hexacontakaihexagon
hexacontakaiheptagon hexacontakaioctagon
hexacontakaienneagon heptacontagon
heptacontakaihenagon heptacontakaidigon
heptacontakaitrigon heptacontakaitetragon
heptacontakaipentagon heptacontakaihexagon
heptacontakaiheptagon heptacontakaioctagon
heptacontakaienneagon octacontagon
octacontakaihenagon octacontakaidigon
octacontakaitrigon octacontakaitetragon
octacontakaipentagon octacontakaihexagon
octacontakaiheptagon octacontakaioctagon
octacontakaienneagon enneacontagon
enneacontakaihenagon enneacontakaidigon
enneacontakaitrigon enneacontakaitetragon
enneacontakaipentagon enneacontakaihexagon
enneacontakaiheptagon enneacontakaioctagon
enneacontakaienneagon
)
sides = ->a{i,n=0,"#{a}";[/ontag|sa/,/(he|mo)n/,/di|do|ic/,/tri/,/tet|q/,/pe/,/hex/,/hep/,/oc/,/enn/,/^d/,/1d/,/2d/].each{|r|n.gsub!(r,i.to_s);i+=1;};n=n.gsub(/ide/,'!').gsub(/[a-z]/,'');n='1'+n[0] if /!/=~n;n.to_i}
polygons.each {|p| puts "#{p} -> #{sides.call(p)}"; }