principles in programming: orientation & lecture...

22
1 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected]) Principles in Programming: Orientation & Lecture 1

Upload: nguyentu

Post on 30-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

1 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Principles in Programming: Orientation & Lecture 1

2 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Course Objectives §  Introduce various subjects

in computer science through puzzles and problems

§  Most problems came from ICPC

3 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Introduction §  Instructor: Euiseong Seo

•  Associate professor, Software Dept. •  Web page: http://csl.skku.edu/Euiseong •  E-mail: [email protected] •  Office: #85564 (5F of Industrial Collaboration Bldg.)

§  Course Homepage •  http://csl.skku.edu/SWE2004S15

§  Lab •  Thursday class •  #400222 •  Conducted by Two TAs

4 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Course Elements §  14 Lectures (once every week) (10%) §  10 Programming Labs (20%) §  2 Individual Programming Homework

Assignments (15%) §  1 Team Project (15%) §  Final Exam (40%)

•  Most questions will be based on labs and assignments

•  Programming

5 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Textbook §  Programming Challenges by

Steven S. Skiena and Miguel A. Revilla – Springer •  You can download from the SKKU library

6 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Course Rules (1/2) §  Cheating in exams

•  machine check •  will receive an “F” for the course

§  Late homework •  10% penalty per day

§  Cheating on homework •  will receive a “0” point

§  1% penalty for missing a lecture class §  2% penalty for missing a Lab.

7 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Course Rules (2/2) §  “not attending” a class includes

•  not attending a class •  being late to a class •  leaving a class in the middle •  chatting in class •  having the mobile phone on in class •  if you sleep, you will die!!

8 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Course Outline

9 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Problem Solving §  Using C §  Basic Software Engineering

§  Programming Patterns §  Problem Solving Techniques

§  Practice

10 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

What You Need to Solve a Problem by Programming

§  Programming Language Skills •  Correct rules •  Identifying rule violations

§  Programming Skills •  Training on programming patterns •  Training on software engineering methods

§  Problem Solving Skills •  Logical thinking

11 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Programming §  Programming Is To Use the “Dumb” Computer

To Solve a Problem That A Human Cannot Solve Fast Enough.

§  The Computer Needs “Very Very Very” Precise and Detailed Instructions.

§  The Instructions Must Be in a Programming Language, Not a Natural Language.

12 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Natural Language

What is 27.2 times 13.8 ?

13 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Programming Languages §  Machine Languages §  Assembly Languages §  High-Level Languages

14 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Machine Language

000000 00001 00010 00110 00000 100000 Add the registers 1 and 2 and place the result in register 6 100011 00011 01000 00000 00001 000100 Load a value into register 8, taken from the memory cell 68 after the location listed in register 3: 000010 00000 00000 00000 00100 000000 Jump to the memory address 1024:

15 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Assembly Language

MOV r0, #0C load base address of string into r0 LOAD: MOV r1,(r0) load contents into r1 CALL PRINT call a print routine to print the character in r1 INC r0 point to the next character JMP LOAD load next character

16 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

High-Level Language

float length, width, area; length = 27.2; width = 13.8; area = length * width;

17 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

