Python VII
Mary PW Chin 钱碧慧博士 PhD (Wales), MSc (Surrey) E-mail:
Algorithm design using simple maths
Slides here are continuously being revised and updated. If you have been here before, your browser might have some old files cached. To ensure that you see the latest version, please follow the instructions given in the following link: ☞ how to clear browser cache .
Gentle reminder
For every program you encounter it is always an excellent exercise to change the variable names to names which are most meaningful to you, then make the program run. You will gain deeper understanding of the program. You will see the program in a new light. Everyone is different: variable names most intuitive to me differ from variable names most intuitive to you. My programs are written with variable names most intuitive to me, so you should change them to names most intuitive to you so that the program flow becomes more obvious.
Synopsis
Nested loops;
while loops;
functions and returns;
more drills on logic;
multiplying using the + operator only, without the * operator;
raising to power using the * operator only, without the ** operator or the pow function;
finding the minimum, maximum and average of a list of numbers manually, without using built-in function;
converting between decimal, binary, octal and hexadecinal number systems with and without using built-in functions int , bin , oct and hex .
formatted printing.
Exercise
Write a program that prints the multiplication tables for 2, 3, 4, 5, 6, 7, 8 and 9:
1 2 2
2 2 4
3 2 6
…
…
12 2 24
1 3 3
2 3 6
3 3 9
…
…
12 3 36
1 4 4
2 4 8
3 4 12
…
…
12 4 48
1 5 5
2 5 10
3 5 15
…
…
12 5 60
1 6 6
2 6 12
3 6 18
…
…
12 6 72
1 7 7
2 7 14
3 7 21
…
…
12 7 84
1 8 8
2 8 16
3 8 24
…
…
12 8 96
1 9 9
2 9 18
3 9 27
…
…
12 9 108
Write a program that prints a combined multiplication table for 2, 3, 4, 5, 6, 7, 8 and 9 as follows:
n 2 2*n 3 3*n 4 4*n 5 5*n 6 6*n 7 7*n 8 8*n 9 9*n
1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
2 2 4 3 6 4 8 5 10 6 12 7 14 8 16 9 18
3 2 6 3 9 4 12 5 15 6 18 7 21 8 24 9 27
… 2 3 4 5 6 7 8 9
… 2 3 4 5 6 7 8 9
12 2 24 3 36 4 48 5 60 6 72 7 84 8 96 9 108
Write a program that prints the following with as few repetitions as you can:
*...*****.......*********
*********.......*****...*
*...*****.......*********
*********.......*****...*
*...*****.......*********
*********.......*****...*
*...*****.......*********
*********.......*****...*
*...*****.......*********
*********.......*****...*
Write a program that prints the following with as few repetitions as you can:
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
*..*..*..*..*
Write a program to generate a random number 1930 ≤ y ≤ 2017, take this as somebody's year of birth. Generate another random number 1 ≤ m ≤ 12, let this be his month of birth. We need to know his age in the coming years until he reaches 90. Print your output in three columns:
YEAR MONTH AGE
2018 1 __ years __ months
2018 2
2018 3
…
…
__ __ 90 years 0 month
Challenge
☞ Pixar Animation