ELEVATE

PPIA

Macquarie

TALK

Daniel Sutantyo

  • Department of Computing, Macquarie University
  • Lecturer / Tutor / Research Assistant

Programming as a Sport

  • programming and problem-solving
    • ideally you would be able to write a short computer program
    • but being able to solve problems is often more important
  • it is actually called competitive programming
    • but don't let this scare you ...

COMPETITIVE PROGRAMMING

  • you are given a problem that you have to solve using a computer

WHY?

  • learn how to solve problem
    • there are lots of problems, but not that many
    • recognise problems and general method to solve
  • learn how to solve problem
    • many problems are taken from real-world problems (and also uses techniques we use in every day life)
    • e.g. sorting
  • learn programming in a fun way
    • learn python (even if you're not a computing major)
    • learning to program is like learning a language, you have to use it
  • learn programming in a fun way
    • learn python (even if you're not a computing major)
    • learning to program is like learning a language, you have to use it
  • learn programming in a fun way
    • aren't we going to learn this at uni anyway?
  • prepare for technical interviews (for computing students)
  • it CAN be competitive
    • (if that is something you like)

HOW?

  • learn a programming language
  • if you are new to programming:
    • repl.it
    • codingbat.com/python
  • join one of the coding sites:
    • leetcode.com
    • topcoder.com
    • open.kattis.com
      • https://www.youtube.com/c/danielsutantyo
    • hackerrank.com

ALGORITHMS

  • what makes a good algorithm?
    • correct
    • efficient (in both time and space)
      • how do we measure efficiency?
      • growth rate
  • do problems take longer to complete if we increase the size of the input?

\(7 + 4 + 9\)

\(11 + 8 + 9\)

\(3 + 9 + 2 + 11 + 4 + 7\)

\(8 + 8 + 13 + 4 + 1 + 7 + 4 + 5 + 3\)

  • how much more time did you need to answer the larger problem?
  • is it linear?
  • is it always linear for all problems?

\(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
  • so how exactly do we measure the speed of an algorithm?
    • we cannot use time, because this is not precise
    • we count the number of elementary operations
      • every problem can be reduced down to a mathematical problem needing only

\( <  =  >  +  -  \times  \div  +  \)

find the largest and smallest element

find the largest and smallest element:

  • if you go through the array comparing max and min
    • need 2n comparisons
  • if you divide the array into two parts
    • need n/2 + n = 3n/2 comparisons
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:

  • don't compute something if you don't have to

 

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:

  • don't compute something if you don't have to

Rule 2:

  • don't compute the same thing twice

28 in binary is 

1

answer   : 

1

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

2

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

3

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

3

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

7

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

7

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

14

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

1

1

0

0

14

28 in binary is 

1

answer   : 

7

compute \(7^{28}\)

Algorithm (work left-to-right):

  • if you see a 0, square answer
  • if you see a 1, square answer, then multiply by 7

 

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: 

  • you are in a room with N people, find out if there are two people with the same birthday (day/month)
  • you don't need to find out the actual birthday, just answer yes or no
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

basic math

basic math

  • 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
    

logic and if STATEMENT

  • 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 loop

for x in "hello":
  print(x)
for x in range(0,5):
  print(x)

reading input

import sys

for line in sys.stdin:
  print(line)
python3 main.py < input.txt
import sys

line = input()
print(line)

LIST

[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

not-so-basic math

PRINTING

  • print(12+5)
    • 17
  • print(12+5,12-5)
    • 17 7
  • print(12+5,12-5,sep='\n')
    • 17
    • 7

PPIA

By Daniel Sutantyo

PPIA

  • 160