#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2015-1-6
@author: beyondzhou
@name: test_2darray.py
'''
def test_2darray():
from myarray import Array2D
# Open the text file for reading
gradeFile = open(r'e:\pica8\eclipse\grade.txt', 'r')
# Extract the first two values which indicate the size of the array
numStudents = int(gradeFile.readline())
numExams = int(gradeFile.readline())
# Create the 2D array to store the grades
examGrades = Array2D(numStudents, numExams)
# Extract the grades from the remaining lines
records = gradeFile.readlines()
i = 0
for student in records:
grades = student.split()
for j in range(numExams):
examGrades[i,j] = int(grades[j])
i += 1
# close the text file
gradeFile.close()
# compute each student's aveerage exam grade
for i in range(numStudents):
# Tally the exam grades for the ith student
total = 0
for j in range(numExams):
total += examGrades[i,j]
# Compute average for the ith student
examAvg = total / float(numExams)
print "%2d: %6.2f" % (i+1, examAvg)
if __name__ == "__main__":
test_2darray()
# An iterator for the Array ADT
class _ArrayIterator:
def __init__(self, theArray):
self._arrayRef = theArray
self._curNdx = 0
def __iter__(self):
return self
def next(self):
if self._curNdx < len(self._arrayRef):
entry = self._arrayRef[self._curNdx]
self._curNdx += 1
return entry
else:
raise StopIteration
# Implements the Array ADT using array capabilities of the ctypes module
import ctypes
class Array:
# Creates an array with size elements
def __init__(self, size):
assert size > 0, "Array size must be > 0"
self._size = size
# Create the array structure using the ctypes module
PyArrayType = ctypes.py_object * size
self._elements = PyArrayType()
# Initialize each element
self.clear(None)
# Returns the size of the array
def __len__(self):
return self._size
# Gets the contents of the index element
def __getitem__(self, index):
assert index >= 0 and index < len(self), "Array subscript out of range"
return self._elements[index]
# Puts the value in the array element at index position
def __setitem__(self, index, value):
assert index >= 0 and index < len(self), "Array subscript out of range"
self._elements[index] = value
# Clears the array by setting each element to the given value
def clear(self, value):
for i in range(len(self)):
self._elements[i] = value
# Returns the array's iterator for traversing the elements
def __iter__(self):
return _ArrayIterator(self._elements)
# Implementation of the Array2D ADT using an array of arrays
class Array2D:
# Creates a 2D array of size numRows * numCols
def __init__(self, numRows, numCols):
# Create a 1D array to store an array reference for each row
self._theRows = Array(numRows)
# Create a 1D array for each row of the 2D array
for i in range(numRows):
self._theRows[i] = Array(numCols)
# Returns the number of rows in the 2D array
def numRows(self):
return len(self._theRows)
# Returns the number of columns in the 2D array
def numCols(self):
return len(self._theRows[0])
# Clears the array by setting every element to the given value
def clear(self, value):
for row in range(self.numRows()):
self._theRows[row].clear(value)
# Gets the contents of the element at position [i, j]
def __getitem__(self, ndxTuple):
assert len(ndxTuple) == 2, "Invalid number of array subscripts."
row = ndxTuple[0]
col = ndxTuple[1]
assert row >= 0 and row < self.numRows() \
and col >= 0 and col < self.numCols(), \
"Array subscript out of range."
the1dArray = self._theRows[row]
return the1dArray[col]
# Sets the contents of the element at position [i, j] to value
def __setitem__(self, ndxTuple, value):
assert len(ndxTuple) == 2, "Invalid number of array subscripts."
row = ndxTuple[0]
col = ndxTuple[1]
assert row >= 0 and row < self.numRows() \
and col >= 0 and col < self.numCols(), \
"Array subscript out of range."
the1dArray = self._theRows[row]
the1dArray[col] = value
cat grade.txt
7
3
90 96 92
85 91 89
82 73 84
69 82 86
95 88 91
78 64 84
92 85 89
Result:
1: 92.67
2: 88.33
3: 79.67
4: 79.00
5: 91.33
6: 75.33
7: 88.67