Number of sides on a polygon
+1
−0
Input
- The name of a polygon.
- You are only required to handle inputs listed in the test cases.
Output
- The number of edges the polygon has.
Test cases
Test cases are in the format input : output.
triangle : 3
trigon : 3
quadrilateral : 4
quadrangle : 4
tetragon : 4
rectangle : 4
square : 4
rhombus : 4
parallelogram : 4
kite : 4
trapezium : 4
pentagon : 5
hexagon : 6
heptagon : 7
septagon : 7
octagon : 8
enneagon : 9
nonagon : 9
decagon : 10
hendecagon : 11
undecagon : 11
dodecagon : 12
triskaidecagon : 13
tridecagon : 13
tetrakaidecagon : 14
tetradecagon : 14
pentakaidecagon : 15
pentadecagon : 15
hexakaidecagon : 16
hexadecagon : 16
heptakaidecagon : 17
heptadecagon : 17
octakaidecagon : 18
octadecagon : 18
enneakaidecagon : 19
enneadecagon : 19
icosagon : 20
Scoring
This is a code golf challenge. Your score is the number of bytes in your code. Lowest score for each language wins.
Explanations are optional, but I'm more likely to upvote answers that have one.
1 answer
+1
−0
Perl, 102 bytes
perl -nE'%h=@a=de0hen1u1d2tri3t4pe5hex6h7se7o8e9n9i20=~/\D+|\d+/g;$"="|";say/^(@a)/?$h{$1}+10*/dec/:4'
perl -nE'
# split bareword into list of non-digits and digits
# initialise regex alternation list @a from list
# initialise mapping table %h from @a
%h = @a = de0hen1u1d2tri3t4pe5hex6h7se7o8e9n9i20 =~ /\D+|\d+/g;
# set array->string interpolation character for use in regex
$"="|";
say
/^(@a)/ # if prefix match
? $h{$1} # map to number
+ 10*/dec/ # add ten if string contains "dec"
: 4 # else assume is 4
'

0 comment threads