Top Banner
Stanford Splash Spring 2016 Basic Programming Yu-Sheng (Yosen) Chen [email protected]
32

Stanford splash spring 2016 basic programming

Jan 13, 2017

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stanford splash spring 2016 basic programming

Stanford Splash Spring 2016

Basic ProgrammingYu-Sheng (Yosen) [email protected]

Page 2: Stanford splash spring 2016 basic programming

Self IntroductionYosen Chen

● 1st year Master’s student in Stanford ICME (Institute for Computational & Math Engineering). Got an EE Master’s degree in Taiwan in 2010.

● Will be a Facebook software intern this summer● 5 years of industry working experience in Taiwan (software/algorithm

engineer)● 2 years of research experience in Taiwan (computer vision)● Publications: 3 camera patents (pending), 1 computer vision patent, 1 best

paper in IEEE ITC.● Programming experience

○ C/C++ (7+ years), Python, Matlab, Perl, Java, ...

Page 3: Stanford splash spring 2016 basic programming

Why Programming?

● High salary?● Change the way people communicate/work/build relationships?● Redefine how a device can play a role in people’s lives?● The world is flat: Everyone can learn programming once they have access to

the internet!

Google services

FacebookFrom glassdoor.com

Page 4: Stanford splash spring 2016 basic programming

What Can You Learn from This Lecture?

● Basic programming concept (control flows, functions, iterative, recursive, ...)● Basic data structure concept: what tools can we use to deal with problems● Basic complexity concept: how much work does it take? how many times of

basic operations, ex:○ 10*100=1000, less than 10 operations○ 10+10+10+...+10=1000, at least 100 operations

● Be able to deal with basic programming problems● How to learn on your own (Programming learning never ends!!)

Note that in this lecture, I will only use C++, but the concepts are generally the same for all languages.

Page 5: Stanford splash spring 2016 basic programming

Basic Programming Concept

Page 6: Stanford splash spring 2016 basic programming

Control Flows● if/else:

○ if (condition) statement1 else statement2 ○

● While:○ while (condition) statement○ ○

Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,

What if we write like this: while(true) { /*do something*/ } → it will never end unless we add some “break;” inside the loop body.

Reference: http://www.cplusplus.com/doc/tutorial/control/

Page 7: Stanford splash spring 2016 basic programming

Control Flows (cont’)● for:

○ for (initialization①; condition②; increase③) {statement④;}○ How it works

■ A. initialization is executed. This is executed a single time, at the beginning of the loop.■ B. condition is checked. If it is true, the loop continues; otherwise, the loop ends.■ C. statement(s) is executed.■ D. Increase (or decrease) is executed, then the loop gets back to step B.

○ It goes like: ■ Start → ①②{④③②}{④③②}{④③②}...{④③②} → End■ Start → ①② → End

○ for (;;) is identical to while(1)

Reference http://www.cplusplus.com/doc/tutorial/control/

Page 8: Stanford splash spring 2016 basic programming

Control Flows (cont’)● switch:

○ to check for a value among a number of possible constant expressions.

Reference http://www.cplusplus.com/doc/tutorial/control/

Page 9: Stanford splash spring 2016 basic programming

Functions● A function is a group of statements that together perform a task. Every C++

program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions.

● A function can have its input(s) and output(s)● Functions can call other functions, including itself (recursive calls)● We can create variables inside the functions, but they will be released when

we exit from the function.Return_type Function_name (input1, input2, …, inputN){

Statement1; Statement2; ...return Output;

}

Reference http://www.tutorialspoint.com/cplusplus/cpp_functions.htm

Page 10: Stanford splash spring 2016 basic programming

Recursive vs. Iterative Method● Recursive function: a function who calls itself.

○ It will recursively call itself until it reaches some stopping condition (usually its base case)

● Iterative method uses a loop structure (while, for), it terminates when loop condition fails.

● Let’s see how to solve a problem in both way. Problem: 1*2*3*...*n = ?

Execution result:36288003628800

Page 11: Stanford splash spring 2016 basic programming

