move: arrays retro clock and slices
This commit is contained in:
@ -1,38 +0,0 @@
|
||||
// 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() {
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
// 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.")
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
// 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)
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
// 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"}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
// 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.")
|
||||
}
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
# 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)**
|
Reference in New Issue
Block a user