..  Copyright (C) 2011  Brad Miller and David Ranum
    Permission is granted to copy, distribute
    and/or modify this document under the terms of the GNU Free Documentation
    License, Version 1.3 or any later version published by the Free Software
    Foundation; with Invariant Sections being Forward, Prefaces, and
    Contributor List, no Front-Cover Texts, and no Back-Cover Texts.  A copy of
    the license is included in the section entitled "GNU Free Documentation
    License".



Counting Letters
================


Simple Solution: Counting a Single Letter
-----------------------------------------

We have previously discussed the solution to the problem of counting the number of times a specific letter appears
in a string.  In the case below, that specific letter is "a".

.. activecode:: chp12_single

    def countA(text): 
        count = 0
        for c in text:
            if c == 'a':
                count = count + 1
        return count

    print(countA("banana"))

Of course, we could also solve this problem by using the ``count`` method provided for strings.

.. sourcecode:: python

    def countA(text): 
        
        return text.count("a")




General Solution: Counting All Letters
--------------------------------------

Now we will generalize the counting problem and consider how to count the number of times each letter appears in
a given string.  In order to do this we need to realize that writing a function that returns a single integer will no
longer work.  Instead we will need to return some kind of collection that holds the counts for each character.

Although there may be many possible ways to do this, we suggest a dictionary where the keys of the dictionary will be
the letters in the string and the associated values for each key will be the number of times that the letter
appeared.

What about a letter that does not appear in the string?  It will never be placed in the dictionary.  By assumption, any
key that is not in the dictionary has a count of 0.

If we call the function ``countAll``, then a call to ``countAll`` would return the dictionary.  For example,

.. sourcecode:: python

    print(countAll("banana"))

would return the dictionary

.. sourcecode:: python

    {"a":3, "b":1, "n":2}




Here is a start to the development of the function.

#. Define the function to require one parameter, the string.

#. Create an empty dictionary of counts.

#. Iterate through the characters of the string, one character at a time.



