Programming%20Interest%20Group - PowerPoint PPT Presentation

About This Presentation
Title:

Programming%20Interest%20Group

Description:

Or called Machine code, understandable directly by CPU ... controlled through the Java Community Process (http://www.jcp.org/en/home/index) ... – PowerPoint PPT presentation

Number of Views:149
Avg rating:3.0/5.0
Slides: 56
Provided by: xiaow
Category:

less

Transcript and Presenter's Notes

Title: Programming%20Interest%20Group


1
Programming Interest Group
  • Tutorial One
  • Introduction
  • Get Familiar with Your Weapon

2
What is ACM?
  • ACM Association for Computing Machinery
  • http//www.acm.org/
  • the worlds largest educational and scientific
    computing society
  • ACM ICPC
  • ACM International Collegiate Programming Contest
  • http//en.wikipedia.org/wiki/ACM_International_Col
    legiate_Programming_Contest

3
ACM ICPC
  • ICPC is a two-tiered competition among teams of
    students representing institutions of higher
    education.
  • Teams compete in Regional Contests, from which
    top scoring teams advance to the ACM-ICPC World
    Finals.
  • Each team has three students, sharing one
    computer, given a number of programming problems
  • Coordination and teamwork are essential

4
Online Judge (OJ)
  • When you finish a problem, submit the source code
    to the online judge
  • You can use C, C, Java
  • The online judge will compile your source code,
    and check with unknown number of secret test
    cases
  • Feedback from the judge
  • Accepted (AC) congratulations!
  • Presentation Error (PE) Your program outputs
    are correct, but are not presented in the
    specified format. Check for spaces, left/right
    justification, line feeds, etc.
  • Wrong Answer (WA) Your program returned an
    incorrect answer to one or more of the judges
    secret test cases
  • Compile Error (CE) The judges compiler cannot
    compile your source code

5
Online Judge (cont.)
  • Runtime Error (RE) Your program failed during
    execution due to a segmentation fault, floating
    point exception, or others.
  • Time Limit Exceeded (TL) Your program took too
    much time on at least one of the test cases. Try
    to improve the efficiency of your solution!
  • Memory Limit Exceeded (ML) Your program tried
    to use more memory than the judges settings.

6
Available OJs
  • There are many famous online judges
  • Valladolid OJ (http//acm.uva.es/p)
  • Ural OJ (http//acm.timus.ru)
  • Saratov OJ (http//acm.sgu.ru)
  • ZJU OJ (http//acm.zju.edu.cn)
  • ZJUT OJ (http//acm.zjut.edu.cn)
  • Official ACM Live Archive (http//cii-judge.baylor
    .edu/)
  • Peking University Online Judge (http//acm.pku.edu
    .cn/JudgeOnline/)
  • Programming Challenges (http//www.programming-cha
    llenges.com)

7
What do we use?
  • Programming Challenges
  • Steven S. Skiena and Miguel Revilla, Programming
    Challenges, the Programming Contest Training
    Manual, Springer, 2003.
  • http//www.programming-challenges.com/pg.php?page
    index
  • Our main training reference, available from
    library

8
Suggested Books
  • Art of Programming Contest (free online)
  • http//online-judge.uva.es/p/Art_of_Programming_Co
    ntest_SE_for_uva.pdf
  • Thomas H. Cormen, Charles E. Leiserson, Ronald L.
    Rivest, and Clifford Stein, Introduction to
    Algorithms, 2nd Edition, The MIT Press, 2001.
  • Robert Sedgewick, Bundle of Algorithms in Java,
    Third Edition (Parts 1-5), 3rd Edition,
    Addison-Wesley Professional, 2003. (There is also
    a C version).
  • Donald E. Knuth,The Art of Computer Programming,
    Volume 1, 2, 3.
  • Bill Gates said that in order to be "a really
    good programmer" one must read its first volume
    and solve all of its problems before sending his
    company a resume.

9
Programming Languages
  • Machine Language
  • Or called Machine code, understandable directly
    by CPU
  • Different CPU has different Instruction Set
  • A program is just a sequence of instructions that
    are executed by a CPU.
  • E.g. adding registers 1 and 2 and placing the
    result in register 6 is encoded as
  • 000000 00001 00010 00110 00000 100000

10
Machine Language
  • Shall we understand the so cold binary code?
  • CPU designer
  • Writing an assembler
  • Reverse-engineering
  • http//www.reverse-engineering.net/
  • A book
  • Hacker Disassembling Uncovered by Kris Kaspersky

11
Assembly Language
  • The same as machine language. But the command
    numbers are replaced by letter sequences,
    understandable by CS students.
  • Assembler transform the assembly language into
    machine code
  • E.g.

movl 1, eax movl 0, ebx int 0x80
12
Assembly Language
  • Shall we learn assembly language today?
  • Write an Operating System
  • Write a compiler
  • Optimize the performance of a piece of program
  • MMX, SSE, SSE2, etc.
  • Help us to understand how computer works!
  • A free book Programming from the Ground Up, by
    Jonathan Bartlett

13
High-level Language
  • Allow us to describe the program in a more
    natural language.
  • A comparison of different programming languages
  • http//en.wikipedia.org/wiki/Comparison_of_program
    ming_languages
  • Compilation vs. Interpretation
  • Interpreters are generally slower to run, but
    more flexible than compilers.

14
C
  • Quoted from wikipedia
  • The C programming language (often, just "C") is a
    general-purpose, procedural, imperative computer
    programming language developed in the early 1970s
    by Dennis Ritchie for use on the Unix operating
    system.
  • KR C
  • In 1978, Dennis Ritchie and Brian Kernighan
    published the first edition of The C Programming
    Language. This book, known to C programmers as
    "KR," served for many years as an informal
    specification of the language. The version of C
    that it describes is commonly referred to as "KR
    C."

15
C
  • ANSI C and ISO C
  • The first standard for C was published by ANSI.
  • After a long and arduous process, the standard
    was completed in 1989 and ratified as ANSI
    X3.159-1989 "Programming Language C." This
    version of the language is often referred to as
    ANSI C, or sometimes C89.
  • In 1990, the ANSI C standard (with a few minor
    modifications) was adopted by the International
    Organization for Standardization (ISO) as ISO/IEC
    98991990. This version is sometimes called C90.
    Therefore, the terms "C89" and "C90" refer to
    essentially the same language.
  • ANSI C is now supported by almost all the widely
    used compilers.

16
C
  • C99
  • After the ANSI standardization process, the C
    language specification remained relatively static
    for some time, whereas C continued to evolve.
  • A new C standard is finally published as ISO
    98991999 in 1999. This standard is commonly
    referred to as "C99." It was adopted as an ANSI
    standard in March 2000.
  • Some new features
  • Inline functions
  • Variables can be declared anywhere
  • Variable-length arrays
  • Support for one-line comments beginning with //

17
C Books
  • C Programming Language, 2nd Edition, by Brian W.
    Kernighan and Dennis M. Ritchie.
  • Expert C Programming deep C secrets, by Peter
    van der Linden.
  • The Standard C Library, by P. J. Plauger.

18
C
  • Stroustrup began work on C with Classes in 1979.
  • In 1983, the name of the language was changed
    from C with Classes to C.
  • In 1985, the first edition of The C Programming
    Language was released.
  • As the C language evolved, a standard library
    also evolved with it, which finally leaded to
    Standard Template Library (STL).
  • After years of work, a joint ANSI-ISO committee
    standardized C in 1998 (ISO/IEC 148821998).
  • The 1998 C standard consists of two parts the
    core language and the C standard library the
    latter includes most of the Standard Template
    Library and a slightly modified version of the C
    standard library.
  • A newer version of C standard was published in
    2003.

19
C
  • C can be thought of as comprising three parts
  • The low-level language, largely from C
  • Object-Oriented programming to define our own
    data types and to organize large-scale programs
    and systems
  • Standard library to provide a set of useful data
    structures and algorithms

20
C
  • Templates and Generic programming
  • Generic programming involves writing code in a
    way that is independent of any particular type.
  • Templates are the foundation of generic
    programming.

int compare(const string v1, const string
v2) if (v1 lt v2) return -1 if(v2 lt
v1) return 1 return 0
int compare(const double v1, const double
v2) if (v1 lt v2) return -1 if(v2 lt
v1) return 1 return 0
21
C
  • The following is a template version of compare

Template lttypename Tgt int compare(const T v1,
const T v2) if (v1 lt v2) return -1
if(v2 lt v1) return 1 return 0
22
C Books
  • C Primer
  • C Primer Plus
  • Thinking in C
  • The C Programming Language, by Bjarne
    Stroustrup
  • The C Standard Library A Tutorial and
    Reference , by Nicolai M. Josuttis

23
Java
  • Java was started as a project called "Oak" by
    James Gosling in June 1991.
  • The first public implementation was Java 1.0 in
    1995. It made the promise of "Write Once, Run
    Anywhere" (WORA), with free runtimes on popular
    platforms.
  • Java remains a proprietary de facto standard that
    is controlled through the Java Community Process
    (http//www.jcp.org/en/home/index).

24
Java
  • Object-Oriented programming
  • Platform independence
  • It means that programs written in the Java
    language must run similarly on diverse hardware.
    One should be able to write a program once and
    run it anywhere.
  • Java language code ? Java bytecode
  • Jave bytecode is then run on a virtual machine
    (VM), a program written in native code on the
    host hardware that interprets and executes
    generic Java bytecode.
  • To improve the performance, Just-in-time (JIT)
    compilation has been introduced, by translating
    bytecode into native machine code at runtime.

25
Java Libraries
  • Core libraries
  • Data structures
  • XML parsing libraries
  • Security
  • Internationalization and localization
  • Integration libraries
  • The Java Database Connectivity (JDBC) API for
    database access
  • Java Naming and Directory Interface (JNDI) for
    lookup and discovery
  • RMI and CORBA for distributed application
    development
  • User interface libraries
  • AWT (Abstract Windowing Toolkit )
  • Swing
  • APIs for audio capture, processing, and playback
  • Lots of extensions
  • Java EE (J2EE), Java ME (J2ME)
  • JMF (Java Media Framework)
  • JAI (Java Advanced Imaging)
  • Java 3D, JOGL (Java OpenGL)

26
Others
  • Python
  • Rumor advocated by Google
  • "Python has been an important part of Google
    since the beginning, and remains so as the system
    grows and evolved. Today dozens of Google
    engineers use Python, and we're looking for more
    people with skills in this language"-- Peter
    Norvig, Director of Search Quality at Google
  • I am the author of the Python programming
    language. In December 2005 I joined Google in
    Mountain View, CA.-- Guido van Rossum, Creator
    of Python
  • C
  • Developed by Microsoft (to combat Java)

27
Standard Input/Output
  • In ACM contest, each program must read the test
    data from the standard input and print the
    results to the standard output
  • For C language, use scanf() and printf()
  • For C, use cin and cout
  • scanf() and printf() are also supported
  • For Java, refer to http//www.programming-challeng
    es.com/pg.php?pagejavainfo
  • Programs are not allowed to open files or to
    execute certain system calls

28
Not convenient for debugging?
  • include ltstdio.hgt
  • int main ()
  • freopen(FILE_NAME_FOR_INPUT,r,stdin)
  • freopen(FILE_NAME_FOR OUTPUT,w,stdout)
  • Rest of the codes
  • return 0

While sending your code to online judges,
remember to remove the two lines with freopen.
29
Simple Coding
  • Avoid the usage of the or -- operators inside
    expressions or function calls
  • Avoid expressions of the form p
  • Avoid pointer arithmetic. Instead of (p5) use
    p5.
  • Never code like return (xy)Func(t)/(1-s)
  • but like
  • temp func(t)
  • RetVal (xy) temp/(1-s)
  • return RetVal

30
Naming
  • Dont use small and similar names for your
    variables. Use descriptive names.
  • Dont use names like i,j,k for loop control
    variables. Use I,K,M.
  • It is very easy to mistake a j for an i when you
    read code or copy, paste change code,

31
Internal force
  • Data Structures
  • Commonly seen in almost every programming
    projects
  • A treasure from tens of years of experiences
  • Summarized by computer scientists as textbooks
  • A core subject for every computer
    science/computer engineering department

32
Internal force
  • Algorithms
  • To solve real problems efficiently
  • Categories
  • Sorting
  • Searching
  • Graph algorithms
  • Scientific computing matrix, number-theoretic,
    computational geometry, etc.

33
Internal force
  • Mathematics
  • Everything finally goes back to mathematics!
  • Number theory
  • Geometry
  • Combinatorics
  • Graph theory

34
A Good Team
  • three factors crucial for being a good
    programming team
  • Knowledge of standard algorithms and the ability
    to find an appropriate algorithm for every
    problem in the set
  • Ability to code an algorithm into a working
    program
  • Having a strategy of cooperation with your
    teammates

35
Quickly identify problem types
  • In programming contests, you will be dealing with
    a set of problems, not only one problem.
  • Problem types
  • Mathematics (number theory, big integer, etc)
  • Sorting
  • Searching
  • Simulation
  • String processing
  • Dynamic programming (DP)
  • Graph
  • Computational geometry
  • Ad Hoc (i.e., no standard categories)

36
Analyze your algorithm
  • Proof of algorithm correctness
  • Time/Space complexity analysis for non recursive
    algorithms
  • For recursive algorithms, the knowledge of
    computing recurrence relations and analyze them
    iterative method, substitution method, recursion
    tree method and finally, Master Theorem
  • Given the maximum input bound (usually given in
    problem description), can my algorithm, with the
    complexity that I can compute, pass the time
    limit given in the programming contest?

37
Some rules of thumb
  • Biggest built in data structure "long long" is
    263-1 91018 (up to 18 digits)
  • If you have k nested loops running about n
    iterations each, the program has O(nk) complexity
  • The best times for sorting n elements are
    O(nlog n)
  • DP algorithms which involves filling in a matrix
    usually in O(n3)
  • In contest, most of the time O(n log n)
    algorithms will be sufficient

38
Testing your code
  • You wont get any credit by partially solving the
    problem.
  • You need to be able to design good test cases
  • Sample input-output given in problem description
    is by default too trivial to measure your code's
    correctness
  • Before submission, you may want to design some
    tricky test cases first, test them in your own
    machine, and ensure your code is able to solve
    them correctly

39
Guidelines in designing good test cases
  • Must include sample input, the most trivial one
  • Must include boundary cases, what is the maximum
    n,x,y, or other input variables, try varying
    their values to test for out of bound errors
  • For multiple input test case, try using two
    identical test case consecutively. Both must
    output the same result. This is to check whether
    you forgot to initialize some variables
  • Increase the size of input. Sometimes your
    program works for small input size, but behave
    wrongly when input size increases.
  • Tricky test cases, analyze the problem
    description and identify parts that are tricky,
    test them to your code.
  • Dont assume input will always nicely formatted
    if the problem description didnt say so. Try
    inserting white spaces (space, tabs) in your
    input, check whether your code is able to read in
    the values correctly
  • Finally, do random test cases, try random input
    and check your code's correctness

40
Producing Winning Solution
  • Write down a game plan for what you're going to
    do in a contest round
  • Read through all the problems first, don't
    directly attempt one problem since you may missed
    easier problem.
  • Order the problems shortest job first, in terms
    of your effort
  • Sketch the algorithms, complexity, the numbers,
    data structures, tricky details.
  • Brainstorm other possible algorithms
  • Do the Math! (space time complexity)
  • Code it of course, as fast as possible, and it
    must be correct
  • Try to break the algorithm - use special
    (degenerate?) test cases.

41
Coding a problem
  • Only coding after you finalize your algorithm.
  • Create test data for tricky cases.
  • Code the input routine and test it (write extra
    output routines to show data).
  • Code the output routine and test it.
  • Write data structures needed.
  • Stepwise refinement write comments outlining the
    program logic.
  • Fill in code and debug one section at a time.
  • Get it working verify correctness (use trivial
    test cases).
  • Try to break the code - use special cases for
    code correctness.

42
Tips tricks for contests
  • Brute force when you can, Brute force algorithm
    tends to be the easiest to implement.
  • KISS Simple is smart! (Keep It Simple, Stupid
    !!! / Keep It Short Simple).
  • Hint focus on limits (specified in problem
    statement).
  • Waste memory when it makes your life easier
    (trade memory space for speed).
  • Don't delete your extra debugging output, comment
    it out.
  • Optimize progressively, and only as much as
    needed.
  • Keep all working versions!

43
Tips tricks for contests (Cont.)
  • Code to debug
  • a. white space is good,
  • b. use meaningful variable names,
  • c. don't reuse variables, (we are not doing
    software engineering here)
  • d. stepwise refinement,
  • e. Comment before code.
  • Avoid pointers if you can.
  • Avoid dynamic memory like the plague statically
    allocate everything.
  • Try not to use floating point if you have to,
    put tolerances in everywhere (never test equality)

44
Some Google interview questions
  • Given a function which produces a random integer
    in the range 1 to 5, write a function which
    produces a random integer in the range 1 to 7.
  • Find the intersection of 2 sorted integer arrays.
    What if one of them is huge? What if one of them
    is so huge, it can't fit in memory. How do you
    minimize the number of disk seeks?
  • Given a string A, how do you find all the
    repeated substrings with minimum size of 2?
  • Given N computers networked together, with each
    computer storing N integers, describe a procedure
    for finding the median of all of the numbers.
    Assume that a computer can only hold O(N)
    integers (i.e. no computer can store all N2
    integers). Also assume that there exists a
    computer on the network without integers, that we
    can use to interface with the computers storing
    the integers .

45
Example AB Problemhttp//acm.zju.edu.cn/onlinej
udge/showProblem.do?problemCode1001
  • Time Limit1 second  Memory Limit32768 KB
  • Description Calculate a b
  • Input
  • The input will consist of a series of pairs of
    integers a and b, separated by a space, one pair
    of integers per line.
  • Output
  • For each pair of input integers a and b you
    should output the sum of a and b in one line, and
    with one line of output for each line in input.
  • Sample Input
  • 1 5
  • Sample Output
  • 6

46
Example Solution
/ C code / include stdio.h int main()
int a, b while (scanf(d d, a, b) !
EOF) printf(d\n, ab)
return 0
/ Java code / import java.util.Scanner
public class Main public static void
main(String args) Scanner in new
Scanner(System.in) while
(in.hasNextInt()) int a
in.nextInt() int b in.nextInt()
System.out.println(a b)

47
Fundamental Problemshttp//acm.zjut.edu.cn
  • 1167
  • 1166
  • 1174
  • 1175
  • 1176
  • 1177
  • 1178
  • 1179
  • 1181
  • 1185
  • 1190
  • 1191
  • 1187
  • 1204
  • 1208
  • 1205
  • 1044

48
Fundamental Problemshttp//acm.uva.es
  • http//acm.uva.es/p/v1/100.html
  • http//acm.uva.es/p/v101/10189.html
  • http//acm.uva.es/p/v101/10137.html
  • http//acm.uva.es/p/v7/706.html
  • http//acm.uva.es/p/v102/10267.html
  • http//acm.uva.es/p/v100/10033.html
  • http//acm.uva.es/p/v101/10196.html
  • http//acm.uva.es/p/v101/10142.html

49
3n1 Problem
  • The 3n1 problem
  • http//acm.uva.es/p/v1/100.html
  • This is an outstanding unsolved problems in
    number theory, called 3n1 conjecture.
  • Start with an integer n
  • If n is even, nn/2 else n 3n1
  • Repeat this process, terminating when n1.
  • Its conjectured that this algorithm will
    terminate at n1 for every integer n.
  • E.g.
  • 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
  • The cycle length for 22 is 16.

50
3n1 problem
  • Write a function to calculate the maximum cycle
    for integers between and including i and j
  • Write a function to calculate the cycle length
    for integer i

void solve(int i, int j) int t, m, n int
length, maxlength if( i lt j) m i
n j else m j n i
maxlength 1 for(t m t lt n t)
length cycle_length(t) if (length gt
maxlength) maxlength length
printf("d d d\n", i, j, maxlength)
int cycle_length(int i) int length 1
while (i ! 1) if( i 2 0) i
igtgt1 else i iltlt1 i 1
length return length
51
The trip
  • http//acm.uva.es/p/v101/10137.html
  • A number of students are members of a club that
    travels annually to exotic locations.
  • The group agrees in advance to share expenses
    equally, but it is not practical to have them
    share every expense as it occurs. So individuals
    in the group pay for particular things, like
    meals, hotels, taxi rides, plane tickets, etc.
  • After the trip, each student's expenses are
    tallied and money is exchanged so that the net
    cost to each is the same, to within one cent. In
    the past, this money exchange has been tedious
    and time consuming.
  • Your job is to compute, from a list of expenses,
    the minimum amount of money that must change
    hands in order to equalize (within a cent) all
    the students' costs.

52
The trip (Cont.)
  • The Input
  • Standard input will contain the information for
    several trips. There are no more than 1000
    students and no student spent more than
    10,000.00. A single line containing 0 follows
    the information for the last trip.
  • The Output
  • For each trip, output a line stating the total
    amount of money, in dollars and cents, that must
    be exchanged to equalize the students' costs.

Sample Input 4 15.00 15.01 3.00 3.01 0 Output
11.99
Sample Input 3 10.00 20.00 30.00 0 Output 10.00
53
The trip (Cont.)
  • Its important to analyze the problem first.
  • After you find the optimal way which can achieve
    the minimum amount of money that must change
    hands, writing the program will be very easy.
  • Whats the difficulty of this problem?
  • We need to equalize the costs as much as
    possible. That means
  • if A pays the most, and A pays PA if B pays the
    least, and B pays PB,
  • We need to have PB lt PA lt PB1

54
The trip (Cont.)
  • Assume there are n students. We can calculate the
    total money.
  • If the total money is a multiple of n, then we
    can equalize the costs. The problem is easy to
    solve.
  • Example 1 10.00, 20.00, 30.00
  • Total 60.00, average 20.00
  • The first student receives 10.00 from the third
    student
  • The second student doesnt need to do anything.
  • The answer will be 10.00

55
The trip (Cont.)
  • If the total money is not a multiple of n
  • Let start from an example
  • 1.10, 2.10, 3.10, 10.00
  • Total 16.30 4x4.07 0.02
  • The final cost should be 4.07, 4.07, 4.08, 4.08
  • To minimize the amount of money change
  • 1.10 ? 4.07 2.10 ? 4.07 3.10 ? 4.08 10.00
    ?4.08
  • The answer will be 5.92
  • Question do we need to sort the cost?
Write a Comment
User Comments (0)
About PowerShow.com