restructure: arrays and slices

This commit is contained in:
Inanc Gumus
2019-01-29 19:43:12 +03:00
parent 096ac9c251
commit c43d152d33
108 changed files with 92 additions and 243 deletions

View File

@ -0,0 +1,38 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// EXERCISE: Declare nil slices
//
// 1. Declare the following slices as nil slices:
//
// 1. The names of your friends (names slice)
//
// 2. The distances to locations (distances slice)
//
// 3. A data buffer (data slice)
//
// 4. Currency exchange ratios (ratios slice)
//
// 5. Up/Down status of web servers (alives slice)
//
//
// 2. Print their type, length and whether they're equal to nil value or not.
//
//
// EXPECTED OUTPUT
// names : []string 0 true
// distances: []int 0 true
// data : []uint8 0 true
// ratios : []float64 0 true
// alives : []bool 0 true
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,26 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
var (
names []string // The names of your friends
distances []int // The distances
data []byte // A data buffer
ratios []float64 // Currency exchange ratios
alives []bool // Up/Down status of web servers
)
fmt.Printf("names : %T %d %t\n", names, len(names), names == nil)
fmt.Printf("distances: %T %d %t\n", distances, len(distances), distances == nil)
fmt.Printf("data : %T %d %t\n", data, len(data), data == nil)
fmt.Printf("ratios : %T %d %t\n", ratios, len(ratios), ratios == nil)
fmt.Printf("alives : %T %d %t\n", alives, len(alives), alives == nil)
}

View File

@ -0,0 +1,41 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE: Assign empty slices
//
// Assign empty slices to all the slices that you've declared in the previous
// exercise, and print them here.
//
//
// EXPECTED OUTPUT
// names : []string 0 false
// distances: []int 0 false
// data : []uint8 0 false
// ratios : []float64 0 false
// alives : []bool 0 false
// ---------------------------------------------------------
func main() {
var (
names []string // The names of your friends
distances []int // The distances
data []byte // A data buffer
ratios []float64 // Currency exchange ratios
alives []bool // Up/Down status of web servers
)
fmt.Printf("names : %T %d %t\n", names, len(names), names == nil)
fmt.Printf("distances: %T %d %t\n", distances, len(distances), distances == nil)
fmt.Printf("data : %T %d %t\n", data, len(data), data == nil)
fmt.Printf("ratios : %T %d %t\n", ratios, len(ratios), ratios == nil)
fmt.Printf("alives : %T %d %t\n", alives, len(alives), alives == nil)
}

View File

@ -0,0 +1,32 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
var (
names []string // The names of your friends
distances []int // The distances
data []byte // A data buffer
ratios []float64 // Currency exchange ratios
alives []bool // Up/Down status of web servers
)
names = []string{}
distances = []int{}
data = []byte{}
ratios = []float64{}
alives = []bool{}
fmt.Printf("names : %T %d %t\n", names, len(names), names == nil)
fmt.Printf("distances: %T %d %t\n", distances, len(distances), distances == nil)
fmt.Printf("data : %T %d %t\n", data, len(data), data == nil)
fmt.Printf("ratios : %T %d %t\n", ratios, len(ratios), ratios == nil)
fmt.Printf("alives : %T %d %t\n", alives, len(alives), alives == nil)
}

View File

