title: How do I count the letters in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch?
body: <p>How do I count the letters in Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch?</p>
  <pre><code>print(len('Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'))
  </code></pre>
  <p>Says 58</p>
  <p>Well if it was that easy I wouldn't be asking you, now would I?!</p>
  <p>Wikipedia says (<a href="https://en.wikipedia.org/wiki/Llanfairpwllgwyngyll#Placename_and_toponymy" rel="noreferrer">https://en.wikipedia.org/wiki/Llanfairpwllgwyngyll#Placename_and_toponymy</a>)</p>
  <blockquote>
  <p>The long form of the name is the longest place name in the United
  Kingdom and one of the longest in the world at 58 characters <strong>(51
  &quot;letters&quot; since &quot;ch&quot; and &quot;ll&quot; are digraphs, and are treated as single
  letters in the Welsh language).</strong></p>
  </blockquote>
  <p>So I want to count that and get the answer 51.</p>
  <p>Okey dokey.</p>
  <pre><code>print(len(['Ll','a','n','f','a','i','r','p','w','ll','g','w','y','n','g','y','ll','g','o','g','e','r','y','ch','w','y','r','n','d','r','o','b','w','ll','ll','a','n','t','y','s','i','l','i','o','g','o','g','o','g','o','ch']))
  51
  </code></pre>
  <p>Yeh but that's cheating, obviously I want to use the word as input, not the list.</p>
  <p>Wikipedia also says that the digraphs in Welsh are <strong>ch, dd, ff, ng, ll, ph, rh, th</strong></p>
  <p><a href="https://en.wikipedia.org/wiki/Welsh_orthography#Digraphs" rel="noreferrer">https://en.wikipedia.org/wiki/Welsh_orthography#Digraphs</a></p>
  <p>So off we go. Let's add up the length and then take off the double counting.</p>
  <pre><code>word='Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch'
  count=len(word)
  print('starting with count of',count)
  for index in range(len(word)-1):
    substring=word[index]+word[index+1]
    if substring.lower() in ['ch','dd','ff','ng','ll','ph','rh','th']:
      print('taking off double counting of',substring)
      count=count-1
  print(count)
  </code></pre>
  <p>This gets me this far</p>
  <pre><code>starting with count of 58
  taking off double counting of Ll
  taking off double counting of ll
  taking off double counting of ng
  taking off double counting of ll
  taking off double counting of ch
  taking off double counting of ll
  taking off double counting of ll
  taking off double counting of ll
  taking off double counting of ch
  49
  </code></pre>
  <p>It appears that I've subtracted too many then. I'm supposed to get 51. Now one problem is that with the <code>llll</code> it has found 3 <code>ll</code>s and taken off three instead of two. So that's going to need to be fixed. (Must not overlap.)</p>
  <p>And then there's another problem. The <code>ng</code>. Wikipedia didn't say anything about there being a letter &quot;ng&quot; in the name, but it's listed as one of the digraphs on the page I quoted above.</p>
  <p>Wikipedia gives us some more clue here: <strong>&quot;additional information may be needed to distinguish a genuine digraph from a juxtaposition of letters&quot;</strong>. And it gives the example of &quot;<strong>llongyfarch</strong>&quot; where the ng is just a &quot;juxtaposition of letters&quot;, and &quot;<strong>llong</strong>&quot; where it is a digraph.</p>
  <p>So it seems that 'Llanfairpwllgwy<strong>ng</strong>yllgogerychwyrndrobwllllantysiliogogogoch' is one of those words where the -ng- is bit just a &quot;juxtaposition of letters&quot;.</p>
  <p>And obviously there's no way that the computer can know that. So I'm going to have to give it that &quot;additional information&quot; that Wikipedia talks about.</p>
  <p>So anyways, I decided to look in an online dictionary <a href="http://geiriadur.ac.uk/gpc/gpc.html" rel="noreferrer">http://geiriadur.ac.uk/gpc/gpc.html</a> and you can see that if you look up <strong>llongyfarch</strong> (the example from Wikipedia that has the &quot;juxtaposition of letters&quot;) it displays it with <strong>a vertical line between the n and the g</strong> but if you look up &quot;llong&quot; then it doesn't do this.</p>
  <p><img src="https://i.stack.imgur.com/VZ9VR.png" alt="screenshot from dictionary (llongyfarch)" /></p>
  <p><img src="https://i.stack.imgur.com/yyS9G.png" alt="screenshot from dictionary (llong)" /></p>
  <p>So I've decided okay what we need to do is provide the additional information by putting a <code>|</code> in the input string like it does in the dictionary, just so that the algorithm knows that the <code>ng</code> bit is really two letters. But obviously I don't want the <code>|</code> itself to be counted as a letter.</p>
  <p>So now I've got these inputs:</p>
  <pre><code>word='llong'
  ANSWER NEEDS TO BE 3 (ll o ng)
  
  word='llon|gyfarch'
  ANSWER NEEDS TO BE 9 (ll o n g y f a r ch)
  
  word='Llanfairpwllgwyn|gyllgogerychwyrndrobwllllantysiliogogogoch'
  ANSWER NEEDS TO BE 51 (Ll a n f a i r p w ll g w y n g y ll g o g e r y ch w y r n d r o b w ll ll a n t y s i l i o g o g o g o ch)
  </code></pre>
  <p>and still this list of digraphs:</p>
  <pre><code>['ch','dd','ff','ng','ll','ph','rh','th']
  </code></pre>
  <p>and the rules are going to be:</p>
  <ol>
  <li><p><strong>ignore case</strong></p>
  </li>
  <li><p><strong>if you see a digraph then count it as 1</strong></p>
  </li>
  <li><p><strong>work from left to right so that <code>llll</code> is <code>ll</code> + <code>ll</code>, not <code>l</code> + <code>ll</code> + <code>l</code></strong></p>
  </li>
  <li><p><strong>if you see a <code>|</code> don't count it, but you can't ignore it completely, it is there to stop <code>ng</code> being a digraph</strong></p>
  </li>
  </ol>
  <p>and I want it to count it as 51 and to do it for the right reasons, not just fluke it.</p>
  <p>Now I am getting 51 but it is fluking it because it is counting the <code>|</code> as a letter (1 too high), and then it is taking off one too many with the <code>llll</code> (1 too low) - ERRORS CANCEL OUT</p>
  <p>It is getting <code>llong</code> right (3).</p>
  <p>It is getting <code>llon|gyfarch</code> wrong (10) - counting the <code>|</code> again</p>
  <p>How can I fix it the right way?</p>