Chapter 7: Working with arrays

Program 7.1, Page number: 99

In [1]:
values=[0]*10

values[0]=197
values[2]=-100
values[5]=350
values[3]=values[0]+values[5]
values[9]=values[5]/10
values[2]-=1
i=0

for index in values:
   print("values[{0}] = {1}".format(i,index))
   i=i+1
values[0] = 197
values[1] = 0
values[2] = -101
values[3] = 547
values[4] = 0
values[5] = 350
values[6] = 0
values[7] = 0
values[8] = 0
values[9] = 35

Program 7.2, Page number: 101

In [1]:
ratingCounters=[0]*11

for i in range(1,11):
        ratingCounters[i]=0

print("Enter your Responses:")

for i in range (1,21):
        response=5                                   #score=5 for all iterations
        #response=input()

        if(response<1 or response>10):
             print("Bad response: {0}".format(response))
        else:
           ratingCounters[response]+=1

print("\n\n Rating |  Number of Responses\n")
print(" -----   -------------------")

for i in range(1,11):
        print("  {0}              {1}".format(i,ratingCounters[i]))
Enter your Responses:


 Rating |  Number of Responses

 -----   -------------------
  1              0
  2              0
  3              0
  4              0
  5              20
  6              0
  7              0
  8              0
  9              0
  10              0

Program 7.3, Page number: 103

In [2]:
Fibonacci=[0]*15

Fibonacci[0]=0
Fibonacci[1]=1

for i in range(2,15):
        Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1]  #Calculation

for i in range(15):
        print("{0}".format(Fibonacci[i]))           #Print Result
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377

Program 7.4, Page number: 105

In [3]:
primeIndex=2
primes=[0]*50

primes[0]=2
primes[1]=3
p=5

while(p<=50):                                        #Outer Loop-1
        isPrime=True
        
        i=1
        while(isPrime and p/primes[i]>=primes[i]):   #Inner Loop
            if(p%primes[i]==0):
               isPrime=False
            i+=1
         

        if(isPrime==True):
           primes[primeIndex]=p
           primeIndex+=1
        p=p+2

for i in range (primeIndex):                         #Outer Loop-2
        print("{0}".format(primes[i]))
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47

Program 7.5, Page number: 107

In [4]:
array_values=[0]*10
array_values[0:4]=[0,1,4,9,16]

for i in range(5,10):
        array_values[i]=i*i

for i in range(10):
        print("array values[{0}] = {1}".format(i,array_values[i]))
array values[0] = 0
array values[1] = 1
array values[2] = 4
array values[3] = 9
array values[4] = 16
array values[5] = 25
array values[6] = 36
array values[7] = 49
array values[8] = 64
array values[9] = 81

Program 7.6, Page number: 108

In [5]:
import sys           #For sys.stdout.write()

word=['H','e','l','l','o','!']

for i in word:
    sys.stdout.write("{0} ".format(i))
print("\n")
H e l l o ! 

Program 7.7, Page number: 110

In [6]:
import sys                                    #for sys.stdout.write()

baseDigits=['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F']
convertedNumber=[0]*10
index=0

numberToConvert=234                             #numberToConvert=input("Number to be converted?: ")
base=2                                          #base=input("Base?: ")

while True:
     convertedNumber[index]=numberToConvert%base
     index+=1
     numberToConvert=numberToConvert/base
     if(numberToConvert==0):
        break

print("Converted Number= ")
index-=1
while(index>=0):
        nextDigit=convertedNumber[index]
        sys.stdout.write("{0} ".format(baseDigits[nextDigit]))
        index-=1

print("\n")
Converted Number= 
1 1 1 0 1 0 1 0 

Program 7.8, Page number: 116

In [7]:
import sys                               #For sys.exit()     
 
numFibs=18                               #numFibs=input("How many Fibonacci numbers do you want (between 1 and 75)? ")

if(numFibs<1 or numFibs>75):
        print("bad number,sorry!")
        sys.exit()

Fibonacci=[0]*numFibs
Fibonacci[0]=0
Fibonacci[1]=1

for i in range(2,numFibs):
    Fibonacci[i]=Fibonacci[i-2]+Fibonacci[i-1] 
            
for i in range(numFibs):
    print("{0}".format(Fibonacci[i]))
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597