Complexity ( the smaller, the better)● Complexity: What is the order of the number of basic operations? ● Given an input with size N, how many basic operations will be executed in this

function/algorithm? A measure of algorithm efficiency.● Let’s see some examples

○ Calculate 1+2+3+...+N = ?■ Naive method: 1+2+3+...+N, Complexity: (N-1) additions.■ Better method: (1+N)*N/2, Complexity: 1 add, 1 div, and 1 mult.

○ Is N a prime number?

■ Naive method: check if all the remainders of N/k, where k = 2, 3, 4, ... N-1, are nonzero, then it’s a prime number. Complexity: N-2 divisions

■ Better method: check if all the remainders of N/k, where k=2, 3, 4, … sqrt(N), are nonzero, then it’s a prime number. Complexity: sqrt(N) -1 divisions

Note: A composite number N is a positive integer which is not prime (i.e., which has factors other than 1 and itself, at least one factor <= sqrt(N))

Page 12: Stanford splash spring 2016 basic programming

Basic Data Structures

Page 13: Stanford splash spring 2016 basic programming

Array vs. Linked-List

Array

Linked-ListRemove element

Insert element

Picture credit: java67.blogspot.com https://en.wikipedia.org/wiki/Linked_list

Array Linked-List

Adv. random access, no extra memory

Distributed memory; fast insert, remove, resize

Disadv.

Slow insert, remove, resize iterating access; need memory to store next address

Page 14: Stanford splash spring 2016 basic programming

Queue vs. StackQueue: First in first out (operations: enqueue, dequeue, see back/front)

Stack: First in last out, like crowded caltrain or elevators (operations: push/pop, see top)

Picture credit: En.wikipedia.org www.creativebloq.com quincypublicschools.com

Page 15: Stanford splash spring 2016 basic programming

More!

● Trees

● Hash table

Picture credit: Collegelabs.co http://www.tutorialspoint.com/data_structures_algorithms/tree_data_structure.htm

class TreeNode{

int data;TreeNode* leftChild;TreeNote* rightChild;

};

By Jorge Stolfi - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=6471238

A small phone book as a hash table

The beauty of hash table is search/insert/delete in constant time, regardless of the size of the table

Page 16: Stanford splash spring 2016 basic programming

Let’s solve some coding problems!

Page 17: Stanford splash spring 2016 basic programming

Let’s See What Problems We Can Solve Now!

● Surprisingly, we now are able to solve some real interview questions!!● Problem#1: Number of the trailing zeros in a factorial● Problem#2: valid parenthesis expression → symmetricity problem● Problem#3: Fibonacci sequence● All the above have been asked in real job interviews. (software engineer, data

scientist, trader, …)

Page 18: Stanford splash spring 2016 basic programming

Problem#1: Num of Trailing Zeros

● Given an integer n, return the number of trailing zeroes in n!● Analysis:

○ Don’t try to calculate the n factorial (it could be very large, overflow!)○ Idea A: number of trailing zeros = min{p, q} where p and q are the exponent numbers of

2p and 5q in n!’s prime factor decomposition. ○ Ex: 210*56 = 24x(2x5)6 = 24x(10)6 → 6 trailing zeros○ So we need to count the 2’s and 5’s exponent numbers for all numbers between 1 and

n?○ Actually, we just have to count 5’s exponents, since num of 5’s is less than 2’s

EX: 10!1x2x3x4x5x6x7x8x9x10, we have 1+2+1+3+1=8 2’s, but only 2 5’smin{8, 2} is 2.2’s multiples are way more than 5’s multiples!

Page 19: Stanford splash spring 2016 basic programming

Problem#1: Num of Trailing Zeros (cont’)

● So, how to count the number of 5’s?● Idea B: for i=1 to n, we accumulate the exponents of 5 in i’s prime factor

decomposition

Convert this into code:

EX: 28!1x2x3x4x5x6x7x8x9x10x...x15x...x20x...x25x…x28Count: 1 1 1 1 2Sum: 1 2 3 4 6 → 6 trailing zeros

