Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Dictionary Exercises

  1. Word Frequencies. Update the exercise in Accumulating Unique Values to count each occurance of the values in aSequence. Change the result from a simple sequence to a dict. The dict key is the value from aSequence. The dict value is the count of the number of occurances.

    If this is done correctly, the input sequence can be words, numbers or any other immutable Python object, suitable for a dict key.

    For example, the program could accept a line of input, discarding punctuation and breaking them into words in space boundaries. The basic string operations should make it possible to create a simple sequence of words.

    Iterate through this sequence, placing the words into a dict. The first time a word is seen, the frequency is 1. Each time the word is seen again, increment the frequency. Produce a frequency table.

    To alphabetize the frequency table, extract just the keys. A sequence can be sorted (see section 6.2). This sorted sequence of keys can be used to extract the counts from the dict.

  2. Stock Reports. A block of publicly traded stock has a variety of attributes, we'll look at a few of them. A stock has a ticker symbol and a company name. Create a simple dict with ticker symbols and company names.

    For example:

    stockDict = { 'GM': 'General Motors', 
     'CAT':'Caterpillar', 'EK':"Eastman Kodak" }

    Create a simple list of blocks of stock. These could be tuples with ticker symbols, prices, dates and number of shares. For example:

    purchases = [ ( 'GE', 100, '10-sep-2001', 48 ), 
     ( 'CAT', 100, '1-apr-1999', 24 ), 
     ( 'GE', 200, '1-jul-1998', 56 ) ]

    Create a purchase history report that computes the full purchase price (shares times dollars) for each block of stock and uses the stockDict to look up the full company name. This is the basic relational database join algorithm between two tables.

    Create a second purchase summary that which accumulates total investment by ticker symbol. In the above sample data, there are two blocks of GE. These can easily be combined by creating a dict where the key is the ticker and the value is the list of blocks purchased. The program makes one pass through the data to create the dict. A pass through the dict can then create a report showing each ticker symbol and all blocks of stock.

  3. Date Decoder. A date of the form 8-MAR-85 includes the name of the month, which must be translated to a number. Create a dict suitable for decoding month names to numbers. Create a function which uses string operations to split the date into 3 items using the "-" character. Translate the month, correct the year to include all of the digits.

    The function will accept a date in the "dd-MMM-yy" format and respond with a tuple of ( y , m , d ).

  4. Dice Odds. There are 36 possible combinations of two dice. A sim→ple pair of loops over range(6)+1 will enumerate all combinations. The sum of the two dice is more interesting than the actual combination. Create a dict of all combinations, using the sum of the two dice as the key.

    Each value in the dict should be a list of tuples; each tuple has the value of two dice. The general outline is something like the following:

    d= {}
    Loop with d1 from 1 to 6
        Loop with d2 from 1 to 6
            newTuple ← ( d1, d2 ) # create the tuple
            oldList ← dictionary entry for sum d1+d2
            newList ← oldList + newTuple
            replace entry in dictionary with newList
    Loop over all values in the dictionary
        print the key and the length of the list

  Published under the terms of the Open Publication License Design by Interspire