# Chapter 6: Structured Data Types¶

## Example 6.1, page no. 173¶

In [1]:
'''
Example 6.1 and example 6.2 are same for python.
'''

class wp_char:
wp_cval = ''
wp_font = 0
wp_psize = 0

def __init__(self):
wp_cval = ''
wp_font = 0
wp_psize = 0

def infun():
w = wp_char()
#w.wp_cval = raw_input("Enter any Character.. :")
w.wp_font = 2
w.wp_psize = 10
return w

ARSIZE = 10
icount=0
lo_indx=0
hi_indx=0
ar = []
for icount in range(ARSIZE):
ar.append(infun())
if(ar[icount].wp_cval == '\n'):
''' /*
* Leave the loop.
* not incrementing icount means that the
* '\n' is ignored in the sort
*/
'''
break;

#/* now a simple exchange sort */
for lo_indx in range(icount-1):
for hi_indx in range(icount):
if (ar[lo_indx].wp_cval > ar[hi_indx].wp_cval):
#Swap the two structures.
wp_tmp = ar[lo_indx]
ar[lo_indx] = ar[hi_indx]
ar[hi_indx] = wp_tmp

for lo_indx in range(icount+1):
print "%s %d %d\n" % (ar[lo_indx].wp_cval,ar[lo_indx].wp_font,ar[lo_indx].wp_psize)

 2 10

2 10

2 10

2 10

2 10

2 10

2 10

2 10

2 10

2 10



## Example 6.3, page no. 183¶

In [4]:
'''
This wont give any output.

Note: while compiling this program. comment either of the s_1 class structure.
'''

class s_1: #incomplete type
pass

class s_2:
something = 0
s = s_1()

class s_1:
something = 0.0
s =  s_2()


## Example 6.4, page no. 183¶

In [5]:
'''
This wont give any output.
'''

class x:
f = 0.0

def func():
a = x()
a.f = 0
return a

def f2():
o = func()


## Example 6.5, page no. 185¶

In [1]:
'''
this wont give any output.
'''

class list_ele:
def __init__(self,d=0,p=None):
data=d
pointer = p
ar=[]

ar.append(list_ele())
ar.append(list_ele())
ar.append(list_ele())
ar[0].data = 5;
ar[0].pointer = ar[2]
ar[1].data = 99
ar[1].pointer = ar[2]
ar[2].data = -7
ar[2].pointer = None


## Example 6.6, page no. 185¶

In [2]:
class list_ele:
def __init__(self,d=0,p=None):
data=d
pointer = p
ar=[]

ar.append(list_ele())
ar.append(list_ele())
ar.append(list_ele())
ar[0].data = 5;
ar[0].pointer = ar[2]
ar[1].data = 99
ar[1].pointer = ar[2]
ar[2].data = -7
ar[2].pointer = None

for i in ar:
print "contents %d\n" %(i.data)

contents 5

contents 99

contents -7



## Example 6.7, page no. 187¶

In [2]:
'''
This wont compile as we do not have list_ele class here.
'''

def sortfun(list ):
exchange = True
nextp = []
thisp = None
dummy = list_ele()
'''
* Algorithm is this:
* Repeatedly scan list.
* If two list items are out of order,
* link them in the other way round.
* Stop if a full pass is made and no
* exchanges are required.
* The whole business is confused by
* working one element behind the
* first one of interest.
* This is because of the simple mechanics of
*/ '''
dummy.pointer = list;
while(exchange):
exchange = False
thisp=dummy
while( (nextp == thisp.pointer) and nextp.pointer):
if(nextp.data < nextp.pointer.data):
#/* exchange */
exchange = 1
thisp.pointer = nextp.pointer
nextp.pointer =thisp.pointer.pointer
thisp.pointer.pointer = nextp

thisp = thisp.pointer;

return(dummy.pointer);


## Example 6.8, page no. 190¶

In [3]:
'''
Examples 6.8 and Example 6.9
Both are same for Python as we do not have any concept of pointer.
Python will go same for both.
'''