Page 20: Stanford splash spring 2016 basic programming

Problem#1: Num of Trailing Zeros (cont’)In fact, we have a better way to do it (no need to iterate all number from 1 to n)

Idea C: EX: 28!1x2x3x4x5x6x7x8x9x10x...x15x...x20x...x25x…x28Count: 1 1 1 1 2 Sum = 6 → 6 trailing zeros

28/5 = 5 … 3, which means among 1 to 28, 5 of them are 5’s multiples (exponent of 5 >=1)28/25 = 1 … 3, which means among this 5 multiples, 1 of them have exponent of 5 >=2So the sum = 5 + 1 = 6.

One more example, EX: 152!1x...x5x...x10x...x15x...x20x...x25x…x50x...x100x...x125x...x150x151x152 1 1 1 1 2 2 2 3 2

152/5 = 30 … 2, which means among 1 to 152, 30 of them are 5’s multiples (exponent of 5 >=1)152/25 = 6 … 2, which means among this 30 multiples, 6 of them have exponent of 5 >=2152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3So the sum = 30+6+1 = 37.

Page 21: Stanford splash spring 2016 basic programming

Problem#1: Num of Trailing Zeros (cont’)Idea C:

Let’s convert it into code (iterative) i=5 cnt = 0 + 30

i=25 cnt = 30 + 6

i=125 cnt = 36 + 1

i=625 Condition failsReturn cnt=37

One more example, EX: 152!1x...x5x...x10x...x15x...x20x...x25x…x50x...x100x...x125x...x150x151x152 1 1 1 1 2 2 2 3 2

152/5 = 30 … 2, which means among 1 to 152, 30 of them are 5’s multiples (exponent of 5 >=1)152/25 = 6 … 2, which means among this 30 multiples, 6 of them have exponent of 5 >=2152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3So the sum = 30+6+1 = 37.

Page 22: Stanford splash spring 2016 basic programming

Problem#1: Num of Trailing Zeros (cont’)Interpret idea C into a recursive method (only 1 line!)

Complexity comparison between Idea A, idea B, Idea C

Ex: n = 152trailingZeros(152) = 152/5(=30) + trailingZeros(30);trailingZeros(30) = 30/5(=6) + trailingZeros(6);trailingZeros(6) = 6/5(=1) + trailingZeros(1);trailingZeros(1) = 1/5(=0) + trailingZeros(0);trailingZeros(0) = 0; so finally we have 30+6+1+0+0=37

Idea A Idea B Idea C

Details Count both exp of 2 and 5, find min of them

Only count exp of 5 Count exp of 5 in a clever way

Complexity Double of Idea B’s Iterate every number from 1 to n

Iterate only 5, 25, 125, …, n

Page 23: Stanford splash spring 2016 basic programming

Problem#2: Valid Parenthesis Expression

● A good example to make use of stack● Given a string, tell if it’s a valid parenthesis expression (all are paired properly)

○ () → OK○ (()) → OK○ (()()(())) → OK○ ()( → X○ ))(( → X○ (()()()()()()()()()()()()()()()()()()()()()()())) → X

● Solution: use a stack to deal with all paired parenthesis, if finally the stack is not empty, then it’s not a valid expression

Page 24: Stanford splash spring 2016 basic programming

Problem#2: Valid Parenthesis Expression (cont’)● Solution:

○ Iterate through the input string, from the first characters to the last one■ If we see a ‘(’, then push it into the stack (waiting for being paired)■ If we see a ‘)’, then check the top of the stack,

● if it’s a ‘(’, good, then pop it, which means the incoming ‘)’ is paired with a ‘(’● Otherwise (i.e., stack is empty), the incoming ‘)’ cannot be paired, return false

○ In the end ■ If the stack is not empty, then it means some are not paired, return false■ If the stack is empty, then all the parentheses are paired, return true

Let’s see an example: ( ( ) ( ( ) ) ) )

iter# 1: ( 2: ( 3: ) 4: ( 5: ( 6: ) 7: ) 8: ) 9: )

