0% found this document useful (0 votes)
2 views44 pages

L.6 - Python & Iterative Algorithms

Uploaded by

Salma Wesam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views44 pages

L.6 - Python & Iterative Algorithms

Uploaded by

Salma Wesam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Taught by: Hisham Othman

Slides are based on: Prof. Dr. Slim Abdennadher’s slides

German University Cairo, Department of Media Engineering and Technology

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Recall: Objectives
 We plan to learn:

 Sequential algorithms 

 Conditional algorithms 

 Iterative algorithms 

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Iterative operations
 Repeat a set of steps over and over – also called a looping
operation

 Notation
 Use the same primitives as before plus the following:

 while
 <operation>
 <operation>
 ...
 <operation>
 <operation>

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Execution
 1- Check, the

 2- If condition is true,
execute steps inside the
loop body.
 Then, Check the
.

 3- If condition is false
continue the execution
after the while loop.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
..
..

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
while
Q: What is the difference
<operation>
between “while” and “if”?
<operation>
...
<operation>
<operation>

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
while
Q: What is the difference
<operation>
between “while” and “if”?
<operation>
...
A: “if” checks its
<operation>
condition only once.
<operation>
“while” can check its
condition repeatedly.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 1:
 Given a natural number n, compute the sum of
numbers from 1 to n. n=5

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 1:
 Given a natural number n, compute the sum of
numbers from 1 to n. n=5
 Algorithm:
 n = eval(input())
 i=1
 result = 0
 while i <= n:
 result = ( result + i )

 i = (i+1)

 print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 1:
 Given a natural number n, compute the sum of
numbers from 1 to n. n=5
 Algorithm: i Result
 n = eval(input()) 1 0
 i=1 2 1
 result = 0 3 3

 while i <= n: 4 6

 result = ( result + i ) 5 10
Trace
 i = (i+1) 15

 print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
..
..

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
n = eval(input())
Q: What would happen if
i=1
push the last print
result = 0
statement under the
while i <= n:
shadow of “while”?
result = ( result + i )
i = (i+1)
A: This will print the
print(result)
intermediate values of
the variable “result”, as
follows:
1
3
6
10
15
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
n = eval(input())
Q: What would happen if
i=1
use “<“ instead of “<=“?
result = 0
while i < n:
.
result = ( result + i )
i = (i+1)
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
n = eval(input())
Q: What would happen if
i=1
use “<“ instead of “<=“?
result = 0
while i < n:
A: this will end the loop
result = ( result + i )
one-round short.
i = (i+1)
If you enter n=5, the
print(result)
output will be result=10
because you’re gonna
miss the last n.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 2:
 Write an algorithm to perform the average of n values
entered by the user.
 For example:
 INPUT: The user can enter:
 n=3 (that’s the number of values)

 9,8,7 (these are the n values)

 OUTPUT:
 8 (which is (9+8+7)/3 )

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 2:
 Write an algorithm to perform the average of n
numbers entered by the user.
 Algorithm:
 n = eval(input())
 result = 0
 i=1
 while (i <= n):
 num = eval(input())

 result = result + num

 i=i+1

 average = result/n
 print(average)
Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
.
Q: Could we write this
statement in a shorter
way?

result = result + num

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
A: yes, you can:
Q: Could we write this
statement in a shorter
result += num
way?
You can also write:
result = result + num
i += 1

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 3:
 Multiplication of two positive integers N and M via
addition
 Trace for: N = 3, M = 4
 This means:
 Result =3 + 3 + 3 + 3 ….. M-times
 i.e. Result = 12

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 3:
 Multiplication of two positive integers N and M via
addition
 Trace for: N = 3, M = 4
 Algorithm:
 N, M = eval(input()), eval(input())
 result = 0
 while M > 0:
 result = result + N

 M=M-1

 print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 3:
 Multiplication of two positive integers N and M via
addition
 Trace for: N = 3, M = 4
 Algorithm:
 N, M = eval(input()), eval(input()) N M Result
 result = 0 3 4 0
 while M > 0: 3 3 3
 result = result + N
3 2 6
 M=M-1
