Week 1, Day 2

Videos

  1. Part 1: Review (variables, types)
  2. Part 2: Review (types, control structures)
  3. Part 3: Review (control structures, continue/break)
  4. Part 4: Strings Revisited
  5. Part 5: Miles to Kilometers
  6. Part 6: Distance Converter
  7. Part 7: Arrays, Slices, Maps
  8. Part 8: Maps
  9. Part 9: Slice and Map Problems
  10. Part 10: Functions
  11. Part 11: Functions
  12. Part 12: Functions
  13. Part 13: Closure, Defer, Recursion
  14. Part 14: Recursion
  15. Part 15: Recursion

Outline

  1. Strings Revisited

    • Immutability
    • strings
    • strconv
    • Command line arguments

    Problems

    1. Modify our miles to kilometers program to display in the following format:
      +-------------------------+
      | Miles: 50               |
      +-------------------------+
      | Kilometers: 80.47       |
      +-------------------------+
      
      Miles will be input by the user, and kilometers should be formatted to 2 decimal places.
    2. Modify the above program so that it generates HTML instead of text. For example:
      <!DOCTYPE html>
      <html>
      	<head></head>
      	<body>
      		Miles: 50<br>
      		Kilometers: 80.47
      	</body>
      </html>
      
      Feel free to use any HTML tags and CSS you may know.
    3. Create a program which parses a query to do distance conversions. For example, from a terminal:
    4. $ distance_converter 50mi km
      Should produce:
      80.47km
      It should support miles (mi), kilometers (km), feet (ft) and meters (m).
    5. One classic method for composing secret messages is called a square code. The spaces are removed from the english text and the characters are written into a square (or rectangle). For example, the sentence "If man was meant to stay on the ground god would have given us roots" is 54 characters long, so it is written into a rectangle with 7 rows and 8 columns.
      ifmanwas
      meanttos
      tayonthe
      groundgo
      dwouldha
      vegivenu
      sroots
      The coded message is obtained by reading down the columns going left to right. For example, the message above is coded as:
      imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
      In your program, have the user enter a message in english with no spaces between the words. Have the maximum message length be 81 characters. Display the encoded message. (Watch out that no "garbage" characters are printed.) Here are some more examples:
      Input                                 Output
      haveaniceday                          hae and via ecy
      feedthedog                            fto ehg ee  dd
      chillout                              clu hlt io
  2. Arrays, Slices and Maps

    • Arrays
    • Slices
    • Maps

    Problems

    1. How do you access the 4th element of an array or slice?
    2. What is the length of a slice created using: make([]int, 3, 9)?
    3. Given the array:
      x := [6]string{"a","b","c","d","e","f"}
      what would x[2:5] give you?
    4. Write a program that finds the smallest number in this list:
      x := []int{
      	48,96,86,68,
      	57,82,63,70,
      	37,34,83,27,
      	19,97, 9,17,
      }
    5. Write a program that takes in a state code and returns the state’s name. (eg CA -> California)
  3. Functions

    • Functions
    • Multiple Return Values
    • Variadic Functions
    • Closure
    • Recursion
    • Panic & Recover

    Problems

    1. sum is a function which takes a slice of numbers and adds them together. What would its function signature look like in Go?
    2. Implement the sum function
    3. Write a function which takes an integer and halves it and returns true if it was even or false if it was odd. For example half(1) should return (0, false) and half(2) should return (1, true).
    4. Write a function with one variadic parameter that finds the greatest number in a list of numbers.
    5. Using makeEvenGenerator as an example, write a makeOddGenerator function that generates odd numbers.
    6. The Fibonacci sequence is defined as:
      fib(0) = 0, fib(1) = 1, fib(n) = fib(n-1) + fib(n-2)
      . Write a recursive function which can find fib(n).
    7. What are defer, panic and recover? How do you recover from a run-time panic?
    8. Create a function which reverses a list of integers:
      reverse([]int{1,2,3}) → []int{3,2,1}