PROG0069 - Counting letters

Assignment

Write a function countLetters that codes a given sentence by counting how many times every letter from the alphabet occurs. The given sentence must be given as an argument to the function. The function must print a string as a result, in which all letters that occur in the sentence are alphabetically listed, each preceded by an integer that indicates the amount of times the letter occurs in the sentence. On counting the letters, no distinction should be made between uppercase and lowercase letters, and in the coded sentence, only lowercase letters occur. All characters from the given sentence that are not a letter from the alphabet (spaces, punctuation marks, …) must be ignored.

Example

>>> countLetters('Always look on the bright side of life.')
'2a1b1d3e2f1g2h3i1k3l1n4o1r2s2t1w1y'
>>> countLetters('And now for something completely different.')
'1a1c2d5e3f1g1h2i2l2m4n4o1p2r1s3t1w1y'
>>> countLetters('THIS IS AN EX-PARROT!!')
'2a1e1h2i1n1o1p2r2s2t1x'

Opgave

Schrijf een functie lettersTellen die een gegeven zin codeert door te tellen hoeveel keer elke letter uit het alfabet in de zin voorkomt. De gegeven zin moet als argument aan de functie doorgegeven worden. De functie moet als resultaat een string teruggeven, waarin alle letters die in de zin voorkomen worden opgelijst in alfabetische volgorde, telkens voorafgegaan door een natuurlijk getal dat aangeeft hoeveel keer de letter die erop volgt in de zin voorkomt. Bij het tellen van de letters moet geen onderscheid gemaakt worden tussen hoofdletters en kleine letters, en in de gecodeerde zin komen enkel kleine letters voor. Alle karakters uit de gegeven zin die geen letter uit het alfabet voorstellen (spaties, leestekens, …) moeten genegeerd worden.

Voorbeeld

>>> lettersTellen('Always look on the bright side of life.')
'2a1b1d3e2f1g2h3i1k3l1n4o1r2s2t1w1y'
>>> lettersTellen('And now for something completely different.')
'1a1c2d5e3f1g1h2i2l2m4n4o1p2r1s3t1w1y'
>>> lettersTellen('THIS IS AN EX-PARROT!!')
'2a1e1h2i1n1o1p2r2s2t1x'

Added by:Peter Dawyndt
Date:2011-07-29
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.