CSEN102 Lecture 03 - Notes - 24860
CSEN102 Lecture 03 - Notes - 24860
14.10.2017 - 19.10.2017
1 Synopsis
1.1 Sequential operations
Synopsis
• What is an algorithm?
Definition 1 (Algorithm). An algorithm is a well-ordered collection of unam-
biguous and effectively computable operations that, when executed, produces a
result and halts in a finite amount of time.
• Why python?
• What are the necessary elements for sequential algorithms?
– Input (e. g., “A = eval(input())”)
– Output (e. g., “print(A)”, or “print("text")”)
– Calculation, manipulation (e. g., “A = B + C”)
Sequential operations
Example 2 (See last lecture). For a given number of eggs, find out how many dozen
eggs we have and how many extra eggs are left over.
1
1 eggs = eval(input())
2 dozens = int(eggs / 12)
3 extras = eggs - (dozens * 12)
4 print("Your number of eggs is ")
• Let the input be 27
5 print(dozens)
6 print(" dozen(s) and ") • What is the output?
7 print(extras)
8 print(" extra(s)")
Where the function int rounds down the result to an integer. For example int(10/3) = 3.
2 Conditional operations
2.1 Introduction
Objectives
By the end of this lecture, you should be able to:
• Design algorithms using conditional operations
Algorithms: operations
Algorithms can be constructed by the following operations:
• Sequential Operation
• Conditional Operation
• Iterative Operation
2.2 Concepts
Conditional operation – idea
2
Decision
1 nameOfSinger = input()
2 if nameOfSinger == ’Mohamed Mounir’:
3 print(’I will go home’)
4 else:
5 print(’I will stay at the GUC’)
• Rationale
– Determines whether or not a condition is true; and based on whether or not
it is true; selects the next step to do
• Notation
– Use the same primitives as before plus the following:
1 if condition:
2 # <operations for the if-part>
3 else:
4 # <operations for the else-part>
• Execution
– Evaluate <condition> expression to see whether it is true or false.
– If true, then execute operations in if-part
– Otherwise, execute operations in else-part
Input num
False
num < 0
True 1 num = eval(input())
? 2 if num < 0:
3 print("The number is negative")
The number The number 4 else:
is is
positive negative 5 print("The number is positive")
6
7 print("Good-bye for now")
Good bye
End
3
2.3 General examples
Conditional operation – examples
Example 1:
Write an algorithm to compute the absolute value of a given number.
1 Number = int(input())
2
3 if (Number >= 0):
4 Value = Number
5 else:
6 Value = (-1) * Number
7
8 print(Value)
1 radius = eval(input())
2 response = input("Type A for area or C for circumference")
3 if (response == "A"):
4 area = (radius * radius * 3.14)
5 print(area)
6 else:
7 circumference = (2 * radius * 3.14)
8 print(circumference)
4
2.4 Compounded conditions
Compounded conditions
Conditions may be compounded using AND, OR and NOT.
1 if first_condition:
2 # <do first thing>
3 else:
4 if second_condition:
5 # <do second thing>
6 else:
7 # <do something else>
5
Nested if-statement – examples
Example 2
Write an algorithm that reads each student’s marks, print either a grade or an error
message. Students marks in a class are graded on the following policy:
• A: 85-100
• B: 74-85
• C: 60-74
• D: 50-60
• F: <50
1 Mark = eval(input())
2 if(Mark >=0):
3 if (Mark >100):
4 print("invalid mark")
5 else:
6 if (Mark <50):
7 print("grade is F")
8 else:
9 if (Mark <60):
Solution with if and else
10 print("grade is D")
11 else:
12 if (Mark <74):
13 print("grade is C")
14 else:
15 if (Mark <85):
16 print("grade is B")
17 else:
18 print("grade is A")
1 Mark = eval(input())
2 if(Mark >=0):
3 if (Mark >100):
4 print("invalid mark")
5 elif (Mark <50):
6 print("grade is F")
7 elif (Mark <60):
Solution with if and elif
8 print("grade is D")
9 elif (Mark <74):
10 print("grade is C")
11 elif (Mark <85):
12 print("grade is B")
13 else:
14 print("grade is A")
6
Nested if-statement – examples
Example 3
Given an employee’s eligible medical expenses for a calendar year, write an algorithm
which computes the amount of reimbursement from group medical insurance.
• The insurance does not cover the first 100 LE of medical expenses.
• It pays 90% of the remaining amount in the first 2000 LE of expenses and 100%
of any additional expenses.
1 LL = 100
2 UL = 2000
3 Expense = int(input())
4
5 if (Expense < LL):
6 Refund = 0
7 else:
8 if (Expense < UL):
9 Refund = 0.9 * (Expense-LL)
10 else:
11 Refund = 0.90 * (UL-LL) + (Expense - UL)
12
13 print(Refund)