class Tree:
def __init__(self, cargo, left=None, right=None):
self.cargo = cargo
self.left  = left
self.right = right

def __str__(self):
return str(self.cargo)
'''
/*
* Tree search algorithm.
* Searches for value 'v' in tree,
* returns pointer to first node found containing
* the value otherwise 0.
*/

'''
def t_search(root,v):
while(root):
if(root.cargo == v):
return(root)
if(root.cargo > v):
root = root.left
else:
root = root.right

#/* construct tree by hand */
tp = None
root_p = None
tree=[]
for i in range(7):
j = 0
j = i+1
tree.append(Tree(j))

if(j == 2 or j == 6):
tree[i].left = tree[i-1]
tree[i-1].right = tree[i]

root_p = tree[3]
root_p.left = tree[1]
root_p.right = tree[5]
# /* try the search */
tp = t_search(root_p, 9)
if(tp!=None):
print "found at position %d\n" %(tp-tree)
else:

value not found



## Example 6.10, page no. 192¶

In [4]:
'''
Note : this wont give any output.
'''

def t_walk(r):
if(root_p == 0):
return
t_walk(root_p.left)
print "%d\n" %(root_p.data)
t_walk(root_p.right)


## Example 6.11, page no. 193¶

In [5]:
class A:
def __init__(self):
u_f = 0.0
u_i = 0

var = A()
var.u_f = 23.5
print "value is %f\n" %(var.u_f)
var.u_i = 5
print "value is %d\n" %(var.u_i)

value is 23.500000

value is 5



## Example 6.12, page no. 194¶

In [6]:
FLOAT_TYPE = 1
CHAR_TYPE = 2
INT_TYPE = 3
class var_type:
def __init__(self):
type_in_union = 0
un_float = 0.0
un_char = '0'
un_int = 0

v = var_type()
def print_vt():
if(v.type_in_union == FLOAT_TYPE):
print "%f\n" %(v.un_float)
else:
if(v.type_in_union == CHAR_TYPE):
print "%s\n" %(v.un_char)
else:
if(v.type_in_union == INT_TYPE):
print "%d\n" %(v.un_int)
else:
print "Unknown type in union\n"

v.type_in_union = FLOAT_TYPE
v.un_float = 3.5
print_vt()
v.type_in_union = CHAR_TYPE
v.un_char = 'a'
print_vt()

3.500000

a



## Example 6.13, page no. 195¶

In [7]:
'''
This wont give any output.
'''

class Abc:
field1 = 4
a = 3
field2 = 1
igned = 0
field3 = 6


## Example 6.14, page no. 200¶

In [8]:
NMONTHS=12
month = 0
month_days =[31,28,31,30,31,30,31,31,30,31,30,31]
mnames=["January", "February","March", "April","May", "June","July", "August","September", "October","November", "December"]

day_count = month;
for day_count in range(NMONTHS):
print "%d days in %s\n" %(month_days[day_count],mnames[day_count])

31 days in January

28 days in February

31 days in March

30 days in April

31 days in May

30 days in June

31 days in July

31 days in August

30 days in September

31 days in October

30 days in November

31 days in December



## Example 6.15, page no. 201¶

In [9]:
class s:
a = 0
b = '0'
cp = '0'
def set(self,aa,bb,c):
a = aa
b = bb
cp = c
ex_s = s()
ex_s.set(1, 'a', "hello")

first = ex_s
second = s()
second.set(2, 'b', "byebye")

print first

print second

<__main__.s instance at 0x7f29180251b8>
<__main__.s instance at 0x7f29180252d8>


## Example 6.16, page no. 202¶

In [10]:
class s:
a = 0
class ss:
c = 0
d = '0'

e = s()
e.a = 1
e.c = 2
e.d ='a'

print e

<__main__.s instance at 0x7f29180253b0>


## Example 6.18, page no. 202¶

In [11]:
y = [[1, 3, 5],[2, 4, 6],[3, 5, 7]]

print y

[[1, 3, 5], [2, 4, 6], [3, 5, 7]]