37
Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

Embed Size (px)

Citation preview

Page 1: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

Programming Appreciation Camp

Session 1: Introduction and Problem SolvingAaron Tan

NUS School of Computing

Page 2: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

2[Programming Appreciation Camp November 2008]

Contents Why are You here? Some Local Programming Competitions What is Computer Science? What is Problem Solving? What is Algorithmic Problem Solving?

Page 3: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

3[Programming Appreciation Camp November 2008]

Why are You here? What are your objectives? What is your level of programming skills? What you think programming is about? Have you taken part in any programming

competition?

Page 4: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

4[Programming Appreciation Camp November 2008]

Some Local Programming Competitions There are many local competitions, some are

more programming related, some are less: National Infocomm Competitions (NIC)

These are more algorithmic-problem-solving type of programmig competitions: National Olympiad in Informatics (NOI)

NUS School of Computing, March National Software Competition (NSC)

Singapore Polytechnic, September Asia-Pacific Informatics Olympiad (APIO)

NUS School of Computing, May

Page 5: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

5[Programming Appreciation Camp November 2008]

The Ultimate Competition The International Olympiad in Informatics (IOI) Singapore participated since 1992, and we have

won 4 gold, 16 silver, and 23 bronze medals since.

How to get to the honour roll? Participate in NOI and be one of the gold medallists If you are Singaporean, you’ll be invited for training for

IOI Do well in the selection tests during training, and be

one of the top four students Off you go, to IOI (in Bulgaria next year)!

Page 6: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

6[Programming Appreciation Camp November 2008]

What is Computer Science? (1/3) Computing Curricula 2001 (Computer Science) Report

identifies 14 knowledge focus groups Applications within and outside of Computing

Discrete Structures (DS) Programming Fundamentals (PF) Algorithms and Complexity (AL) Architecture and Organization (AR) Operating Systems (OS) Net-Centric Computing (NC) Programming Languages (PL)

Human-Computer Interaction (HC) Graphics and Visual Computing (GV) Intelligent Systems (IS) Information Management (IM) Social and Professional Issues (SP) Software Engineering (SE) Computational Science (CN)

Page 7: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

7[Programming Appreciation Camp November 2008]

What is Computer Science? (2/3) Some recommended readings

Algorithmics: The Spirit of ComputingDavid Harel, 2nd ed, Addison-Wesley (3rd ed. available)

Introduction to AlgorithmsT.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, 2nd ed, MIT Press

The New Turing Omnibus: 66 Excursions in Computer ScienceA.K. Dewdney, Holt

Page 8: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

8[Programming Appreciation Camp November 2008]

What is Computer Science? (3/3) Come to NUS School of Computing (SoC) to find

out! Honours degree in…

Communications and Media Computational Biology Computer Engineering Computer Science Electronic Commerce Information Systems

Website: http://www.comp.nus.edu.sg/undergradprog/degree.shtml

Page 9: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

9[Programming Appreciation Camp November 2008]

Problem Solving Process (1/5) Analysis Design Implementation Testing

Determine the inputs, outputs, and other components of the problem.

Description should be sufficiently specific to allow you to solve the problem.

Page 10: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

10[Programming Appreciation Camp November 2008]

Problem Solving Process (2/5) Analysis Design Implementation Testing

Describe the components and associated processes for solving the problem.

Straightforward and flexible

Method – process

Object – component and associated methods

Page 11: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

11[Programming Appreciation Camp November 2008]

Problem Solving Process (3/5) Analysis Design Implementation Testing

Develop solutions for the components and use those components to produce an overall solution.

Straightforward and flexible

Page 12: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

12[Programming Appreciation Camp November 2008]

Problem Solving Process (4/5) Analysis Design Implementation Testing

Test the components individually and collectively.

Page 13: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

13[Programming Appreciation Camp November 2008]

Problem Solving Process (5/5)

Page 14: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

14[Programming Appreciation Camp November 2008]

Problem Solving Exercises The exercises in the next few slides are of

