""" array.py An Indexed Array "abstract data type" implemented as a python class Running it looks like this : $ python array.py A random IndexedArray of 10 numbers is The values are The smallest is 2 """ from random import randint class IndexedArray: """ An indexed array with a fixed size """ def __init__(self, size): # special : invoked to initialize an array self.size = size # the number of elements in this array self.values = [0]*size def get(self, i): """ return the i'th value in this array """ return self.values[i] def put(self, i, value): """ assign the i'th entry in the array to this value """ self.values[i] = value def __str__(self): # special : redefines str(array) return f"" # ---------------------------------------------------------------------- def random_array(n): """ Return an IndexedArray of size n with random values from 1 to 10*n """ result = IndexedArray(n) for i in range(n): result.put(i, randint(1, 10*n)) return result print("A random IndexedArray of 10 numbers is ", random_array(10)) # ------------------------------------------------------------------------ def smallest(array): """ Return the smallest value from an IndexedArray """ # array.size : number of elements # array.get(i) : read value at i if array.size == 0: return None else: answer = array.get(0) for i in range(1, array.size): if array.get(i) < answer: answer = array.get(i) return answer def main(): values = random_array(10) print(" The values are ", values) small = smallest(values) print(" The smallest is ", small) main()