SB113 Course Overview Risk, Resource and Opportunity Marc Compeau – 8/23/04.
Loops in Go - Phillip Compeau, Carnegie...
Transcript of Loops in Go - Phillip Compeau, Carnegie...
![Page 1: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/1.jpg)
Loops in Go 02-201!
![Page 2: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/2.jpg)
Arranging Objects
Number of ways to arrange n items:!
1! 2! 3! 4! 5!2! 3 ! 4! 5!1!
2! 3! 4! 5!1!
…!
n choices for the first item !n - 1 choices for the second item!n - 2 choices for the third item!…!
⇒ n(n-1)(n-2)…(2)(1) = n!!
![Page 3: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/3.jpg)
Permutations & Combinations
P(n, k): number of ways to order k out of n items: !
2! 3! 4! 5!1! n choices for the first item!n - 1 choices for the second item!n - 2 choices for the third item!…!⇒ n(n-1)(n-2) … (n – k + 1)! = n!/(n-k)!!
![Page 4: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/4.jpg)
Permutations & Combinations
Number of ways to choose k items from a set of n items:!
2! 3! 4! 5!1!
n!/(n - k)! ways of ordering the k items in the box!k! equivalent orderings of the items that fall in the box !
![Page 5: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/5.jpg)
Coding Permutations & Combinations in Go
Warm-Up: Write the following functions in Go (use subroutines wherever possible). 1. Fact (n), computing n! = n * (n-1) * ... * 1 (last time)
2. Perm(n, k), computing P(n, k) = n! / (n-k)!
3. Comb(n, k), computing C(n, k) = n! / ((n-k)! * k!)
![Page 6: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/6.jpg)
Last Time: The First Nontrivial Algorithm
// Gcd(a,b) computes the greatest common // divisor of a and bfunc Gcd(a int, b int) int {
if a == b {return a
} else if a > b {return Gcd(a - b, b)
} else {return Gcd(a, b - a)
}}
Theorem: If a > b, then gcd(a, b) = gcd(a – b, b).
![Page 7: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/7.jpg)
Nimmy’s Idea: A Non-Recursive GCD Function
We need some way of expressing “while” in Go!
AnotherGcd(a,b)whilea!=b ifb>a b=b-a else a=a-breturna
![Page 8: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/8.jpg)
Nimmy’s Idea: A Non-Recursive GCD Function
func AnotherGcd(a, b int) int {for a != b {
if b > a {b = b-a
} else {a = a-b
}}return a
}
In Go, we use “for” to mean “while”
![Page 9: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/9.jpg)
Translating Gauss to Go
Gauss(n)sumß0iß1whilei<=nsumßsum+iißi+1returnsum
Exercise: Convert this pseudocode to Go.
![Page 10: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/10.jpg)
Translating Gauss to Go
func Gauss(n int) int {var sum int = 0var i int = 1for i<=n {
sum = sum + ii = i+1
}return sum
}
![Page 11: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/11.jpg)
Translating Gauss to Go
We need some way of expressing “for” in Go!
AnotherGauss(n)sumß0foriß1tonsumßsum+ireturnsum
![Page 12: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/12.jpg)
Translating Gauss to Go
func AnotherGauss(n int) int {sum := 0for i:=1; i<=n; i=i+1 {
sum = sum + i}return sum
}
Initialization statement: executed once before the loop starts
The condition: loop continues until this is false.
post-iteration statement: executed after each time through the loop.
![Page 13: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/13.jpg)
Quick Note
func AnotherGauss(n int) int {sum := 0for i:=1; i<=n; i++ {
sum += i}return sum
}
“i++” is equivalent to “i = i+1”
“sum += i” is equivalent to “sum = sum + i”
![Page 14: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/14.jpg)
Translating Fibonacci to Go
Fibonacci(n)a,b,cß1,1,1foriß3ton cßa+b aßb bßcreturnb
Exercise: Convert this pseudocode to Go.
![Page 15: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/15.jpg)
Translating Fibonacci to Go
func Fibonacci(n int) int {var a, b, c int = 1, 1, 1for i:=3; i<=n; i++ {
c = a + ba = bb = c
}return b
}
![Page 16: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/16.jpg)
Think Fast: What is the Difference?
// returns sum of first n positive integers func Gauss(n int) int {
var sum int = 0var i int = 1for i<=n {
sum = sum + ii = i + 1
}return sum
}
// returns sum of first n positive integers func AnotherGauss(n int) int {
var sum int = 0for i:=1; i<=n; i=i+1; {
sum = sum + i}return sum
}
![Page 17: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/17.jpg)
Think Fast: What is the Difference?
// returns sum of first n positive integers func Gauss(n int) int {
var sum int = 0var i int = 1for i<=n {
sum = sum + ii = i + 1
} fmt.Println(i)return sum
}
// returns sum of first n positive integers func AnotherGauss(n int) int {
var sum int = 0for i:=1; i<=n; i=i+1; {
sum = sum + i}return sum
}
i variable has limited scope
i variable still exists here
![Page 18: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/18.jpg)
Autolab Homework Grading
Correct? (75%)!
Style (25%)!
![Page 19: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/19.jpg)
Style Guidelines – Variables (5%)
Do your variables follow proper naming convention? • Camel case • Don’t name a variable the same thing as a function
![Page 20: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/20.jpg)
Style Guidelines – Variables (5%)
Do your variables follow proper naming convention? • Camel case • Don’t name a variable the same thing as a function
Do your variables fit into the proper scope? • Avoid “global variables” whose scope is too great
func Gauss(n int) int {var sum int = 0 // OKvar i int = 1 // No purpose for thisfor i<=n {
sum = sum + ii = i + 1
}return sum
}
![Page 21: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/21.jpg)
Style Guidelines – Functions/Modularity (10%)
Is your program partitioned reasonably into functions? • Does each function accomplish a single task? • Are functions reusable in other contexts?
![Page 22: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/22.jpg)
Style Guidelines – Functions/Modularity (10%)
Is your program partitioned reasonably into functions? • Does each function accomplish a single task? • Are functions reusable in other contexts? Does the input/output of your functions make sense? • Don’t take in more inputs or return more outputs than
you need. • Start with algorithms on the level of pseudocode, and
it will be easier to keep track of function input/output.
![Page 23: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/23.jpg)
Style Guidelines – Comments (5%)
Include your name and date at the top of the file. Does each function have a comment introducing it and explaining what it does? Have you provided comments inside functions to provide additional details as needed?
// This is John Doe’s HW1 assignment.// Due September 16, 2015
//Compute sum of first n integers.func Gauss(n int) int { ...
![Page 24: Loops in Go - Phillip Compeau, Carnegie Melloncompeau.cbd.cmu.edu/wp-content/uploads/2016/08/lec_6.pdf · Nimmy’s Idea: A Non-Recursive GCD Function We need some way of expressing](https://reader033.fdocuments.net/reader033/viewer/2022043018/5f3a50b4ca228c6c8a2314b7/html5/thumbnails/24.jpg)
Style Guidelines – Efficiency (5%)
Does your program represent a reasonably efficient solution to the problem? • Tip: you may accumulate old, unused code; make
sure to trim it from your program.