SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Stanford Splash Spring 2016
Basic Programming
Yu-Sheng (Yosen) Chen
chen.yosen@gmail.com
Self Introduction
Yosen 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, ...
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
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.
Basic Programming Concept
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/
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/
Control Flows (cont’)
● switch:
○ to check for a value among a number of possible constant expressions.
Reference http://www.cplusplus.com/doc/tutorial/control/
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
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:
3628800
3628800
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))
Basic Data Structures
Array vs. Linked-List
Array
Linked-List
Remove 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
Queue vs. Stack
Queue: 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
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
Let’s solve some coding
problems!
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, …)
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
= 24
x(2x5)6
= 24
x(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’s
min{8, 2} is 2.
2’s multiples are way more than 5’s multiples!
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…x28
Count: 1 1 1 1 2
Sum: 1 2 3 4 6 → 6 trailing zeros
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…x28
Count: 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 >=2
So 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 >=2
152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3
So the sum = 30+6+1 = 37.
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 fails
Return 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 >=2
152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3
So the sum = 30+6+1 = 37.
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 = 152
trailingZeros(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
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
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 pop
return false!( (( ( (( ((( (( (
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
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!
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=1
curr = 1+1 = 2
Copy pv to pvpv (1)
Copy curr to pv (2)
pv=2, pvpv=1
curr = 2+1 = 3
Copy pv to pvpv (2)
Copy curr to pv (3)
pv=3, pvpv=2
curr = 3+2 = 5
Copy pv to pvpv (3)
Copy curr to pv (5)
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)
Wrap Up
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.
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!!!
How to find this slides
Google: slideshare YosenChen, the first result!

Más contenido relacionado

La actualidad más candente

Rational Expressions
Rational ExpressionsRational Expressions
Rational Expressions
king_danickus
 
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial Fu...
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial  Fu...Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial  Fu...
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial Fu...
magnesium121
 
7.3 quadratic techniques
7.3 quadratic techniques7.3 quadratic techniques
7.3 quadratic techniques
Jessica Garcia
 
Simplex method
Simplex methodSimplex method
Simplex method
Abu Bashar
 

La actualidad más candente (20)

Ch08
Ch08Ch08
Ch08
 
Mathematics 8 Linear Functions
Mathematics 8 Linear FunctionsMathematics 8 Linear Functions
Mathematics 8 Linear Functions
 
Rational Expressions
Rational ExpressionsRational Expressions
Rational Expressions
 
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
Accurate, Simple and Efficient Triangulation of a Polygon by Ear Removal with...
 
Introduction to Functions
Introduction to FunctionsIntroduction to Functions
Introduction to Functions
 
Teaching Graphs of Polynomial Functions
Teaching Graphs of Polynomial FunctionsTeaching Graphs of Polynomial Functions
Teaching Graphs of Polynomial Functions
 
Prime
PrimePrime
Prime
 
Algebra 2 Section 4-6
Algebra 2 Section 4-6Algebra 2 Section 4-6
Algebra 2 Section 4-6
 
Polynomial function
Polynomial functionPolynomial function
Polynomial function
 
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial Fu...
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial  Fu...Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial  Fu...
Quotient of polynomial using (Synthetic Division) and Zeros of Polynomial Fu...
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
2LinearSequences
2LinearSequences2LinearSequences
2LinearSequences
 
Polynomial Function and Synthetic Division
Polynomial Function and Synthetic DivisionPolynomial Function and Synthetic Division
Polynomial Function and Synthetic Division
 
7.3 quadratic techniques
7.3 quadratic techniques7.3 quadratic techniques
7.3 quadratic techniques
 
Actividad resolución de problemas
Actividad resolución de problemasActividad resolución de problemas
Actividad resolución de problemas
 
Simplex algorithm
Simplex algorithmSimplex algorithm
Simplex algorithm
 
Long division, synthetic division, remainder theorem and factor theorem
Long division, synthetic division, remainder theorem and factor theoremLong division, synthetic division, remainder theorem and factor theorem
Long division, synthetic division, remainder theorem and factor theorem
 
Simplex method
Simplex methodSimplex method
Simplex method
 
This quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutletThis quiz is open book and open notes/tutorialoutlet
This quiz is open book and open notes/tutorialoutlet
 
Permutations and Combinations IIT JEE+Olympiad Lecture 1
Permutations and Combinations IIT JEE+Olympiad Lecture 1 Permutations and Combinations IIT JEE+Olympiad Lecture 1
Permutations and Combinations IIT JEE+Olympiad Lecture 1
 

Destacado

Resume_Yilun Chong_EN
Resume_Yilun Chong_ENResume_Yilun Chong_EN
Resume_Yilun Chong_EN
Yilun Chong
 
Wei_Zhao_Resume
Wei_Zhao_ResumeWei_Zhao_Resume
Wei_Zhao_Resume
Wei Zhao
 
CVLinkedIn
CVLinkedInCVLinkedIn
CVLinkedIn
Jun Ma
 
Zejia_CV_final
Zejia_CV_finalZejia_CV_final
Zejia_CV_final
ZJ Zheng
 
Xiaoli_Ma_developer_resume
Xiaoli_Ma_developer_resumeXiaoli_Ma_developer_resume
Xiaoli_Ma_developer_resume
Xiaoli Ma
 
CV_Shilidong
CV_ShilidongCV_Shilidong
CV_Shilidong
?? ?
 

Destacado (20)

Lichang Wang_CV
Lichang Wang_CVLichang Wang_CV
Lichang Wang_CV
 
Resume_Yilun Chong_EN
Resume_Yilun Chong_ENResume_Yilun Chong_EN
Resume_Yilun Chong_EN
 
Wei_Zhao_Resume
Wei_Zhao_ResumeWei_Zhao_Resume
Wei_Zhao_Resume
 
Cv 12112015
Cv 12112015Cv 12112015
Cv 12112015
 
dpdp
dpdpdpdp
dpdp
 
詹剑锋:Big databench—benchmarking big data systems
詹剑锋:Big databench—benchmarking big data systems詹剑锋:Big databench—benchmarking big data systems
詹剑锋:Big databench—benchmarking big data systems
 
CVLinkedIn
CVLinkedInCVLinkedIn
CVLinkedIn
 
CV-YacineRhalmi
CV-YacineRhalmiCV-YacineRhalmi
CV-YacineRhalmi
 
Sara Saile cv
Sara Saile cv Sara Saile cv
Sara Saile cv
 
Zejia_CV_final
Zejia_CV_finalZejia_CV_final
Zejia_CV_final
 
Xiaoli_Ma_developer_resume
Xiaoli_Ma_developer_resumeXiaoli_Ma_developer_resume
Xiaoli_Ma_developer_resume
 
CV_Shilidong
CV_ShilidongCV_Shilidong
CV_Shilidong
 
Hung DO-DUY - Spikenet
Hung DO-DUY - Spikenet Hung DO-DUY - Spikenet
Hung DO-DUY - Spikenet
 
台湾趴趴走
台湾趴趴走台湾趴趴走
台湾趴趴走
 
Kafka文件系统设计
Kafka文件系统设计Kafka文件系统设计
Kafka文件系统设计
 
前端规范(初稿)
前端规范(初稿)前端规范(初稿)
前端规范(初稿)
 
Research Park: Year in Review 2014
Research Park: Year in Review 2014Research Park: Year in Review 2014
Research Park: Year in Review 2014
 
Dpdk Validation - Liu, Yong
Dpdk Validation - Liu, YongDpdk Validation - Liu, Yong
Dpdk Validation - Liu, Yong
 
周士云的简历
周士云的简历周士云的简历
周士云的简历
 
Introducing Ubuntu SDK
Introducing Ubuntu SDKIntroducing Ubuntu SDK
Introducing Ubuntu SDK
 

Similar a Stanford splash spring 2016 basic programming

how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
DebiPrasadSen
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
chidabdu
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
Low Ying Hao
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
Abbas Ali
 

Similar a Stanford splash spring 2016 basic programming (20)

tutorial5.ppt
tutorial5.ppttutorial5.ppt
tutorial5.ppt
 
Course project solutions 2019
Course project solutions 2019Course project solutions 2019
Course project solutions 2019
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
P1-Chp13-Integration.pptx
P1-Chp13-Integration.pptxP1-Chp13-Integration.pptx
P1-Chp13-Integration.pptx
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
Algorithms required for data structures(basics like Arrays, Stacks ,Linked Li...
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
1 chapter1 introduction
1 chapter1 introduction1 chapter1 introduction
1 chapter1 introduction
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
 
Insider mathematical
Insider   mathematicalInsider   mathematical
Insider mathematical
 
mathematics o level book
mathematics o level bookmathematics o level book
mathematics o level book
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
 
Algorithm And analysis Lecture 03& 04-time complexity.
 Algorithm And analysis Lecture 03& 04-time complexity. Algorithm And analysis Lecture 03& 04-time complexity.
Algorithm And analysis Lecture 03& 04-time complexity.
 
lec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.pptlec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.ppt
 
lec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.pptlec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.ppt
 
Lecture02
Lecture02Lecture02
Lecture02
 
Daa unit 1
Daa unit 1Daa unit 1
Daa unit 1
 

Último

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 

Stanford splash spring 2016 basic programming

  • 1. Stanford Splash Spring 2016 Basic Programming Yu-Sheng (Yosen) Chen chen.yosen@gmail.com
  • 2. Self Introduction Yosen 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, ...
  • 3. 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
  • 4. 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.
  • 6. 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/
  • 7. 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/
  • 8. Control Flows (cont’) ● switch: ○ to check for a value among a number of possible constant expressions. Reference http://www.cplusplus.com/doc/tutorial/control/
  • 9. 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
  • 10. 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: 3628800 3628800
  • 11. 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))
  • 13. Array vs. Linked-List Array Linked-List Remove 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
  • 14. Queue vs. Stack Queue: 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
  • 15. 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
  • 16. Let’s solve some coding problems!
  • 17. 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, …)
  • 18. 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 = 24 x(2x5)6 = 24 x(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’s min{8, 2} is 2. 2’s multiples are way more than 5’s multiples!
  • 19. 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…x28 Count: 1 1 1 1 2 Sum: 1 2 3 4 6 → 6 trailing zeros
  • 20. 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…x28 Count: 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 >=2 So 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 >=2 152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3 So the sum = 30+6+1 = 37.
  • 21. 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 fails Return 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 >=2 152/125 = 1...27, which means among this 6 multiples, 1 of them has exponent of 5 >=3 So the sum = 30+6+1 = 37.
  • 22. 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 = 152 trailingZeros(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
  • 23. 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
  • 24. 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 pop return false!( (( ( (( ((( (( (
  • 25. 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
  • 26. 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!
  • 27. 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=1 curr = 1+1 = 2 Copy pv to pvpv (1) Copy curr to pv (2) pv=2, pvpv=1 curr = 2+1 = 3 Copy pv to pvpv (2) Copy curr to pv (3) pv=3, pvpv=2 curr = 3+2 = 5 Copy pv to pvpv (3) Copy curr to pv (5)
  • 28. 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)
  • 30. 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.
  • 31. 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!!!
  • 32. How to find this slides Google: slideshare YosenChen, the first result!