3 1 9
 print(result) 3 0 12

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
..
..

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: Can we make another N, M = eval(input()), eval(input())
solution counting up to result = 0
M? while M > 0:
result = result + N
M=M-1
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: Can we make another N, M = eval(input()), eval(input())
solution counting up to result = 0
M? i=1
while i <= M :
A: Yes. You can. result = result + N
See: This Alternative i=i+1
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: Why did we use M as a N, M = eval(input()), eval(input())
counter in this case? result = 0
Could we use N? while M > 0:
result = result + N
M=M-1
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: Why did we use M as a N, M = eval(input()), eval(input())
counter in this case? result = 0
Could we use N? while N > 0:
result = result + M
A: Yes. N=N-1
You can use N as a print(result)
counter instead of M.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: What would happen N, M = eval(input()), eval(input())
the user enters a negative result = 0
value of N? while M > 0:
result = result + N
M=M-1
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: What would happen N, M = eval(input()), eval(input())
the user enters a negative result = 0
value of N? while M > 0:
result = result + N
A: With the way the M=M-1
algorithm is written, it is print(result)
going to work fine.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: What would happen N, M = eval(input()), eval(input())
the user enters a negative result = 0
value of M? while M > 0:
result = result + N
M=M-1
print(result)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
Q: What would happen N, M = eval(input()), eval(input())
the user enters a negative result = 0
value of M? while M > 0:
result = result + N
A: With the way the M=M-1
algorithm is written, it is print(result)
going to print 0.

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 4: Compound Interest
 Invest 10000 Euros with 5% interest compounded annually

 Question:
 When will the balance be at least 20000 Euro?

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 4: Compound Interest
 Invest 10000 Euros with 5% interest compounded annually

 Question:
 When will the balance be doubled, i.e. at least 20000 Euro?

Year Balance
0 10000
1 10500
2 11025
3 11576.25
4 12,155.06
… …

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 4: Compound Interest

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 4: Compound Interest
 Algorithm:
 balance = eval(input())
 rate = 5
 targetBalance = 20000
 year = 0
 while (balance < targetBalance):
 year = year + 1

 interest = balance * rate / 100

 balance = balance + interest

 print(year, balance)

 print("The investment doubled after", year, "years")

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 4: Compound Interest
Year Balance
 Algorithm: 1 10500
2 11025
 balance = eval(input()) 3 11576
 rate = 5 4 12155
5 12763
 targetBalance = 20000 6 13401
7 14071
 year = 0 8 14775
9 15513
 while (balance < targetBalance): 10 16289
11 17103
 year = year + 1
12 17959
 interest = balance * rate / 100 13 18856
14 19799
 balance = balance + interest 15 20789
The investment doubled after 15 years
 print(year, balance)

 print("The investment doubled after", year, "years")

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
 Any Question ?
..
..

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Question

Q: Can I calculate the new balance balance = eval(input())


rate = 5
directly?
targetBalance = 20000
year = 0
A: Yes you can: while (balance < targetBalance):
balance *= (1+rate/100) year = year + 1
interest = balance * rate / 100
balance = balance + interest
This means: print(year, round(balance) )
balance = balance * (1+rate/100) print("The investment doubled after", year,
"years")

This means:
balance = balance + (balance * rate/100)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Question

Q: Can I calculate the new balance balance = eval(input())


rate = 5
directly?
targetBalance = 20000
year = 0
while (balance < targetBalance):
year = year + 1
interest = balance * rate / 100
balance = balance + interest
print(year, round(balance) )
print("The investment doubled after", year,
"years")

A: Yes you can:


balance = balance + (balance * rate/100)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 5:
 Write an algorithm to get 4 exam scores as inputs and
find the highest score. (Assume scores are from 0 to
10)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Example 5:
 Write an algorithm to get 4 exam scores as inputs and
find the highest score. (Assume scores are from 0 to
10)
 Algorithm:
 max = 0
 i=1
 while (i <= 4):
 num = eval(input())
 if (num > max):
 max = num

 i=i+1

 print(max)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Any Question ?

. .

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Any Question ?
Q: What would happen if we
push the last print statement
into the shadow of “while”?
max = 0
A: In this case, the intermediate i=1
values of the max variable will be while (i <= 4):
printed. We will have 4 printouts, num = eval(input())
once per iteration, i.e. once per if (num > max):
round. max = num
i=i+1
(You can do this for debugging and print(max)
tracing purposes !)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Any Question ?
Q: What would happen if we
push the last print statement
into the shadow of “while”?
max = 0
A: In this case, the intermediate i=1
values of the max variable will be while (i <= 4):
printed. num = eval(input())
- We will have 4 printouts, once if (num > max):
per iteration, i.e. once per max = num
round. i=i+1
- The last printout will be the print(max)
final and the correct one.

(You can do this for debugging and


tracing purposes !)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman
Summary
 While loop running for n times
 Counting up to n (as seen in example 1)
 Counting down to 0 (as seen in example 3)
 Receiving n inputs (as seen in example 2)

 Main parts of while loop:


 Initialization
 Condition
 Loop body (under the shadow of while)

Course Code: CSIS104 Course Title : Introduction to Computer Science Taught by: Hisham Othman

You might also like