loops in go - phillip compeau, carnegie...
TRANSCRIPT
Loops in Go 02-201!
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!!
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)!!
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 !
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!)
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).
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
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”
Translating Gauss to Go
Gauss(n)sumß0iß1whilei<=nsumßsum+iißi+1returnsum
Exercise: Convert this pseudocode to Go.
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
}
Translating Gauss to Go
We need some way of expressing “for” in Go!
AnotherGauss(n)sumß0foriß1tonsumßsum+ireturnsum
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.
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”
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.
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
}
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
}
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
Autolab Homework Grading
Correct? (75%)!
Style (25%)!
Style Guidelines – Variables (5%)
Do your variables follow proper naming convention? • Camel case • Don’t name a variable the same thing as a function
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
}
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?
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.
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 { ...
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.