Chapter 5: Arrays and Pointers

Example 5.1, page no. 137

In [1]:
'''
python does not have pointer functionality.We can get it explicitly.
'''

class ref:
    def __init__(self, obj): self.obj = obj
    def get(self):    return str(self.obj)
    def set(self, obj):      self.obj = obj


x=ref([0])
p=x # assigning variable value to p

p.set(0) # set x to zero */
print "\nx is " + x.get()
print "\np is " + p.get()
a = p.get()
p.set(int(a)+1) # increment what p points to */
print "\nx is " +x.get()
a = p.get()
p.set(int(a)+1) # increment what p points to */
print "\nx is " + x.get()
x is 0

p is 0

x is 1

x is 2

Example 5.2, page no. 139

In [1]:
def date(a,b): # /* declare the function */
    day_ret =0
    month_ret = 0
    ''' At this point, calculate the day and month
    * values in day_ret and month_ret respectively.
    '''
    a = day_ret;
    b = month_ret;
    return a,b




month = 5
day = 1
d,m = date(day,month);
print "day is %d, month is %d\n" %(d, m)
day is 0, month is 0

Example 5.3, page no. 142

In [2]:
from array import array

ARSZ=20
ar=[]

for i in range(ARSZ + 1):
    ar.append( i)
    print "ar[%d] now = %d\n" %( i, ar[i])
ar[0] now = 0

ar[1] now = 1

ar[2] now = 2

ar[3] now = 3

ar[4] now = 4

ar[5] now = 5

ar[6] now = 6

ar[7] now = 7

ar[8] now = 8

ar[9] now = 9

ar[10] now = 10

ar[11] now = 11

ar[12] now = 12

ar[13] now = 13

ar[14] now = 14

ar[15] now = 15

ar[16] now = 16

ar[17] now = 17

ar[18] now = 18

ar[19] now = 19

ar[20] now = 20

Example 5.4 & 5.5, page no. 146

In [4]:
ARSZ = 10

fp1= ARSZ
fp2=0


while(fp2 != fp1):
    print "Difference: %d\n" %((ARSZ-fp1))
    fp1 -= 1
Difference: 0

Difference: 1

Difference: 2

Difference: 3

Difference: 4

Difference: 5

Difference: 6

Difference: 7

Difference: 8

Difference: 9

Example 5.6, page no. 149

In [3]:
s = raw_input("Enter String:")
print len(s)
Enter String:python 
7

Example 5.7, page no. 150

In [4]:
def str_eq(s1,s2):
    if s1==s2:
        return True
    return False

print str_eq("this","hello")
print str_eq("this","this")
False
True

Example 5.8, page no. 151

In [5]:
cp = "a string";
for i in cp:
 print i,

print "\n"
for i in range(len(cp)):
 print cp[i],
a   s t r i n g 

a   s t r i n g

Example 5.9, page no. 152

In [4]:
import numpy

ARLEN=10
ar  =  numpy.zeros(ARLEN)

print ar
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

Example 5.10, page no. 158

In [1]:
'''
Python does not have any concept of pointer.
Python will go same for all mentioned.
'''


MAXSTRING=50 # /* max no. of strings */
MAXLEN = 80 #/* max length. of strings */

strings = [] # list of strings

def print_arr(a):
    print a


def sort_arr(a):
      a.sort()
      return a


def next_string():
    s = raw_input("Enter Next String : ")
    strings.append(s)
    return s

nstrings = 0

while(nstrings < MAXSTRING and next_string() != '0'):
    nstrings += 1

print_arr(strings)
print sort_arr(strings)
Enter Next String : abc
Enter Next String : def
Enter Next String : fed
Enter Next String : 0
['abc', 'def', 'fed', '0']
['0', 'abc', 'def', 'fed']

Example 5.13, page no. 165

In [2]:
'''
We need not to worry about memory allocation and deallocation in python.
It is simple to get a string, print and copy it.
'''


str_p = ""
tmp_p = ""

str_p = raw_input("Enter String.. : ")
next_p = str_p
chars_read = len(str_p)


tmp_p = str_p

print tmp_p


print str_p
Enter String.. : hello
hello
hello

Example 5.14, page no. 165

In [9]:
arr = "hello"
a = arr
print "Size of arr %d\n" % len(arr)
print "Size of a %d\n" % len(a)
Size of arr 5

Size of a 5

Example 5.16, page no. 167

In [10]:
def func(arg):
	print "%d\n" %(arg)

func(5)
5