# Chapter 2 : Arrays¶

### Example 2.1 Page no :41¶

In [1]:


nElems = 0
arr = [77,99,44,55,22,88,11,00,66,33]

for j in range(len(arr)):
# display items
print arr[j] ,
print ''

searchKey = 66
# find item with key 66
for j in range(len(arr)):
if(arr[j] == searchKey):
break
if(j == len(arr)):
print 'Cant find ',  searchKey
else:
print 'Found ' ,searchKey

searchKey = 55
for j in arr:
if(j == searchKey):
arr.remove(searchKey)

for j in range(len(arr)):
# display items
print arr[j] ,
print ''

77 99 44 55 22 88 11 0 66 33
Found  66
77 99 44 22 88 11 0 66 33


### Example 2.2 Page No : 44¶

In [2]:


class LowArray:
def __init__(self,size):
self.a = []
for i in range(size):
self.a.append(0.0)

def setElem(self,index,value):
# set value
self.a[index] = value

def getElem(self,index):
return self.a[index]

arr = LowArray(100)
arr.setElem(0,77)
arr.setElem(1,99)
arr.setElem(2,44)
arr.setElem(3,55)
arr.setElem(4,22)
arr.setElem(5,88)
arr.setElem(6,11)
arr.setElem(7,00)
arr.setElem(8,66)
arr.setElem(9,33)
nElems = 10

# now 10 items in array
for j in range(nElems):
# display items
print arr.getElem(j) ,

print ''

searchKey = 26
find = False
# search for data item
for j in range(nElems):
# for each element,
if(arr.getElem(j) == searchKey): # found item?
find = True
break
if(not find):
print "Can't find " , searchKey
else:
print "Found " , searchKey

for j in range(nElems):
if(arr.getElem(j) == 55):
arr.a.remove(55)
nElems -= 1

for j in range(nElems):
# display items
print arr.getElem(j) ,


77 99 44 55 22 88 11 0 66 33
Can't find  26
77 99 44 22 88 11 0 66 33


### Example 2.3 Page No : 49¶

In [3]:


class HighArray:
def __init__(self,size):
self.a = []
self.nElems = 0
for i in range(size):
self.a.append(0.0)

def insert(self,value):
# set value
self.a[self.nElems] = value
self.nElems += 1

def find(self,searchKey):
for j in range(self.nElems):
if(self.a[j] == searchKey):
return True
return False

def delete(self,value):
for j in range(self.nElems):
# look for it
if (value == self.a[j]):
self.a.remove(value)
self.nElems -= 1

def display(self):
for j in range(self.nElems+1):
print self.a[j] ,
print ''

arr = HighArray(100)
arr.insert(77)
arr.insert(99)
arr.insert(44)
arr.insert(55)
arr.insert(22)
arr.insert(88)
arr.insert(11)
arr.insert(00)
arr.insert(66)
arr.insert(33)

arr.display()

searchKey = 35
if( arr.find(searchKey) ):
print "Found " , searchKey
else:
print "Can't find " , searchKey
arr.delete(00)
arr.delete(55)
arr.delete(99)
arr.display()

77 99 44 55 22 88 11 0 66 33 0.0
Can't find  35
77 44 22 88 11 66 33


### Example 2.4 Page no : 59¶

In [4]:


class OrdArray:
def __init__(self,m):
self.a = []
self.nElems = 0

def size(self):
return self.nElems

def find(self,searchKey):
lowerBound = 0
upperBound = self.nElems-1
while True:
curIn = (lowerBound + upperBound ) / 2
if(self.a[curIn]==searchKey):
return curIn
elif(lowerBound > upperBound):
return self.nElems
else:
if(self.a[curIn] < searchKey):
lowerBound = curIn + 1
else:
upperBound = curIn - 1

def insert(self,value):
self.a.append(value)
self.a.sort()
self.nElems += 1

def delete(self,value):
j = self.find(value)
if(j==self.nElems):
return False
else:
self.a.remove(value)
self.nElems -=1

def display(self):
for i in self.a:
print i ,
print ''

maxSize = 100
arr = OrdArray(maxSize)
arr.insert(77)
arr.insert(99)
arr.insert(44)
arr.insert(55)
arr.insert(22)
arr.insert(88)
arr.insert(11)
arr.insert(00)
arr.insert(66)
arr.insert(33)
searchKey = 55
if( arr.find(searchKey) != arr.size() ):
print 'Found ' , searchKey
else:
print "Can't find " , searchKey

arr.display()
arr.delete(00)
arr.delete(55)
arr.delete(99)
arr.display()

Found  55
0 11 22 33 44 55 66 77 88 99
11 22 33 44 66 77 88


### Example 2.5 Page no : 66¶

In [5]:

class Person:
def __init__(self,last,first,a):
self.lastName = last
self.firstName = first
self.age = a

def displayPerson(self):
print "Last name: " , self.lastName ,", First name: " , self.firstName ,
print ", Age: " , self.age

def getLast(self):
return self.lastName

class ClassDataArray:
def __init__(self,m):
self.a = []
self.nElems = 0

def find(self,searchName):
f = False
for j in range(self.nElems):
if( self.a[j].getLast() ==searchName ) :
f = True
break
if(not f):
return None
else:
return self.a[j]

def insert(self,last,first,age):
self.a.append(Person(last, first, age))
self.nElems += 1

def delete(self,searchName):
f = False
for j in range(self.nElems):
if( self.a[j].getLast() == searchName) :
self.a.remove(self.a[j])
f = True
self.nElems -= 1
break
if(not f):
return False
else:
return True

def displayA(self):
for j in range(self.nElems):
self.a[j].displayPerson()

maxSize = 100
arr = ClassDataArray(maxSize)
arr.insert("Evans", "Patty", 24)
arr.insert("Smith", "Lorraine", 37)
arr.insert("Yee", "Tom", 43)
arr.insert("Hashimoto", "Sato", 21)
arr.insert("Stimson", "Henry", 29)
arr.insert("Velasquez", "Jose", 72)
arr.insert("Lamarque", "Henry", 54)
arr.insert("Vang", "Minh", 22)
arr.insert("Creswell", "Lucinda", 18)
arr.displayA()
searchKey = "Stimson"
found=arr.find(searchKey)
if(found != None ):
print "Found " ,
found.displayPerson()
else:
print "Can't find " , searchKey
print "Deleting Smith, Yee, and Creswell"
arr.delete("Smith")
arr.delete("Yee")
arr.delete("Creswell")
arr.displayA()

Last name:  Evans , First name:  Patty , Age:  24
Last name:  Smith , First name:  Lorraine , Age:  37
Last name:  Yee , First name:  Tom , Age:  43
Last name:  Adams , First name:  Henry , Age:  63
Last name:  Hashimoto , First name:  Sato , Age:  21
Last name:  Stimson , First name:  Henry , Age:  29
Last name:  Velasquez , First name:  Jose , Age:  72
Last name:  Lamarque , First name:  Henry , Age:  54
Last name:  Vang , First name:  Minh , Age:  22
Last name:  Creswell , First name:  Lucinda , Age:  18
Found  Last name:  Stimson , First name:  Henry , Age:  29
Deleting Smith, Yee, and Creswell
Last name:  Evans , First name:  Patty , Age:  24
Last name:  Adams , First name:  Henry , Age:  63
Last name:  Hashimoto , First name:  Sato , Age:  21
Last name:  Stimson , First name:  Henry , Age:  29
Last name:  Velasquez , First name:  Jose , Age:  72
Last name:  Lamarque , First name:  Henry , Age:  54
Last name:  Vang , First name:  Minh , Age:  22

In [ ]: