18
In typography, a counter is the area of a letter that is entirely or partially enclosed by a letter form or a symbol. A closed counter is a counter that is entirely enclosed by a letter form or symbol. You must write a program takes a string as input and prints the total number of closed counters in the text.
Your input:
May be a command line input, or from STDIN, but you must specify which.
Will consist entirely of the printable ASCII characters, meaning all ASCII values between 32 and 126 inclusive. This does include spaces. More information.
Now, this does vary slightly between fonts. For example, the font you're reading this in considers 'g' to have one closed counter, whereas the google font has 'g' with two closed counters. So that this is not an issue, here are the official number of closed counters per character.
All the symbols with no closed counters:
!"'()*+,-./12357:;<=>?CEFGHIJKLMNSTUVWXYZ[\]^_`cfhijklmnrstuvwxyz{|}~
Note that this includes space.
Here are all the symbols with one closed counter:
#0469@ADOPQRabdegopq
And here are all the symbols with 2 closed counters:
$%&8B
And last but not least, here are some sample inputs and outputs.
Programming Puzzles and Code-Golf should print 13
4 8 15 16 23 42 should print 5
All your base are belong to us should print 12
Standard loopholes apply should print 12
Shortest answer in bytes is the winner! should print 8
1Two answers have submitted functions instead of full programs. While that is allowed by default, your wording suggests otherwise. Could you clarify? – Dennis – 2015-06-19T22:07:33.110
Would you mind disclosing which going you used to count the counters? – Martin Ender – 2015-06-20T13:06:50.423
@MartinBüttner I'm not sure if I understand what you mean. – James – 2015-06-20T14:07:06.907
3None of the fonts I'm viewing the question in corresponds to the counts you have given. E.g. in the browser, the zero has a diagonal slash through it, giving two counters. The font in the android app doesn't, but here the
ghas two closed counters. Did you determine the counters based on any particular font? – Martin Ender – 2015-06-20T14:10:06.977@MartinBüttner I just used whichever font came up when I wrote the question. (In the output, not the source) I guess I assumed that everybody is seeing the same font, but apparently they're not. Maybe it's a location thing? – James – 2015-06-20T14:19:02.067
1@DJMcMayhem 'g' has 1; though where listed in code,
ghas 2. Slightly confusing to read, but I don't think it's different by location. – OJFord – 2015-06-21T21:45:28.9101Doesn't
0have 2 closed counters in certain fonts, especially many monospace fonts? – vsz – 2015-06-22T06:17:19.060So what does it vary by? OS? Browser? I wrote this on Firefox on Ubuntu. – James – 2015-06-22T19:27:29.503
@DJMcMayhem It varies by formatting. You used code markup for posting which letters. Look at your question. The monospaced
gand0have 2 closed counters, which is confusing people. – mbomb007 – 2015-06-22T21:09:17.427@mbomb007 but on my computer, both g and 0 have 1 closed counter, even with the formatting. – James – 2015-06-22T21:22:36.800
Then I think you're in the minority, but since you defined the sets, it's fine. – mbomb007 – 2015-06-22T21:37:59.410