Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

3.2 Example Uses of Arrays

The source listing below provides a simple program that illustrates the creation and use of an array based on the Array ADT. Comments are provided to highlight the use of the operator methods.

Program Listing
Program: randomarray.py
  1. # Fill a 1-D array with random values, then print them, one per line.
  2. from ezarrays import Array
  3. import random
  4.  
  5. # The constructor is called to create the array.
  6. valueList = Array(100)
  7.  
  8. # Fill the array with random floating-point values.
  9. for i in range(len(valueList)) :
  10.   valueList[i] = random.random()
  11.  
  12. # Print the values, one per line.
  13. for i in range(len(valueList)) :
  14.   valueList[i] = random.random()

As a second example, suppose you need to read the contents of a text file and count the number of letters occurring in the file with the results printed to the terminal. We know that characters are represented by the ASCII code, which consists of integer values. The letters of the alphabet, both upper- and lowercase, are part of what's known as the printable range of the ASCII code. This includes the ASCII values in the range [32 ... 126] along with some of the codes with smaller values. The latter are known control characters and can include the tab, newline, and form-feed codes. Since all of the letters will have ASCII values less than 127, we can create an array of this size and let each element represent a counter for the corresponding ASCII value. After processing the file, we can traverse over the elements used as counters for the letters of the alphabet and ignore the others. The following program provides a solution to this problem using the Array ADT:

Program Listing
Program: lettercount.py
  1. # Count the number of occurrences of each letter in a text file.
  2. from ezarrays import Array
  3.  
  4. # Create an array for the counters and initialize each element to 0.
  5. theCounters = Array(127)
  6. theCounters.clear(0)
  7.  
  8. # Open the text file for reading and extract each line from the file
  9. # and iterate over each character in the line.
  10. theFile = open("atextfile.txt", "r")
  11. for line in theFile :
  12.   for letter in line :
  13.     code = ord( letter )
  14.     theCounters[code] += 1    
  15. # Close the file
  16. theFile.close()
  17.  
  18. # Print the results. The uppercase letters have ASCII values in the
  19. # range 65..90 and the lowercase letters are in the range 97..122.
  20. for i in range(26) :
  21.   print( "%c - %4d          %c - %4d" %
  22.     (chr(65+i), theCounters[65+i], chr(97+i), theCounters[97+i]))

Python does not provide the array structure as part of the language itself, but it does include low-level modules that provide access to the diverse set of data types available at the hardware-level, including arrays. The use of these low-level modules to create a hardware array is beyond the scope of this text. Thus, we have included the ezarray module as part of this textbook. That module provides the Array class that implements the Array ADT using a true hardware array. Documentation for the ezarray module is available online at http://ezarrays.necaise.org.

Page last modified on July 26, 2023, at 04:48 PM