Push ( Push ( Pop ( Push ( Push ( Pop ( Pop ( Pop ( Empty, can’t popreturn false!( (( ( (( ((( (( (

Page 25: Stanford splash spring 2016 basic programming

Problem#2: Valid Parenthesis Expression (cont’)

Convert it into code● Iterate through the input string, from the first

characters to the last one○ If we see a ‘(’, then push it into the

stack (waiting for being paired)○ If we see a ‘)’, then check the top of

the stack, ■ if it’s a ‘(’, good, then pop it,

which means the incoming ‘)’ is paired with a ‘(’

■ Otherwise (i.e., stack is empty), the incoming ‘)’ cannot be paired, return false

● In the end ○ If the stack is not empty, then it means

some are not paired, return false○ If the stack is empty, then all the

parentheses are paired, return true

Page 26: Stanford splash spring 2016 basic programming

Problem#3: Fibonacci Sequence● Fibonacci sequence, F(1)=F(2)=1; F(n) = F(n-1) + F(n-2)● 1,1,2,3,5,8,13,21,34,55,89,144,.., how to write a function to find F(n) = ?● Idea A: Intuitively, we can convert it into recursive functions

In fact, it’s very inefficient!Lots of redundancy!EX: Why should we calculate F(3) 3 times and F(4) 2 times, and many F(1)’s and F(2)’s.In fact, F(n) has about 1.6n recursive calls!

Page 27: Stanford splash spring 2016 basic programming

Problem#3: Fibonacci Sequence (cont’)

● Another approach to Fibonacci Sequence● Idea B: for each iteration, we sum prev and prevprev as curr, then before next

iteration, we copy prev’s value to prevprev, and curr to prev● Convert into iterative code (iterate n-2 times)

1 1 2 3 5 8 13 ...

pv=1, pvpv=1curr = 1+1 = 2Copy pv to pvpv (1)Copy curr to pv (2)

pv=2, pvpv=1curr = 2+1 = 3Copy pv to pvpv (2)Copy curr to pv (3)

pv=3, pvpv=2curr = 3+2 = 5Copy pv to pvpv (3)Copy curr to pv (5)

Page 28: Stanford splash spring 2016 basic programming

Problem#3: Fibonacci Sequence (cont’)

● There are some even more advanced ideas!!● F(n) can be solved in nearly const time! (regardless of how large n is)● Involves Matrix computation and diagonalization! (topics in Colleges)

Page 29: Stanford splash spring 2016 basic programming

Wrap Up

Page 30: Stanford splash spring 2016 basic programming

Learning Programming is Free!!

● Some useful online resource○ https://www.codecademy.com/ (Learn to code interactively, recommended for beginners)○ https://codefights.com/home (Test your skills agains some company robots)

○ https://leetcode.com/ (LeetCode Online judges, and most important, find the best rated solutions in its “discuss” tab!)

○ http://www.cplusplus.com/ (A good website to consult with for any C++ standard function/library)

○ https://checkio.org/ (A good website to learn Python, like a RPG game)○ https://docs.python.org (A good website to consult with for any Python standard libs)

● It’s not hard to find resources to learn coding, the problem is how to find answers when you have problems.

○ Google it! It always works, except for using wrong keywords.

Page 31: Stanford splash spring 2016 basic programming

Any Advice for New Coders?● Be expert in one single language (strong, complete), then know basics in other 2-3

languages○ EX: like me, C++, Python, Matlab, Perl, Java, …○ At least be familiar with 1 compiled language and a script one.

● Be patient for debugging○ printing out all the values stage by stage is the dumbest, but most useful way.

● Learning never ends○ new standard, new libs, new problems, new fields, new languages…

● If you can tell a computer how to solve a problem, that means you really understand the solution. (because computers know nothing but only digits!)

● Programming is just an approach to solving a problem.○ It is the knowledge of the solutions that really make you irreplaceable! Not the

code itself!!!

Page 32: Stanford splash spring 2016 basic programming

How to find this slides

Google: slideshare YosenChen, the first result!