Chapter 15: Debugging and optimization

Example 15.1, Page number: 275

In [2]:
# Function declaration
def lookup (name) :
   lists = ['John', 'Jim', 'Jane', 'Clyde']
   result = 0

   for index in range (0, 4) :
      if (lists[index] == name) :
         result = 1
         break
   
   return result

# Calculation and result
name = 'John'

if (lookup (name)) :
   print ('%s is in the list\n' % name)
else :
   print ('%s is not in the list\n' % name)
John is in the list

Example 15.6, Page number: 288

In [4]:
# Variable and function declaration
seven_count = 0
three_count = 0
data = []

def get_data (data) :
   for i in range (0, 5) :
      x = 3
      data.append(int(x))
   print (data)

# Calculation
get_data (data)
for index in range (0, 5) :
   if data[index] == 3 :
      three_count += 1

   if data[index] == 7 :
      seven_count += 1

# Result
print ('Threes %d Sevens %d' % (three_count, seven_count))
[3, 3, 3, 3, 3]
Threes 5 Sevens 0

Example 15.7, Page number: 292

In [*]:
# Variable declaration
import sys
import os
in_file = open ('numbers.dat', 'r')

data = []
max_count = 0
 
# Calculation and result
if not os.path.exists ('numbers.dat') :
   print ('Error:Unable to open numbers.dat\n')
   sys.exit(1)

for line in in_file :
   data.append(line)
   max_count += 1

while (1) :
   search = 6
   
   if (search == -1) :
      break

   low = 0
   high = max_count

   while (1) :
      mid = (low + high) / 2
      middle = int (mid)      

      if (int (data[middle]) == search) :
         print ('Found at index %d\n' % (middle + 1))
         break

      if (low == high) :
         print ('Not found\n')
         break

      if (int (data[middle]) < search) :
         low = middle
      else :
         high = middle      

in_file.close()

Example 15.8, Page number: 300

In [*]:
# Variable declaration
import sys
import os
in_file = open ('numbers.dat', 'r')

data = []
max_count = 0
 
# Calculation and result
if not os.path.exists ('numbers.dat') :
   print ('Error:Unable to open numbers.dat\n')
   sys.exit(1)

for line in in_file :
   data.append(line)
   max_count += 1

while (1) :
   search = 14
   
   if (search == -1) :
      break

   low = 0
   high = max_count

   while (1) :
      mid = (low + high) / 2
      middle = int (mid)      

      if (int (data[middle]) == search) :
         print ('Found at index %d\n' % (middle + 1))
         break

      if (low == high) :
         print ('Not found\n')
         break

      if (int (data[middle]) < search) :
         low = middle
      else :
         high = middle      

in_file.close()

Example 15.10, Page number: 304

In [9]:
# Variable declaration
i = 1
j = 1

# Calculation and result
print ('Starting\n')
print ('Before divide...')
i = i / j
print ('After\n')
Starting

Before divide...
After

Example 15.11, Page number: 304

In [8]:
from __future__ import division
# Variable declaration
import sys
i = 1
j = 1

# Calculation and result
print ('Starting\n')
sys.stdout.flush()
print ('Before divide...')
sys.stdout.flush()
i = i / j
print ('After\n')
sys.stdout.flush()
Starting

Before divide...
After

In [ ]: