So in 1st grade math, you learn the names of polygons. Three sides is a triangle, 4 is a square, and 5 is a pentagon. However, in 1st grade honors, you go a bit further.

Your challenge

There is a naming system for polygons above a few sides, so arbitrarily large polygons have a name.

Your task is to write a program or a function that accepts the name of a polygon as input and outputs the number of sides it has.

The names of polygons are defined as in the left column of this wikipedia article with a few exceptions.

Three sided polygons will be called a triangle instead of a trigon and 4 sided polygons will be a square instead of a tetragon (assume that all the polygons are regular). Otherwise, the names in the article will be used.


  • Input will be a string value.
  • The program should print the result to STDOUT (or it should return an integer).
  • Only polygons between 3 and 99 will be inputted.
  • Your program must satisfy all test cases.
  • No standard loopholes.
  • The program doesn't need to do anything for invalid/out of range inputs.
  • Scoring is in bytes. Since this is , shortest program wins.

Test cases

3   triangle
4   square
5   pentagon
10  decagon
11  hendecagon
12  dodecagon
13  triskaidecagon
20  icosagon
21  icosikaihenagon
22  icosikaidigon
34  triacontakaitetragon
35  triacontakaipentagon
36  triacontakaihexagon
47  tetracontakaiheptagon
48  tetracontakaioctagon
49  tetracontakaienneagon
64  hexacontakaitetragon
80  octacontagon
81  octacontakaihenagon
99  enneacontakaienneagon


Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

var QUESTION_ID=75229,OVERRIDE_USER=41505;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes
  • 10
    \$\begingroup\$ Related \$\endgroup\$ Mar 9, 2016 at 21:45
  • 1
    \$\begingroup\$ Not all tetragons are squares. \$\endgroup\$
    – Dennis
    Mar 10, 2016 at 0:39
  • \$\begingroup\$ @Dennis But all regular tetragons are. \$\endgroup\$
    – Daniel M.
    Mar 10, 2016 at 0:41
  • \$\begingroup\$ Note that the question that FryAmTheEggman points out goes in the opposite direction: This question goes from name to number of sides, while the prior one goes from number of sides to name. \$\endgroup\$
    – isaacg
    Mar 10, 2016 at 8:42
  • \$\begingroup\$ Close-voters: These questions are not the same. \$\endgroup\$
    – Daniel M.
    Mar 10, 2016 at 11:17

3 Answers 3


Ruby, 405 207 bytes

207 bytes

->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}

Ungolfed 207

    sides = ->a{
          # Match patterns for zero, one, two, etc. 
          # Each regex corresponds to a digit.
          # Special patterns for 10, 11 and 12.
             /^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

405 bytes:

->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}

Ungolfed 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;

Maybe not the best golf submission, but it might win an obfuscated code contest!


polygons = %w(
  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

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)}"; }

Python2 - 357 368 byte

Since the only real exceptions to the system are "square", "hendecagon" and "dodecagon", all other numbers follow the same pattern of having "kai" for second digits and "conta"/"deca" for their first digit.

import sys;s=input();e=eval;x='y(str(10*i(b)+i(a)))';y=sys.exit;a=b='0';l=['0','hen','di','tri','tet','pen','hex','hep','oct','enn'];i=l.index
if'kai'in s:
 a=[f for f in l if f in s[-8:]][0]
if'ca'in s:
 if l[1]in s:y("11")
 if'do'in s:y("12")
elif'co'in s:
 l[2]='ico';b=[f for f in l if f in s[:5]][0];e(x)
if'sq'in s:y("4")


import sys;s=input();e=eval;x='y(str(10*i(b)+i(a)))';y=sys.exit;a=b='0'
# List of all defining numerals, index equals the number.

if'kai'in s:                       # check if the input is a two-digit number 
                                   # with a second digit greater than 0
 a=[f for f in l if f in s[-8:]][0] # get the second digit, and store it.

if'ca'in s:                        # Special case for 10-19
 if l[1]in s:y("11")               # Exceptions 11 & 12 
 if'do'in s:y("12")                #
elif'co'in s:                      # All other two digit numbers
 b=[f for f in l if f in s[:5]][0] # Get the first digit, and store it
if'sq'in s:y("4")                  # Exception "square"
y(str(i(s[:3])))                   # All other single digit numbers 

This works for all inputs between 3-99, and prints the result to the console.

Might be more golfable, but this is as far as I can go right now.