@ -0,0 +1,63 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE: Assign slice literals
//
// 1. Assign the following data using slice literals to the slices that
// you've declared in the first exercise.
//
// 1. The names of your best three friends (to the names slice)
//
// 2. The distances to five different locations (to the distances slice)
//
// 3. Five bytes of data (to the data slice)
//
// 4. Two currency exchange ratios (to the ratios slice)
//
// 5. Up/Down status of four different web servers (to the alives slice)
//
// 2. Print their type, length and whether they're equal to nil value or not
//
// 3. Compare the length of the distances and the data slices; print a message
// if they are equal (use an if statement).
//
//
// EXPECTED OUTPUT
// names : []string 3 false
// distances: []int 5 false
// data : []uint8 5 false
// ratios : []float64 2 false
// alives : []bool 4 false
// The length of the distances and the data slices are the same.
// ---------------------------------------------------------
func main() {
var (
names []string // The names of your friends
distances []int // The distances
data []byte // A data buffer
ratios []float64 // Currency exchange ratios
alives []bool // Up/Down status of web servers
)
names = []string{}
distances = []int{}
data = []byte{}
ratios = []float64{}
alives = []bool{}
fmt.Printf("names : %T %d %t\n", names, len(names), names == nil)
fmt.Printf("distances: %T %d %t\n", distances, len(distances), distances == nil)
fmt.Printf("data : %T %d %t\n", data, len(data), data == nil)
fmt.Printf("ratios : %T %d %t\n", ratios, len(ratios), ratios == nil)
fmt.Printf("alives : %T %d %t\n", alives, len(alives), alives == nil)
}

View File

@ -0,0 +1,36 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
var (
names []string // The names of your friends
distances []int // The distances
data []byte // A data buffer
ratios []float64 // Currency exchange ratios
alives []bool // Up/Down status of web servers
)
names = []string{"serpil", "ebru", "lina"}
distances = []int{100, 200, 300, 400, 500}
data = []byte{'I', 'N', 'A', 'N', 'C'}
ratios = []float64{3.14, 6.28}
alives = []bool{true, false, false, true}
fmt.Printf("names : %T %d %t\n", names, len(names), names == nil)
fmt.Printf("distances: %T %d %t\n", distances, len(distances), distances == nil)
fmt.Printf("data : %T %d %t\n", data, len(data), data == nil)
fmt.Printf("ratios : %T %d %t\n", ratios, len(ratios), ratios == nil)
fmt.Printf("alives : %T %d %t\n", alives, len(alives), alives == nil)
if len(distances) == len(data) {
fmt.Println("The length of the distances and the data slices are the same.")
}
}

View File

@ -0,0 +1,44 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
// ---------------------------------------------------------
// EXERCISE: Declare the arrays as slices
//
// 1. First run the following program as it is
//
// 2. Then change the array declarations to slice declarations
//
// 3. Observe whether anything changes or not (on the surface :)).
//
// EXPECTED OUTPUT
// names : []string ["Einstein" "Tesla" "Shepard"]
// distances: []int [50 40 75 30 125]
// data : []uint8 [72 69 76 76 79]
// ratios : []float64 [3.14]
// alives : []bool [true false true false]
// zero : []uint8 []
// ---------------------------------------------------------
func main() {
names := [3]string{"Einstein", "Tesla", "Shepard"}
distances := [...]int{50, 40, 75, 30, 125}
data := [5]byte{'H', 'E', 'L', 'L', 'O'}
ratios := [1]float64{3.14145}
alives := [...]bool{true, false, true, false}
zero := [0]byte{}
fmt.Printf("names : %[1]T %[1]q\n", names)
fmt.Printf("distances: %[1]T %[1]d\n", distances)
fmt.Printf("data : %[1]T %[1]d\n", data)
fmt.Printf("ratios : %[1]T %.2[1]f\n", ratios)
fmt.Printf("alives : %[1]T %[1]t\n", alives)
fmt.Printf("zero : %[1]T %[1]d\n", zero)
}

View File

@ -0,0 +1,26 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
names := []string{"Einstein", "Tesla", "Shepard"}
distances := []int{50, 40, 75, 30, 125}
data := []byte{'H', 'E', 'L', 'L', 'O'}
ratios := []float64{3.14145}
alives := []bool{true, false, true, false}
zero := []byte{}
fmt.Printf("names : %[1]T %[1]q\n", names)
fmt.Printf("distances: %[1]T %[1]d\n", distances)
fmt.Printf("data : %[1]T %[1]d\n", data)
fmt.Printf("ratios : %[1]T %.2[1]f\n", ratios)
fmt.Printf("alives : %[1]T %[1]t\n", alives)
fmt.Printf("zero : %[1]T %[1]d\n", zero)
}

