Chapter 4 : Arrays

Example 4.1, Page No 79

In [2]:
#Declarations

iNoOfDays = 31
dSum = 0
dblTempApr=[[0 for col in range(31)] for row in range(31)]


#Entry and calculation

for i in range(1,iNoOfDays):
    dblTempApr[i] = raw_input("Temperature Day %d: " % i)
    dSum += dblTempApr[i]

dAvg = dSum / iNoOfDays

#Printout

print "Average temperature: %f" % dAvg
print "Temperatures exceeding average: "

for i in range(1,iNoOfDays):
    if (dblTempApr[i] > dAvg):
        print "Day no.: %d" % i,
        print " temp: %d" % dblTempApr[i]
Temperature Day 1: 32
Temperature Day 2: 33
Temperature Day 3: 35
Temperature Day 4: 36
Temperature Day 5: 34
Temperature Day 6: 32
Temperature Day 7: 31
Temperature Day 8: 29
Temperature Day 9: 28
Temperature Day 10: 27
Temperature Day 11: 25
Temperature Day 12: 26
Temperature Day 13: 35
Temperature Day 14: 36
Temperature Day 15: 35
Temperature Day 16: 50
Temperature Day 17: 40
Temperature Day 18: 42
Temperature Day 19: 44
Temperature Day 20: 43
Temperature Day 21: 41
Temperature Day 22: 38
Temperature Day 23: 37
Temperature Day 24: 3
Temperature Day 25: 36
Temperature Day 26: 35
Temperature Day 27: 45
Temperature Day 28: 32
Temperature Day 29: 21
Temperature Day 30: 15
Average temperature: 32.000000
Temperatures exceeding average: 
Day no.: 2  temp: 33
Day no.: 3  temp: 35
Day no.: 4  temp: 36
Day no.: 5  temp: 34
Day no.: 13  temp: 35
Day no.: 14  temp: 36
Day no.: 15  temp: 35
Day no.: 16  temp: 50
Day no.: 17  temp: 40
Day no.: 18  temp: 42
Day no.: 19  temp: 44
Day no.: 20  temp: 43
Day no.: 21  temp: 41
Day no.: 22  temp: 38
Day no.: 23  temp: 37
Day no.: 25  temp: 36
Day no.: 26  temp: 35
Day no.: 27  temp: 45

Example 4.2, Page No 83

In [1]:
#Declaration

iMaxNo = 100
dLimit = 5000
perc1 = 0.1
perc2 = 0.15
sales = [[0 for col in range(1)] for row in range(iMaxNo)]

#Initialize array

for i in range(0,iMaxNo):
    sales[i] = 0

#Enter Salesman info
nr=input("Enter Salesman no. : ")
dAmount=input("Enter sales amount: ")

while (nr and dAmount):
    if(nr<1 or nr>iMaxNo or dAmount<0):
        print "Input error"
        break;
    else:
        sales[nr-1]+= dAmount
    nr=int(raw_input("Enter Salesman no. : "))
    dAmount=float(raw_input("Enter sales amount: "))

#Print summary

print "Number        Amount        Fee"
print "======        ======        ==="

for i in range(0,iMaxNo):
    if (sales[i] > 0):
        if(sales[i] <= dLimit):
            dFee = perc1 * sales[i]
        else:
            dFee = perc1 * dLimit + perc2*(sales[i] - dLimit)
        j=i;
        j=j+1;
        print "%4d" % j,
        print "%13d" % sales[i],
        print "%10.0f" % dFee
Enter Salesman no. : 1
Enter sales amount: 1000
Enter Salesman no. : 2
Enter sales amount: 2000
Enter Salesman no. : 3
Enter sales amount: 3000
Enter Salesman no. : 4
Enter sales amount: 4000
Enter Salesman no. : 5
Enter sales amount: 5000
Enter Salesman no. : 6
Enter sales amount: 00
Number        Amount        Fee
======        ======        ===
   1          1000        100
   2          2000        200
   3          3000        300
   4          4000        400
   5          5000        500

Example 4.3, Page No 92

In [ ]:
#Declaration
l=0
r=30
iFound=0
# Array is in sorted form
iProdid= [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]

iSrch = raw_input("Enter the searched product id: ")

if(iSrch == iProdid[0]):
    iPos = 0
    iFound= 1

if (iSrch == iProdid[30]):
    iPos = 30
    iFound = 1

while not (iFound):
    iMid = (int)(l + ((r-l)/2))
    if(iSrch == iProdid[iMid]):
        iFound = 1
        iPos = iMid

    if(iSrch > iProdid[iMid]):
        l=iMid
    else:
        r=iMid
Enter the searched product id: 15
In [ ]: