• No se han encontrado resultados

Heterogenous analysis

In document Essays on the Political (página 109-113)

3.5 Results

3.5.4 Heterogenous analysis

A select set of keywords helps to test conditions and steer program. Their usage is brought out through a set of examples here.

Example 3.3 Output the sum of squares of thefirst eight odd integers.

The segment [1]–[4] in the python Interpreter sequence in Fig. 3.9computes the desired sum—sum of squares of the eight odd integers starting with 1—and outputs the same (=680). Here n is a counter—initialized to 8 and counting is done downwards untiln is zero. The loop starting with ‘while’ is executed as long as n ≠ 0; ‘while’ is a keyword here. n = 0 is interpreted as ‘False’ and causes termination of the loop execution. The flow chart for the program is shown in Fig.3.10; it can be seen to be similar to the flowchart in Fig.3.8a as far as functional blocks and program flow are concerned. In the print statement [3] the function repr(sm) converts sm to a printable string. It is concatenated with the string‘the required sum is’ and the combination output in a convenient form. [4] is possibly a simpler print version. The print function outputs the string and sn directly.

‘True’ and ‘False’ are keywords; they are the Boolean values equivalent to 1 and 0 respectively. Their use is illustrated through the following example.

Example 3.4 Identify thefirst seven positive integers and output the sum of their cubes.

The routine [5]–[8] in the sequence in Fig.3.9 obtains the desired sum and outputs the same. As in the previous programn counts down from 8. a is assigned the‘True’ value initially. The loop execution continues as long as the status of a

(a)

start

end

Output j l1, l2, n = 100, 200, 13 j = l1

Increment j j < l2 yes

no

j % n no yes

(b)

start

end Output Nr & sum Sum, Nr = 0, 12

i = Nr

Increment sum Decrement i i i 0

i = 0

Fig. 3.8 Flowcharts for a Example3.1and b Example3.2

26 3 Simple Programs

remains True. It is changed to False in the loop whenn becomes zero [7]. [7]

illustrates the use of keyword‘if’. Like ‘while’, the ‘if’ statement checks for a condition; on the condition being satisfied, the statement (group of statements) following is (are) executed. The condition being tested is whethern == 0. If the

start

end Output sm N, m, sm = 8, 1, 0

sm = sm + m*m m = m+ 2 Decrement n

n n + 0

n = 0 Fig. 3.10 Flowchart for

Example3.3(Interpreter sequence [1]–[4] in Fig.3.9)

>>> n, m, sm = 8, 1, 0 [1]

>>> while n: #Add squares of the first 8 [2]

... sm += m*m # odd numbers ... m += 2 #Print out the sum ... n -= 1

...

>>> print('The required sum is ' + repr(sm)) [3]

The required sum is 680

>>> print('The required sum is ', sm) [4]

The required sum is 680

>>> a, n, m, sm = True, 8, 7, 0 [5]

>>> while a: #Get the sum of the cubes [6]

... sm += m*m*m #of the first 8 positive ... n -= 1 #integers divisible by 7 ... m += 7

... if n == 0:a = False [7]

...

>>> print('The required sum is ' + repr(sm)) [8]

The required sum is 444528

Fig. 3.9 Python Interpreter sequence for Examples3.3and3.4

condition is true—that is n is zero—the statement specified is executed (a is assigned the value False). In turn execution terminates here. The sum (444,528) is output following line [8]. The flowchart is similar to that in Fig.3.10 and is not shown separately.

The Python Interpreter sequence in Fig.3.11 illustrates use of some additional features basic to Python programs, again through examples.

Example 3.5 What is the total number of positive integers below 200 which are divisible by 11?

The routine [1]–[4] in Fig.3.11obtains this number. There are altogether 18 of these numbers as can be seen from the line following [4]. The program is also an illustration for the use of keyword‘is’.

>>> a = True [1]

>>> n = sm = 0 #Total number of positive integers

>>> while a is True:# below 200 divisible by 11 [2]

... sm += 1 ... n += 11

... if n >200: a = False [3]

...

>>> print("The required sum is " + repr(sm-1)) [4]

The required sum is 18

>>> b, sn = 100, 0 #numbers between 100 and 1000 which are [5]

>>> while b < 1000: # divisible by 11 as well as 13 [6]

... if (b% 11 == 0) and (b% 13 == 0): [7]

... sn += 1

... print('sn = '+ repr(sn) + ', b =' + repr(b) + ';

') [8]

... b += 1 ...

sn = 1, b =143;

sn = 2, b =286;

sn = 3, b =429;

sn = 4, b =572;

sn = 5, b =715;

sn = 6, b =858;

>>> n = 29 # What is the smallest number greater [9]

>>> while True:# than 10,000 which is a power of 29? [10]

... n *=29

... if n > 10000:break [11]

...

>>> print('The desired number is ' + repr(n)) The desired number is 24389

>>>

Fig. 3.11 Python Interpreter sequence for Examples3.5,3.6, and3.7

28 3 Simple Programs

Here the condition‘a’ being true is being tested in [2] with the use of ‘is’. The block of three executable statements up to [3] is executed as long asa is True. As soon asa = False the loop execution stops. The operation ‘is not’ can be used similarly.‘a is not b’ has value ‘True’ as long as a and b are not identical.

Example 3.6 Identify all the numbers between 100 and 1000 which are divisible by 11 and 13 and output them.

The program sequence [5]–[8] in Fig. 3.11identifies and outputs these numbers.

The condition that the number represented byb is divisible by 11 (b%11 == 0) as well as by 13 (b%13 == 0) is tested using the single condition in [7]. It uses the logical operator ‘and’—a keyword; p and q is True only if p is True and simultaneouslyq is also True. The logical operator ‘or’ can be used in a similar manner.p or q is True if either p is True or q is True.

Example 3.7 Identify the smallest number greater than 10000 which is a power of 29.

The routine follows from [9] in Fig.3.11. Starting with 29 we take its successive powers. The process is continued without break until the number crosses the value 10000. Once this value is crossed execution breaks out of the loop as specified by [11].‘break’—a keyword—exits from the current loop on the specified condition being true.‘while True’ is always true: hence in the absence of the conditional break statement within, the loop execution will continue ad infinitum. Incidentally the desired number here is 24,389(=293).

Example 3.8 Obtain the sum of the cubes of all positive integers in the range [0, 10]

which have 3 as a factor.

The segment [1]–[4] in the Python Interpreter sequence in Fig.3.12 is the rel-evant program. This simple example also illustrates the use of the ‘range()’

function.

The scope of the range function is clarified in Fig.3.13. range() specifies a sequence of integers—often used to carry out an iteration as done here. The first integer signifies the start for the range and the second one its termination. The first can be absent in which case the default value is taken as zero. The third stands for the interval for the range; if absent the default interval value is taken as unity. All the three integers can be positive or negative. The range specification should be realizable.

range(5), range(0, 5), range (0, 5, 1) all these specify the range {0, 1, 2, 3, 4}.

range(−2, 3, 1) and range (−2, 3) specify the same range {−2, −1, 0, 1, 2}

range(10, −5, −2) implies the range {10, 8, 6, 4, 2, 0, −2, −4}.

range(2, 4,−1) is erroneous.

In the context here the suite of executable statements—in fact the single one in [2]: sm += i3is executed over a specified range—namely 0–10 with an interval of 3. The routine outputs the sum 33+ 63+ 93 as can be seen from [3]. [4] also achieves the same output. Both are shown here to bring out the fact that a string can be specified through ‘… ’ or “…’’.

>>> sm = 0 # Sum up the cubes of all positive integers [1]

>>> for i in range(0, 10, 3): sm += i**3 #up to10,divisible

by3 [2]

... >>> print("the sum - 3**3 + 6**3 + 9**3 = ", sm) [3]

the sum - 3**3 + 6**3 + 9**3 = 972

>>> print('the sum - 3**3 + 6**3 + 9**3 = ', sm) [4]

the sum - 3**3 + 6**3 + 9**3 = 972

>>> for n in range(2,10): #Identify all even & odd numbers [5]

... if n%2 == 0: [6]

... print(n, " : an even number", )

... continue [7]

... print(n, " : an Odd number", ) [8]

...

2 : an even number 3 : an Odd number 4 : an even number 5 : an Odd number 6 : an even number 7 : an Odd number 8 : an even number 9 : an Odd number

>>> for n in range(2,10): [9]

... if n%2 == 0:

... print(n, " : an even number", ) ... print(n, " : a number", )

...

2 : an even number 2 : a number 3 : a number 4 : an even number 4 : a number 5 : a number 6 : an even number 6 : a number 7 : a number 8 : an even number 8 : a number

Fig. 3.12 Python Interpreter sequence for Examples3.8and3.9

range( [a], b, [c]) Optional: specifies start of range; if absent, the default value is 0

Essential: specifies termination of range Optional: incrementing interval for the range; if absent, the default interval is 1

Fig. 3.13 Structure and scope of range() in Python: Note that a, b, and c should be integers or functions which return an integer

30 3 Simple Programs

Example 3.9 In the range [2, 10] identify the integers as‘odd’ or ‘even’ and output accordingly.

The sequence [5]–[8] in Fig. 3.12executes the desired task. It also illustrates the use of the keyword‘continue’. The outer loop is executed for all m in the range 2–10. If (n%2 == 0)—that is n is an even number—the value is output as an ‘even number’. Then the routine continues with the loop—ignoring the sequence fol-lowing. This is implied by‘continue’. Of course if n is odd, (n%2 == 0) is not satisfied and the loop execution continues with the rest of the executable lines. Here the number concerned is output as an‘odd number’. If the ‘continue’ is replaced by‘break’ the loop will terminate after the first execution following satisfaction of condition (n% == 0)—after the first even number (=2) is output. The program has been repeated as a sequence following [9] with‘continue’ being absent. Here the even numbers are output with an‘even number’ tag. Apart from this they are also output with tag ‘number’ since line [8] is also executed here. The program is included here to clarify the role of the keyword—‘continue’.

In document Essays on the Political (página 109-113)