View File

@ -0,0 +1,57 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// EXERCISE: Fix the problems
//
// 1. Uncomment the code
//
// 2. Fix the problems
//
// 3. BONUS: Simplify the code
//
//
// EXPECTED OUTPUT
// "Einstein and Shepard and Tesla"
// ["Fire" "Kafka's Revenge" "Stay Golden"]
// [1 2 3 5 6 7 8 9]
// ---------------------------------------------------------
func main() {
// var names []string
// names := []string{}
// names = [...]string{
// "Einstein" "Shepard"
// "Tesla"
// }
// -----------------------------------
// var books []string = [3]string{
// "Stay Golden",
// "Fire",
// "Kafka's Revenge",
// }
// sort.Strings(books)
// -----------------------------------
// // this time, do not change the nums array to a slice
// nums := [...]int{5,1,7,3,8,2,6,9}
// // use the slicing expression to change the nums array to a slice below
// sort.Ints(nums)
// -----------------------------------
// Here: Use the strings.Join function to join the names
// (see the expected output)
// fmt.Printf("%q\n", names)
// fmt.Printf("%q\n", books)
// fmt.Printf("%d\n", nums)
}

View File

@ -0,0 +1,28 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
names := []string{"Einstein", "Shepard", "Tesla"}
books := []string{"Stay Golden", "Fire", "Kafka's Revenge"}
sort.Strings(books)
nums := [...]int{5, 1, 7, 3, 8, 2, 6, 9}
sort.Ints(nums[:])
fmt.Printf("%q\n", strings.Join(names, " and "))
fmt.Printf("%q\n", books)
fmt.Printf("%d\n", nums)
}

View File

@ -0,0 +1,38 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
// ---------------------------------------------------------
// EXERCISE: Compare the slices
//
// 1. Split the namesA string and get a slice
//
// 2. Sort all the slices
//
// 3. Compare whether the slices are equal or not
//
//
// EXPECTED OUTPUT
//
// They are equal.
//
//
// HINTS
//
// 1. strings.Split function splits a string and
// returns a string slice
//
// 2. Comparing slices: First check whether their length
// are the same or not; only then compare them.
//
// ---------------------------------------------------------
func main() {
// namesA := "Da Vinci, Wozniak, Carmack"
// namesB := []string{"Wozniak", "Da Vinci", "Carmack"}
}

View File

@ -0,0 +1,33 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"sort"
"strings"
)
func main() {
namesA := "Da Vinci, Wozniak, Carmack"
namesB := []string{"Wozniak", "Da Vinci", "Carmack"}
namesC := strings.Split(namesA, ", ")
sort.Strings(namesC)
sort.Strings(namesB)
if len(namesC) == len(namesB) {
for i := range namesC {
if namesC[i] != namesB[i] {
return
}
}
fmt.Println("They are equal.")
}
}

View File

@ -0,0 +1,25 @@
package main
// ---------------------------------------------------------
// EXERCISE: Append
//
// Please follow the instructions within the code below.
//
// EXPECTED OUTPUT
// They are equal.
//
// HINTS
// bytes.Equal function allows you to compare two byte
// slices easily. Check its documentation: go doc bytes.Equal
// ---------------------------------------------------------
func main() {
// 1. uncomment the code below
// png, header := []byte{'P', 'N', 'G'}, []byte{}
// 2. append elements to header to make it equal with the png slice
// 3. compare the slices using the bytes.Equal function
// 4. print whether they're equal or not
}

View File

@ -0,0 +1,23 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"bytes"
"fmt"
)
func main() {
png, header := []byte{'P', 'N', 'G'}, []byte{}
header = append(header, png...)
if bytes.Equal(png, header) {
fmt.Println("They are equal")
}
}

