Understanding Algorithms in Programming
Understanding Algorithms in Programming
—
COMP100: Lecture Three
1/16
The story so far ...
2/16
The story so far ...
2/16
The story so far ...
2/16
The story so far ...
2/16
The story so far ...
2/16
Key takeaways: What is an Algorithm?
3/16
Key takeaways: What is an Algorithm?
3/16
Key takeaways: What is an Algorithm?
3/16
Key takeaways: What is an Algorithm?
3/16
High Level Languages
4/16
High Level Languages
4/16
High Level Languages
4/16
Programming is ...?
Ġ Design/Devise an algorithm
—
COMP100: Lecture Three
5/16
Programming is ...?
Ġ Design/Devise an algorithm
Ġ Write it in a Programming Language
—
COMP100: Lecture Three
5/16
Programming is ...?
Ġ Design/Devise an algorithm
Ġ Write it in a Programming Language
Ġ Run
—
COMP100: Lecture Three
5/16
Programming is ...?
Ġ Design/Devise an algorithm
Ġ Write it in a Programming Language
Ġ Run
Ġ Test and debug
—
COMP100: Lecture Three
5/16
Programming is ...?
Ġ Design/Devise an algorithm
Ġ Write it in a Programming Language
Ġ Run
Ġ Test and debug
—
Ġ Profit
COMP100: Lecture Three
5/16
This Lecture ...
6/16
This Lecture ...
6/16
This Lecture ...
6/16
This Lecture ...
6/16
This Lecture ...
2
COMP100: Lecture Three
6/16
How do we tell if a year is a leap year?
Every year that is exactly divisible by four is a leap year, except for
years that are exactly divisible by 100, but these centurial years are
leap years if they are exactly divisible by 400. For example, the years
1700, 1800, and 1900 are not leap years, but the years 1600 and 2000
are
—
COMP100: Lecture Three
7/16
Leap Year
8/16
Leap Year
8/16
Leap Year
8/16
Leap Year
was not.
COMP100: Lecture Three
8/16
Leap Year Algorithm
Algorithm is_leap_year:
year ← input year
else
answer ← True
9/16
Zeller’s Congruence
10/16
Zeller’s Congruence
10/16
Zeller’s Congruence
10/16
DAY OF WEEK Algorithm
Algorithm: Zellers_Congruence:
h ← (day
COMP100: Lecture Three
12/16
Zeller’s Congruence
Algorithm: Zellers_Congruence:
h ← (day
+ ((13 ∗ (month + 1)) div 5)
—
+ year
+ (year div 4)
− (year div 100)
COMP100: Lecture Three
output day
0 − 6 where 0 = Saturday, 1 = Sunday ...
12/16
Zeller’s Congruence
Algorithm: Zellers_Congruence:
h ← (day
+ ((13 ∗ (month + 1)) div 5)
—
+ year
+ (year div 4)
− (year div 100)
COMP100: Lecture Three
output day
0 − 6 where 0 = Saturday, 1 = Sunday ...
12/16
Zeller’s Congruence
h ← (day
+ ((13 ∗ (month + 1)) div 5)
—
+ year
+ (year div 4)
− (year div 100)
COMP100: Lecture Three
output day
0 − 6 where 0 = Saturday, 1 = Sunday ...
12/16
Zeller’s Congruence
h ← (day
+ ((13 ∗ (month + 1)) div 5)
—
+ year
+ (year div 4)
− (year div 100)
COMP100: Lecture Three
output day
0 − 6 where 0 = Saturday, 1 = Sunday ...
12/16
Zeller’s Congruence
+ year
+ (year div 4) day of the week.
− (year div 100)
COMP100: Lecture Three
output day
0 − 6 where 0 = Saturday, 1 = Sunday ...
12/16
Zeller’s Congruence
+ year
+ (year div 4) day of the week.
− (year div 100)
COMP100: Lecture Three
12/16
Day of the Week: Python
day = int(input(”day−number?␣␣”))
month = int(input(”month−number?␣␣”))
year = int(input(”year␣number?␣”))
if month < 3:
month += 12
year −= 1
day = ”Sunday”
case 2:
day = ”Monday”
COMP100: Lecture Three
case 3:
day = ”Tuesday”
case 4:
day = ”Wednesday”
case 5:
day = ”Thursday”
case 6:
day = ”Friday”
case _:
day = ”Invalid␣day␣number”
print(f”The␣day␣is:␣{day}”)
13/16
Elements of Programming I
14/16
Elements of Programming I
14/16
Elements of Programming I
14/16
Elements of Programming I
14/16
Elements of Programming I
14/16
Elements of Programming II
15/16
Elements of Programming II
15/16
Elements of Programming II
15/16
Elements of Programming II
15/16
Elements of Programming II
15/16
Elements of Programming II
15/16
Elements of Programming II
15/16
What is a Programming Language?
16/16
What is a Programming Language?
16/16
What is a Programming Language?
16/16
What is a Programming Language?
16/16
What is a Programming Language?
executed.
COMP100: Lecture Three
16/16
What is a Programming Language?
executed.
Semantics: Usually it is how memory changes.
COMP100: Lecture Three
16/16