High-Level Languages §  Over 500 Languages (

http://en.wikipedia.org/wiki/List_of_programming_languages_by_category)

§  Basic, FORTRAN, COBOL, RPG §  (Algol, Pascal, PL/1), C §  C++, C#, Java (ADA, Smalltalk, Eiffel) §  Perl, TCL, Java Script, PHP, Python, Ruby §  SNOBOL, LISP, (Scheme) §  MATLAB, (APL) §  Shell, Awk, REXX §  SQL, (Prolog), XML, Xquery, XSLT, Postscript, OWL §  4GL §  UML §  Verilog, VHDL

18 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Executing Programs 1.  Compile

Converting programs written in a high-level language into an assembly language or a pseudo code

2.  Assemble Converting programs written in an assembly language into a machine language

§  Interpret •  Running programs written in a high-level language with

out compiling (one instruction at a time)

19 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Programming Languages §  “You Can Solve Any Problem Using

Any Programming Language” §  But Different Languages Are

Designed To Serve Different Purposes Better. •  FORTRAN for scientific computations •  COBOL for business data processing •  LISP for list processing •  VisualBasic for user-interface programming •  SQL, PHP for database applications •  C++, Java for object-oriented software development •  C for most modern enterprise/scientific applications

20 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Sample Problem: Game Strategy Problem D

Game StrategyTime Limit: 8 seconds

Alice and Bob are playing a board game. The board is divided into positions labeled a, b, c, d, . . . andthe players use a gamepiece to mark the current position. Each round of the game consists of two steps:

1. Alice makes a choice. Depending on the current position, she has different options, where eachoption is a set of positions. Alice chooses one set S among the available sets of positions.

2. Bob makes a choice. His choice is one position p from the set S that Alice chose in step 1. Bobmoves the gamepiece to position p, which is the position for the start of the next round.

Prior to the first round, each player independently selects one of the positions and reveals it at the startof the game. Bob’s position is where the game starts. Alice wins the game if she can force Bob to movethe gamepiece to the position she has chosen. To make things interesting, they have decided that Bobwill pay Alice a certain amount if he loses, but Alice must pay Bob a certain amount after every round.The game now ends if Alice’s position is reached or when Alice runs out of cash.

Both Alice and Bob play optimally: Alice will always choose an option that will lead to her winning thegame, if this is possible, and Bob will always try to prevent Alice from winning.

For all possible start and end positions, Alice would like you to determine whether she can win the gameand if so, how many rounds it will take.

Input

The input consists of a single test case. The first line contains the number of positions n (1 n 25).The n positions are labeled using the first n letters of the English alphabet in lowercase. The rest of thetest case consists of n lines, one for each position p, in alphabetical order. The line for position p containsthe options available to Alice in position p. It starts with the number of options m (1 m < 2

n), whichis followed by m distinct strings, one for each option. Each string contains the positions available toBob if Alice chooses that option. The string has at least 1 character, the characters (which correspondto valid board positions) are in alphabetical order, and no characters are duplicated. The total number ofoptions for the test case is at most 106.

Output

For each position p in alphabetical order, display one line. In that line, for each position q in alphabeticalorder display the minimal number of rounds in which Alice can be guaranteed to arrive at position q

when starting the game in position p, or �1 if Alice cannot be guaranteed to reach q from p.

Sample Input 1 Sample Output 1

2

2 ab b

1 b

0 1

-1 0

ACM-ICPC World Finals 2014 Problem D: Game Strategy 7

21 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Sample Problem: Game Strategy

Problem DGame Strategy

Time Limit: 8 seconds

Alice and Bob are playing a board game. The board is divided into positions labeled a, b, c, d, . . . andthe players use a gamepiece to mark the current position. Each round of the game consists of two steps:

1. Alice makes a choice. Depending on the current position, she has different options, where eachoption is a set of positions. Alice chooses one set S among the available sets of positions.

2. Bob makes a choice. His choice is one position p from the set S that Alice chose in step 1. Bobmoves the gamepiece to position p, which is the position for the start of the next round.

Prior to the first round, each player independently selects one of the positions and reveals it at the startof the game. Bob’s position is where the game starts. Alice wins the game if she can force Bob to movethe gamepiece to the position she has chosen. To make things interesting, they have decided that Bobwill pay Alice a certain amount if he loses, but Alice must pay Bob a certain amount after every round.The game now ends if Alice’s position is reached or when Alice runs out of cash.

Both Alice and Bob play optimally: Alice will always choose an option that will lead to her winning thegame, if this is possible, and Bob will always try to prevent Alice from winning.

For all possible start and end positions, Alice would like you to determine whether she can win the gameand if so, how many rounds it will take.

Input

The input consists of a single test case. The first line contains the number of positions n (1 n 25).The n positions are labeled using the first n letters of the English alphabet in lowercase. The rest of thetest case consists of n lines, one for each position p, in alphabetical order. The line for position p containsthe options available to Alice in position p. It starts with the number of options m (1 m < 2

n), whichis followed by m distinct strings, one for each option. Each string contains the positions available toBob if Alice chooses that option. The string has at least 1 character, the characters (which correspondto valid board positions) are in alphabetical order, and no characters are duplicated. The total number ofoptions for the test case is at most 106.

Output

For each position p in alphabetical order, display one line. In that line, for each position q in alphabeticalorder display the minimal number of rounds in which Alice can be guaranteed to arrive at position q

when starting the game in position p, or �1 if Alice cannot be guaranteed to reach q from p.

Sample Input 1 Sample Output 1

2

2 ab b

1 b

0 1

-1 0

ACM-ICPC World Finals 2014 Problem D: Game Strategy 7

22 SWE2004: Principles in Programming | Spring 2015 | Euiseong Seo ([email protected])

Sample Problem: Game Strategy

Problem DGame Strategy

Time Limit: 8 seconds

Alice and Bob are playing a board game. The board is divided into positions labeled a, b, c, d, . . . andthe players use a gamepiece to mark the current position. Each round of the game consists of two steps:

1. Alice makes a choice. Depending on the current position, she has different options, where eachoption is a set of positions. Alice chooses one set S among the available sets of positions.

2. Bob makes a choice. His choice is one position p from the set S that Alice chose in step 1. Bobmoves the gamepiece to position p, which is the position for the start of the next round.

Prior to the first round, each player independently selects one of the positions and reveals it at the startof the game. Bob’s position is where the game starts. Alice wins the game if she can force Bob to movethe gamepiece to the position she has chosen. To make things interesting, they have decided that Bobwill pay Alice a certain amount if he loses, but Alice must pay Bob a certain amount after every round.The game now ends if Alice’s position is reached or when Alice runs out of cash.

Both Alice and Bob play optimally: Alice will always choose an option that will lead to her winning thegame, if this is possible, and Bob will always try to prevent Alice from winning.

For all possible start and end positions, Alice would like you to determine whether she can win the gameand if so, how many rounds it will take.

Input

The input consists of a single test case. The first line contains the number of positions n (1 n 25).The n positions are labeled using the first n letters of the English alphabet in lowercase. The rest of thetest case consists of n lines, one for each position p, in alphabetical order. The line for position p containsthe options available to Alice in position p. It starts with the number of options m (1 m < 2

n), whichis followed by m distinct strings, one for each option. Each string contains the positions available toBob if Alice chooses that option. The string has at least 1 character, the characters (which correspondto valid board positions) are in alphabetical order, and no characters are duplicated. The total number ofoptions for the test case is at most 106.

Output

For each position p in alphabetical order, display one line. In that line, for each position q in alphabeticalorder display the minimal number of rounds in which Alice can be guaranteed to arrive at position q

when starting the game in position p, or �1 if Alice cannot be guaranteed to reach q from p.

Sample Input 1 Sample Output 1

2

2 ab b

1 b

0 1

-1 0

ACM-ICPC World Finals 2014 Problem D: Game Strategy 7

Sample Input 2 Sample Output 2

3

1 b

2 b a

2 ab ac

0 1 -1

1 0 -1

2 2 0

ACM-ICPC World Finals 2014 Problem D: Game Strategy 8