View File

@ -0,0 +1,41 @@
package main
// ---------------------------------------------------------
// EXERCISE: Append #2
//
// 1. Create the following nil slices:
// + Pizza toppings
// + Departure times
// + Student graduation years
// + On/off states of lights in a room
//
// 2. Append them some elements (use your creativity!)
//
// 3. Print all the slices
//
//
// EXPECTED OUTPUT
// (Your output may change, mine is like so:)
//
// pizza : [pepperoni onions extra cheese]
//
// graduations : [1998 2005 2018]
//
// departures : [2019-01-28 15:09:31.294594 +0300 +03 m=+0.000325020
// 2019-01-29 15:09:31.294594 +0300 +03 m=+86400.000325020
// 2019-01-30 15:09:31.294594 +0300 +03 m=+172800.000325020]
//
// lights : [true false true]
//
//
// HINTS
// + For departure times, use the time.Time type. Check its documentation.
//
// now := time.Now() -> Gives you the current time
// now.Add(time.Hour*24) -> Gives you a time.Time 24 hours after `now`
//
// + For graduation years, you can use the int type
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,55 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"time"
)
func main() {
// ------------------------------------------------------------
// Create nil slices
// ------------------------------------------------------------
// Pizza toppings
var pizza []string
// Departure times
var departures []time.Time
// Student graduation years
var grads []int
// On/off states of lights in a room
var lights []bool
// ------------------------------------------------------------
// Append them some elements (use your creativity!)
// ------------------------------------------------------------
pizza = append(pizza, "pepperoni", "onions", "extra cheese")
now := time.Now()
departures = append(departures,
now,
now.Add(time.Hour*24), // 24 hours after `now`
now.Add(time.Hour*48)) // 48 hours after `now`
grads = append(grads, 1998, 2005, 2018)
lights = append(lights, true, false, true)
// ------------------------------------------------------------
// Print the slices
// ------------------------------------------------------------
fmt.Printf("pizza : %s\n", pizza)
fmt.Printf("\ngraduations : %d\n", grads)
fmt.Printf("\ndepartures : %s\n", departures)
fmt.Printf("\nlights : %t\n", lights)
}

View File

@ -0,0 +1,27 @@
package main
// ---------------------------------------------------------
// EXERCISE: Append #3 — Fix the problems
//
// Fix the problems in the code below.
//
// BONUS
//
// Simplify the code.
//
// EXPECTED OUTPUT
//
// toppings: [black olives green peppers onions extra cheese]
//
// ---------------------------------------------------------
func main() {
// toppings := []int{"black olives", "green peppers"}
// var pizza [3]string
// append(pizza, ...toppings)
// pizza = append(toppings, "onions")
// toppings = append(pizza, extra cheese)
// fmt.Printf("pizza : %s\n", pizza)
}

View File

@ -0,0 +1,20 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
toppings := []string{"black olives", "green peppers"}
var pizza []string
pizza = append(pizza, toppings...)
pizza = append(pizza, "onions", "extra cheese")
fmt.Printf("toppings: %s\n", pizza)
}

View File

@ -0,0 +1,25 @@
# Slice Exercises
## Exercises Level I - Basics — Warm-Up
1. **[Declare nil slices](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/01-declare-nil)**
2. **[Assign empty slices](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/02-empty)**
3. **[Assign slice literals](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/03-slice-literal)**
4. **[Declare the arrays as slices](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/04-declare-arrays-as-slices)**
5. **[Fix the Problems](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/05-fix-the-problems)**
6. **[Compare the slices](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/06-compare-the-slices)**
---
## Exercises Level II - Appending
1. **[Append #1 — Append and compare byte slices](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/07-append)**
2. **[Append #2 — Append to a nil slice](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/08-append-2)**
3. **[Append #3 — Fix the problems](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/09-append-3-fix)**