# Introduction to pointers
# There is no concept of pointers in python
i = 3
j = id(i) # id() returns the address of the variable supplied
j = i # assign value of i to j such that it seems like j points to i
k = j
j = 4
print "i = %d, *j = %d, k = %d"%(i,j,k)
# Demonstration of passing by reference for a function to be able to modify its parameters
def modify(i) :
i = 3
return i
i = 1
print "i is now equal to %d"%i
i = modify(i)
print "i is now equal to %d"%i
# Pointer parameters in action: the swap routine
def swap(i,j) :
""" exchange i with j and return """
temp = i # temp variable can be avoided using i,j = j,i
i = j
j = temp
return i,j
i = 1
j = 9
print "i = %d and j = %d"%(i,j)
# Now exchange them
i, j = swap(i,j)
print "\nnow i = %d and j = %d"%(i,j)
# Array names are constant pointers
an_array = [ 3, 7 ]
def main() :
also = an_array
also[0] = 14
print "an_array = { %d, %d }"%(an_array[0],an_array[1])
main()
# Sorting an array revisited
# Passing an array as parameter
# Macro definition
MAX_ARRAY_SIZE = 20 # The array may have up to 20 elements
# Message flags for "print array"
ORIGINAL = 0 # Display original array
SORTED = 1 # Display sorted array
numbers = [ 10, 42, 7, 0, 923, 12, -5, 6, 19, 3 ]
array_size = 0
def get_size(size) :
""" reads the size of the array (until a valid value is entered) """
global array_size
print "How many numbers?",
size = 10
print size
while size < 1 or size > MAX_ARRAY_SIZE :
print "How many numbers?",
size = 10
print size
array_size = size
def get_array(array, size) :
""" read in the array called 'array' of size 'size' """
print "Please enter the integers:"
for i in array :
print i,
print
def print_array(array, size, message) :
""" print out the array on a single line. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) """
if message == ORIGINAL :
print "\nThe original array is:"
else :
print "\nThe sorted array is:"
for i in array :
print i,
print
def sort_array(array,size) :
""" sort 'array' of size 'size' """
for i in range(size - 1) :
for j in range(i+1, size) :
if array[i] > array[j] :
element_swap(array, i, j)
def element_swap(array, i, j) :
""" swap elements 'i' and 'j' of 'array' """
global numbers
numbers[i],numbers[j] = numbers[j],numbers[i]
get_size(array_size) # Read in the array's size
get_array(numbers, array_size) # Read in the array 'numbers' of size 'array_size'
print_array(numbers, array_size, ORIGINAL) # Echo the array
sort_array(numbers, array_size)
print_array(numbers, array_size, SORTED) # Print the sorted array
# Sorting of array revisited
# Two dimensional char array (array of strings)
# Macro definition
MAX_ARRAY_SIZE = 20 # The array may have up to 20 elements
MAX_STRING_SIZE = 10 # Each string may have up to 10 characters
# Message flags for "print_array"
ORIGINAL = 0
SORTED = 1
words = [ "cat", "pig", "dog", "earlobe", "bingo", "elbow", "likely", "radar" ]
array_size = 0
def get_size(size) :
""" Read the size of the array into size (until a value is entered) """
print "How many words? ",
size = 8
print size
while size < 1 or size > MAX_ARRAY_SIZE :
print "How many words? ",
size = 8
print size
global array_size
array_size = size
def get_array(array,size) :
""" Read in the string array called 'array' of size 'size' """
print "Please enter the words:"
for i in array :
print i
def print_array(array,size,message) :
""" Print out string array 'array' of size 'size'. 'message' is a flag indicating which message to display(ORIGINAL or SORTED) """
if message == ORIGINAL :
print "\nThe original array is:"
else :
print "\nThe sorted array is:"
for i in array :
print i
def sort_array(array,size) :
""" sort string array """
for i in range (size-1) :
for j in range (i + 1, size) :
if array[i] > array[j] :
string_swap(i,j)
def string_swap(i, j) :
""" Exchange strings """
global words
words[i],words[j] = words[j],words[i]
get_size(array_size) # Read in the array size
get_array(words, array_size) # Read in the string array "words" of size "array_size"
print_array(words, array_size, ORIGINAL) # Echo the original array
sort_array(words, array_size)
print_array(words, array_size, SORTED) # Print the sorted array