Week 1, Day 3

Videos

  1. Part 1: Review, Go → Javascript, Gravatar
  2. Part 2: Stderr
  3. Part 3: Gravatar Profiles cont'd, Go Tour, Word Count
  4. Part 4: Word Count solution
  5. Part 5: Count Clumps
  6. Part 6: Count Clumps cont'd
  7. Part 7: Pointers
  8. Part 8: Pointers cont'd
  9. Part 9: Swap, Rotate,
  10. Part 10: Structs
  11. Part 11: Structs cont'd
  12. Part 12: Embedded Types, Interfaces
  13. Part 13: Interfaces cont'd

Outline

  1. Review

    • Create Javascript Cheat Sheet: is.gd/XaJQal.
    • Pair-program on problems

    Problems

    1. Create a program which reads user information (at least name and email) and creates a profile page in HTML. Also use or generate a profile image from gravatar.com.
    2. Implement WordCount. It should return a map of the counts of each “word” in a string. (You might find strings.Fields helpful.) WordCount("test test")map[string]int{ "test": 2 }
    3. Implement a centeredAverage function that computes the average of a list of numbers, but removes the largest and smallest values. centeredAverage([]float64{1, 2, 3, 4, 100})3
    4. Given a non-empty list, return true if there is a place to split the list so that the sum of the numbers on one side is equal to the sum of the numbers on the other side.
      canBalance([]int{1, 1, 1, 2, 1})true
      canBalance([]int{2, 1, 1, 2, 1})false
      canBalance([]int{10, 10})true
    5. Say that a "clump" in a list is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given list.
      countClumps([]int{1, 2, 2, 3, 4, 4})2
      countClumps([]int{1, 1, 2, 1, 1})2
      countClumps([]int{1, 1, 1, 1, 1})1
  2. Pointers

    • * and &
    • new
    • Scan

    Problems

    1. How do you get the memory address of a variable?
    2. How do you assign a value to a pointer?
    3. How do you create a new pointer?
    4. What is the value of x after running this program:
      func square(x *float64) {
      	*x = *x * *x
      }
      func main() {
      	x := 1.5
      	square(&x)
      }
      
    5. Write a program that can swap two integers ( x := 1; y := 2; swap(&x, &y) should give you x==2 and y==1).
    6. Generalize swap into a rotate function so that it works with any number of variables: rotate(&x, &y, &z), with x=1, y=2, y=3, yields x=2, y=3, z=1.
    7. Create a rotateRight function which does the same thing as rotate but in the other direction.
  3. Structs and Interfaces

    • Structs
    • Initialization
    • Fields
    • Methods
    • Embedded Types

    Problems

    1. What's the difference between a method and a function?
    2. Why would you use an embedded anonymous field instead of a normal named field?
    3. Add a new method to the Shape interface called perimeter which calculates the perimeter of a shape. Implement the method for Circle and Rectangle.