PPIA
Macquarie
TALK
\(7 + 4 + 9\)
\(11 + 8 + 9\)
\(3 + 9 + 2 + 11 + 4 + 7\)
\(8 + 8 + 13 + 4 + 1 + 7 + 4 + 5 + 3\)
\(11 \times 8 \times 9\)
\(3 \times 9 \times 2 \times 11 \times 4 \times 7\)
\(8 \times 8 \times 13 \times 4 \times 1 \times 7 \times 4 \times 5 \times 3\)
do problems ALWAYS take longer to complete if we increase the size of the input?
43, 74, 36, 28, 29, 13, 71, 87, 58, 16, 32, 26, 53, 52, 88, 32, 46, 46, 86, 52, 64, 64, 24, 75, 86, 87, 17, 37, 89, 47, 12, 76, 63, 64, 53, 34, 31, 17, 62, 54
11, 55, 26, 79, 66, 59, 53, 49, 14, 56, 27, 86, 78, 25, 40, 69, 81, 84, 81, 72, 63, 78, 60, 58, 28, 24, 79, 19, 49, 30, 11, 76, 57, 45, 84, 45, 47, 56, 74, 75, 27, 44, 20, 52, 14, 10, 24, 44, 69, 21, 29, 69, 85, 69, 15, 82, 57, 88, 80, 10, 64, 79, 25, 37, 85, 27, 47, 62, 36, 67, 45, 19, 59, 73, 83, 85, 31, 75, 34, 18
12, 13, 16, 17, 17, 24, 26, 28, 29, 31, 32, 32, 34, 36, 37, 43, 46, 46, 47, 52, 52, 53, 53, 54, 58, 62, 63, 64, 64, 64, 71, 74, 75, 76, 86, 86, 87, 87, 88, 89
10, 10, 11, 11, 14, 14, 15, 18, 19, 19, 20, 21, 24, 24, 25, 25, 26, 27, 27, 27, 28, 29, 30, 31, 34, 36, 37, 40, 44, 44, 45, 45, 45, 47, 47, 49, 49, 52, 53, 55, 56, 56, 57, 57, 58, 59, 59, 60, 62, 63, 64, 66, 67, 69, 69, 69, 69, 72, 73, 74, 75, 75, 76, 78, 78, 79, 79, 79, 80, 81, 81, 82, 83, 84, 84, 85, 85, 85, 86, 88
moral of the story (so far):
problems MAY take longer to complete if the size of the input increases
this rate of growth can be different for different algorithms
sorting is good (i.e. doing something else first may simplify the problem)
ultimate goal in studying algorithms:
solve a problem using the most efficient method
efficient means slower rate of growth
n | Algorithm 1 | Algorithm 2 |
---|---|---|
100 | 21.3 s | 1.2 s |
200 | 42.1 s | 4.3 s |
300 | 59.2 s | 9.5 s |
400 | 80.2 s | 16.4 s |
500 | 98.5 s | 26.3 s |
1000 | 201.5 s | 100.5 s |
\( < = > + - \times \div + \)
find the largest and smallest element
find the largest and smallest element:
n | Algorithm 1 | Algorithm 2 |
---|---|---|
100 | 200 | 150 |
200 | 400 | 300 |
300 | 600 | 450 |
400 | 800 | 600 |
500 | 1000 | 750 |
1000 | 2000 | 1500 |
Rule 1:
compute \(x^n\)
compute \(x^n\)
\(7^{28} = \underbrace{7 \times 7 \times 7 \times \cdots \times 7}\)
28 times
compute \(x^n\)
\(7^{28} = \underbrace{7 \times 7 \times 7 \times \cdots \times 7}\)
28 times
but \(7^{28} = 7^7 * 7^7 * 7^7 * 7^7\)
Rule 1:
Rule 2:
28 in binary is
1
answer :
1
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
2
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
3
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
3
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
7
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
7
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
14
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
14
28 in binary is
1
answer :
7
compute \(7^{28}\)
Algorithm (work left-to-right):
1
1
0
0
28
n | Algorithm 1 | Algorithm 2 |
---|---|---|
100 | 100 | ? |
200 | 200 | ? |
300 | 300 | ? |
400 | 400 | ? |
500 | 500 | ? |
1000 | 1000 | ? |
n | n in binary | Algorithm 1 | Algorithm 2 |
---|---|---|---|
100 | 1100100 | 100 | ? |
200 | 11001000 | 200 | ? |
300 | 100101100 | 300 | ? |
400 | 110010000 | 400 | ? |
500 | 111110100 | 500 | ? |
1000 | 1111101000 | 1000 | ? |
n | n in binary | Algorithm 1 | Algorithm 2 |
---|---|---|---|
100 | 1100100 | 100 | 14 |
200 | 11001000 | 200 | 16 |
300 | 100101100 | 300 | 18 |
400 | 110010000 | 400 | 18 |
500 | 111110100 | 500 | 18 |
1000 | 1111101000 | 1000 | 20 |
last one:
n | Algorithm 1 | Algorithm 2 | Algorithm 3 |
---|---|---|---|
100 | |||
200 | |||
300 | |||
400 | |||
500 | |||
1000 |
Python has four basic types:
text (str)
whole numbers (int)
floating point numbers (float)
booleans
To convert between types, do:
str(1053)
int("532")
"True" "10" "Hello 123"
10 123 199999999999999
3.1415 235.463
True False
12 + 5 == 17
12 - 5 == 7
12 / 5 == 2.4
12 // 5 == 2
12 // 5.0 == 2.0
12 * 5 == 60
12 ** 5 == 248832
12 % 5 == 2
15 == 15 is True
15 == 16 is False
"Hello" == "hello" is False
"H" > "h" is True
if a > b:
print("a is greater than b")
elif a == b:
print("a is equal to b")
else:
print("a is smaller than b")
for x in "hello":
print(x)
for x in range(0,5):
print(x)
import sys
for line in sys.stdin:
print(line)
python3 main.py < input.txt
import sys
line = input()
print(line)
[1,2,3,4]
['a','b',1,2,True]
[1,2,3] + [4,5,6]
B = [1,2,3,4,5]
print(B[0])
for x in B:
print(x)
import sys
for line in sys.stdin:
words = line.split()
print(words)
round(6.5)
round(7.5)
round(5624.352634 , 2)
round(5624.352634 , -3)
to find out more:
help(round)
https://docs.python.org
print(12+5)
17
print(12+5,12-5)
17 7
print(12+5,12-5,sep='\n')
17
7