**Edit: I just realized, this printed to STDERR, not STDOUT. Fixed code is a little longer:

import sys;s=input();e=eval;x="10*i(b)+i(a)";y=sys.exit;a=b='0';l=['0','hen','di','tri','tet','pen','hex','hep','oct','enn'];i=l.index
if'kai'in s:a=[f for f in l if f in s[-8:]][0]
if'ca'in s:
 if l[1]in s:print"11"
 if'do'in s:print"12"
 b=l[1];print e(x);y()
elif'co'in s:l[2]='ic';b=[f for f in l if f in s[:5]][0];print e(x);y()
if'q'in s:print"4"
  • \$\begingroup\$ Can you save a byte by just testing for q instead of sq? \$\endgroup\$
    – Neil
    Mar 12, 2016 at 15:23
  • \$\begingroup\$ Yup, youre right. Ty \$\endgroup\$
    – Kijata
    Mar 13, 2016 at 21:03
  • \$\begingroup\$ You remove the import sys; and just use y=exit. It does the same thing. \$\endgroup\$
    – Riker
    Mar 21, 2016 at 18:12

Cinnamon Gum, 430 bytes

0000000: 6c4c ce85 0d04 4108 05d0 6e36 9e5b 379a  lL....A...n6.[7.
0000010: 5977 f7f2 6f02 8410 fdbc 11fe 75f4 f9d2  Yw..o.......u...
0000020: 4eb5 e5c1 b9df f951 5b3e 6cf5 72e5 edba  N......Q[>l.r...
0000030: 5801 74f5 8729 3469 238c 602d 29c5 502f  X.t..)4i#.`-).P/
0000040: 4b8d 3181 aa2e 3139 b6b9 bac8 e440 b5ca  K.1...19.....@..
0000050: e082 5977 8e79 2fe2 c155 5f47 ae89 f76b  ..Yw.y/..U_G...k
0000060: a21e 5ab8 8f26 eaa5 85fb 694a a02f d713  ..Z..&....iJ./..
0000070: b36b 63ee cdb1 294c e408 553d 822b 701d  .kc...)L..U=.+p.
0000080: 249e 0836 47f3 c5b0 3a5a a07e ff88 4245  $..6G...:Z.~..BE
0000090: 1b5f 8bc4 d692 2916 c2fa 6809 98fd 79b9  ._....)...h...y.
00000a0: f2ef 9e0d 32ff 9baa 8b43 3982 288a a121  ....2....C9.(..!
00000b0: 35d4 c3fc 03b3 3f4b cbd2 1d3a 43ad d77f  5.....?K...:C...
00000c0: 1ef2 9fe1 bc44 1ce7 b862 39e1 8ee7 a43a  .....D...b9....:
00000d0: a653 ceb8 4eab 633b e38c efec d7f7 0060  .S..N.c;.......`
00000e0: dc04 0be3 a143 8c57 1563 1c67 2123 3c48  .....C.W.c.g!#<H
00000f0: 19c9 6066 94bb 9cd1 0c86 c6b8 4b1a fbf5  ..`f........K...
0000100: f546 9a37 c1d2 7ce8 48f3 a54a 9ac7 59d2  .F.7..|.H..J..Y.
0000110: 0c0f 9266 7a40 9ae5 2e69 b607 a439 ee92  [email protected]..
0000120: e67e fe6a 81d6 cd59 ce7a c87f cc7a 8928  .~.j...Y.z...z.(
0000130: eb50 8dac a0db 5849 17b1 8a6c 6135 5dc0  .P....XI...la5].
0000140: 1ab2 7db5 5fff 4100 fb26 58d8 0f1d 62bf  ..}._.A..&X...b.
0000150: 5419 fb28 1bd9 a1c1 ca4e 0633 bbd4 edec  T..(.....N.3....
0000160: 6630 b447 ddd2 decf bf56 a073 7396 739e  f0.G.....V.ss.s.
0000170: bf0c 735e 22ca 3954 2327 e836 4ed2 459c  ..s^".9T#'.6N.E.
0000180: 225b 384d 1770 866c dfec d785 02e0 de04  "[8M.p.l........
0000190: 0bf7 a143 dc97 2ae3 1e65 2337 3458 b9c9  ...C..*..e#74X..
00001a0: 60e6 96ba 9ddb 0c86 eea8 5bba fb1f       `.........[...

This is a hexdump of the source code; you can reverse it using xxd -r.

Try it online.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.