varied nature, chosen to illustrate the extent of general problem solving.

Different kinds of questions require different domain knowledge and strategies.

Apply your problem solving skills and creativity here!

[Get participants to form groups of 3 or 4.]

Page 15: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

15[Programming Appreciation Camp November 2008]

Exercise #1: Bear A bear, starting from the point P, walked one

mile due south. Then he changed direction and walked one mile due east. Then he turned again to the left and walked one mile due north, and arrived at the point P he started from. What was the colour of the bear?

Page 16: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

16[Programming Appreciation Camp November 2008]

Exercise #2: Silver chain A traveller arrives at an inn and intends to

stay for a week. He has no money but only a silver chain consisting of 7 links. He uses one link to pay for each day spent at the inn, but the innkeeper agrees to accept no more than one broken link. How should the traveller cut up the chain in order to settle accounts with the innkeeper on a daily basis?

Page 17: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

17[Programming Appreciation Camp November 2008]

Exercise #3: Glasses of milk Six glasses are in a row, the first three full of

milk, the second three empty. By moving only one glass, can you arrange them so that empty and full glasses alternate?

Page 18: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

18[Programming Appreciation Camp November 2008]

Exercise #4: Puzzles What am I?

You can catch me but not throw me. I go all around the world but stay in the corner. You throw away the outside and eat the inside.

Then you eat the outside and throw away the inside.

Page 19: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

19[Programming Appreciation Camp November 2008]

Exercise #5: Mad scientist A mad scientist wishes to make a chain out of

plutonium and lead pieces. There is a problem, however. If the scientist places two pieces of plutonium next to each other, BOOM!!! The question is, in how many ways can the scientist safely construct a chain of length n?

Page 20: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

20[Programming Appreciation Camp November 2008]

Exercise #6: Dominoes Figure 1 below shows a domino and Figure 2 shows a 44 board

with the two squares at opposite corners removed. How do you show that it is not possible to cover this board completely with dominoes? All dominoes should lay flat on the board, without overlapping one another, and should not cover area outside the board.

Figure 1. A domino.

Figure 2. A 44 board with 2 corner squares removed.

General case: How do you show the same for an nn board with the two squares at opposite corners removed, where n is even?

Special case: How do you show the same for an nn board with the two squares at opposite corners removed, where n is odd?

Page 21: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

21[Programming Appreciation Camp November 2008]

Exercise #7: Triominoes Figure 3 below shows a triomino and Figure 4 shows a 4 4 board

with a defect (hole) in one square. How do you show that the board can be covered with triminoes?

General case: How do you show that a 2n 2n board (where n 1) with a hole in one square (anywhere on the board) can be covered with triominoes?

Figure 3. A triomino.

Figure 4. A 44 board with a hole.

Page 22: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

22[Programming Appreciation Camp November 2008]

Algorithmic Problem Solving An algorithm is a well-defined computational

procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output.

AlgorithmInput Output

Page 23: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

23[Programming Appreciation Camp November 2008]

Euclidean Algorithm First documented algorithm by Greek mathematician

Euclid in 300 B.C. To compute the GCD (greatest common divisor) of 2

integers.

1. Let A and B be integers with A > B ≥ 0.

2. If B = 0, then the GCD is A and algorithm ends.

3. Otherwise, find q and r such that

A = q.B + r where 0 ≤ r < B

Note that we have 0 ≤ r < B < A and GCD(A,B) = GCD(B,r).

Replace A by B, and B by r. Go to step 2.

Page 24: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

24[Programming Appreciation Camp November 2008]

Control Structures Three control structures

Sequence Selection (branching) Repetition (loop)

Recursion

Page 25: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

25[Programming Appreciation Camp November 2008]

Writing Pseudocodes (1/4) Example 1: Compute average of 3 integers.

A possible algorithm:

enter values for num1, num2, num3 ave ( num1 + num2 + num3 ) / 3 print ave

num1

Variables used:

num2 num3

ave

Another possible algorithm:

enter values for num1, num2, num3 total ( num1 + num2 + num3 ) ave total / 3 print ave

num1

Variables used:

num2 num3

ave

total

Page 26: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

26[Programming Appreciation Camp November 2008]

Writing Pseudocodes (2/4) Example 2: Arrange 2 integers in increasing

order (sort).Algorithm A:

enter values for num1, num2

// Assign smaller number into final1, // larger number into final2 if num1 < num2

then final1 num1 final2 num2

else final1 num2 final2 num1

// Transfer values in final1, final2 back to num1, num2 num1 final1 num2 final2

// Display sorted integers */ print num1, num2

Variables used:

num1 num2

final1 final2

Page 27: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

27[Programming Appreciation Camp November 2008]

Writing Pseudocodes (3/4) Example 2: Arrange 2 integers in increasing

order (sort).Algorithm B:

enter values for num1, num2

// Swap the values in the variables if necessary if num2 < num1

then temp num1 num1 num2

num2 temp

// Display sorted integers */ print num1, num2

Variables used:

num1 num2

temp

Page 28: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

28[Programming Appreciation Camp November 2008]

Writing Pseudocodes (4/4) Example 3: Find the sum of positive integers

up to n (assuming n is a positive integer).Algorithm:

enter value for n

// Initialise a counter count to 1, and ans to 0 count 1ans 0

while count <= n do the following ans ans + count // add count to ans

count count + 1 // increase count by 1

// Display answerprint ans

Variables used:

n

count

ans

Page 29: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

29[Programming Appreciation Camp November 2008]

Exercise: Compute Maximum Compute the maximum of a list of values. The values are entered repeatedly into a

variable num.

max ? // max to hold the largest value eventually

while there is still input,

enter num

if num > max then max num

print max

Page 30: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

30[Programming Appreciation Camp November 2008]

Task 1: Area of a circle (1/2)

2a

What is the data? Side of square = 2a

What is the unknown? Area of circle, C.

What is the condition? If radius r is known, C can be calculated.

How to obtain r?

Page 31: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

31[Programming Appreciation Camp November 2008]

Task 1: Area of a circle (2/2)

a

a r Pythagoras’ theorem:

r2 = 2 * a2

Area of circleC = * r2

= * 2 * a2

Page 32: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

32[Programming Appreciation Camp November 2008]

Task 2: Pascal’s Triangle

11 1

1 2 11 3 3 1

1 4 6 4 11 5 10 10 5 1

Compute nCk or C(n,k)

nCk = n! / (k! * (n – k)!)

Page 33: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

33[Programming Appreciation Camp November 2008]

Task 3: NE-Paths To find the number of north-east paths between any two

points. North-east (NE) path: you may only move northward or

eastward. How many NE-paths between A and C?

C

AA

A

A

Page 34: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

34[Programming Appreciation Camp November 2008]

Task 4: Palindrome A word is a palindrome if it reads the same

forward and backward. Examples: NOON, RADAR.

How do you determine if a word is a palindrome?

Page 35: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

35[Programming Appreciation Camp November 2008]

Task 5: Anagrams Two words are anagrams if the characters in

one word can be rearranged to form the other word. Example: “STAR” and “RATS”.

(In general, it could be two sentences instead of words. You may ignore spaces and punctuations in the sentences. See http://www.comp.nus.edu.sg/~tantc/cs1101/anagrams.html for some examples.)

How do you determine if two words are anagrams?

Page 36: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

36[Programming Appreciation Camp November 2008]

Task 6: Coin Change Given this list of coin denominations: $1, 50

cents, 20 cents, 10 cents, 5 cents, 1 cent, find the smallest number of coins needed for a given amount. You do not need to list out what coins are used. Example 1: For $3.75, 6 coins are needed.

Example 2: For $5.43, 10 coins are needed.

Page 37: Programming Appreciation Camp Session 1: Introduction and Problem Solving Aaron Tan NUS School of Computing

37[Programming Appreciation Camp November 2008]

THE END