add: arrays

This commit is contained in:
Inanc Gumus
2018-11-17 21:56:09 +03:00
parent c0dbce8062
commit a3a0d39a0b
138 changed files with 2022 additions and 1905 deletions

View File

@ -1,10 +1,3 @@
// 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
// ---------------------------------------------------------

View File

@ -1,10 +1,3 @@
// 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
func main() {

View File

@ -10,33 +10,46 @@ package main
// ---------------------------------------------------------
// EXERCISE: Declare empty arrays
//
// 1. Declare and printf the following arrays:
// 1. Declare and print the following arrays with their types:
//
// 1. A string array with 4 items
// 2. An int array 5 items
// 3. A byte array with 5 items
// 4. A float64 array with 1 item
// 5. A bool array with 4 items
// 6. A byte array without any items
// 1. The names of your best three friends
// 2. The distances to five different locations
// 3. A data buffer with five bytes of capacity
// 4. Currency exchange ratios only for a single currency
// 5. Up/Down status of four different web servers
// 6. A byte array that doesn't occupy memory space
//
// 2. Print the types of the previous arrays.
// 2. Print only the types of the same arrays.
//
// NOTE
// You should use printf with #v verb.
// 3. Print only the elements of the same arrays.
//
// HINT
// When printing the elements of an array, you can use the usual Printf verbs.
//
// For example:
// When printing a string array, you can use "%q" verb as usual.
//
// EXPECTED OUTPUT
// names : [4]string{"", "", "", ""}
// names : [3]string{"", "", "", ""}
// distances: [5]int{0, 0, 0, 0, 0}
// data : [5]uint8{0x0, 0x0, 0x0, 0x0, 0x0}
// ratios : [1]float64{0}
// switches : [4]bool{false, false, false, false}
// zero : [0]bool{}
// names : [4]string
// alives : [4]bool{false, false, false, false}
// zero : [0]uint8{}
//
// names : [3]string
// distances: [5]int
// data : [5]uint8
// ratios : [1]float64
// switches : [4]bool
// zero : [0]bool
// alives : [4]bool
// zero : [0]uint8
//
// names : ["" "" ""]
// distances: [0 0 0 0 0]
// data : [0 0 0 0 0]
// ratios : [0.00]
// alives : [false false false false]
// zero : []
// ---------------------------------------------------------
func main() {

View File

@ -10,34 +10,38 @@ package main
import "fmt"
func main() {
// 1. Declare and printf the following arrays:
// 1. A string array with 4 items
// 2. An int array 5 items
// 3. A byte array with 5 items
// 4. A float64 array with 1 item
// 5. A bool array with 4 items
// 6. A byte array without any items
var (
names [4]string
distances [5]int
data [5]byte
ratios [1]float64
switches [4]bool
zero [0]bool
names [3]string // The names of your best three friends
distances [5]int // The distances to five different locations
data [5]byte // A data buffer with five bytes of capacity
ratios [1]float64 // Currency exchange ratios only for a single currency
alives [4]bool // Up/Down status of four different web servers
zero [0]byte // A byte array that doesn't occupy memory space
)
// 1. Declare and print the arrays with their types.
fmt.Printf("names : %#v\n", names)
fmt.Printf("distances: %#v\n", distances)
fmt.Printf("data : %#v\n", data)
fmt.Printf("ratios : %#v\n", ratios)
fmt.Printf("switches : %#v\n", switches)
fmt.Printf("alives : %#v\n", alives)
fmt.Printf("zero : %#v\n", zero)
// 2. Print the types of the previous arrays.
// 2. Print only the types of the same arrays.
fmt.Println()
fmt.Printf("names : %T\n", names)
fmt.Printf("distances: %T\n", distances)
fmt.Printf("data : %T\n", data)
fmt.Printf("ratios : %T\n", ratios)
fmt.Printf("switches : %T\n", switches)
fmt.Printf("alives : %T\n", alives)
fmt.Printf("zero : %T\n", zero)
// 3. Print only the elements of the same arrays.
fmt.Println()
fmt.Printf("names : %q\n", names)
fmt.Printf("distances: %d\n", distances)
fmt.Printf("data : %d\n", data)
fmt.Printf("ratios : %.2f\n", ratios)
fmt.Printf("alives : %t\n", alives)
fmt.Printf("zero : %d\n", zero)
}

View File

@ -0,0 +1,118 @@
package main
// ---------------------------------------------------------
// EXERCISE: Get and Set Array Elements
//
// 1. Use the 01-declare-empty exercise
// 2. Remove everything but the array declarations
//
// 3. Assign your best friends' names to the names array
//
// 4. Assign distances to the closest cities to you to the distance array
//
// 5. Assign arbitrary bytes to the data array
//
// 6. Assign a value to the ratios array
//
// 7. Assign true/false values to the alives arrays
//
// 8. Try to assign to the zero array and observe the error
//
// 9. Now use ordinary loop statements for each array and print them
// (do not use for range)
//
// 10. Now use for range loop statements for each array and print them
//
// 11. Try assigning different types of values to the arrays, break things,
// and observe the errors
//
// 12. Remove some of the array assignments and observe the loop outputs
// (zero values)
//
//
// EXPECTED OUTPUT
//
// Note: The output can change depending on the values that you've assigned to them, of course.
// You're free to assign any values.
//
// names
// ====================
// names[0]: "Einstein"
// names[1]: "Tesla"
// names[2]: "Shepard"
//
// distances
// ====================
// distances[0]: 50
// distances[1]: 40
// distances[2]: 75
// distances[3]: 30
// distances[4]: 125
//
// data
// ====================
// data[0]: 72
// data[1]: 69
// data[2]: 76
// data[3]: 76
// data[4]: 79
//
// ratios
// ====================
// ratios[0]: 3.14
//
// alives
// ====================
// alives[0]: true
// alives[1]: false
// alives[2]: true
// alives[3]: false
//
// zero
// ====================
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// FOR RANGES
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// names
// ====================
// names[0]: "Einstein"
// names[1]: "Tesla"
// names[2]: "Shepard"
//
// distances
// ====================
// distances[0]: 50
// distances[1]: 40
// distances[2]: 75
// distances[3]: 30
// distances[4]: 125
//
// data
// ====================
// data[0]: 72
// data[1]: 69
// data[2]: 76
// data[3]: 76
// data[4]: 79
//
// ratios
// ====================
// ratios[0]: 3.14
//
// alives
// ====================
// alives[0]: true
// alives[1]: false
// alives[2]: true
// alives[3]: false
//
// zero
// ====================
//
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,129 @@
// 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"
"strings"
)
func main() {
var (
names [3]string // The names of your best three friends
distances [5]int // The distances to five different locations
data [5]byte // A data buffer with five bytes of capacity
ratios [1]float64 // Currency exchange ratios only for a single currency
alives [4]bool // Up/Down status of four different web servers
zero [0]byte // A byte array that doesn't occupy memory space
)
names[0] = "Einstein"
names[1] = "Tesla"
names[2] = "Shepard"
distances[0] = 50
distances[1] = 40
distances[2] = 75
distances[3] = 30
distances[4] = 125
data[0] = 'H'
data[1] = 'E'
data[2] = 'L'
data[3] = 'L'
data[4] = 'O'
ratios[0] = 3.14145
alives[0] = true
alives[1] = false
alives[2] = true
alives[3] = false
// zero[0] = "BOMB!"
_ = zero
// =========================================================================
separator := "\n" + strings.Repeat("=", 20) + "\n"
fmt.Print("names", separator)
for i := 0; i < len(names); i++ {
fmt.Printf("names[%d]: %q\n", i, names[i])
}
fmt.Print("\ndistances", separator)
for i := 0; i < len(distances); i++ {
fmt.Printf("distances[%d]: %d\n", i, distances[i])
}
fmt.Print("\ndata", separator)
for i := 0; i < len(data); i++ {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, data[i])
}
fmt.Print("\nratios", separator)
for i := 0; i < len(ratios); i++ {
fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i])
}
fmt.Print("\nalives", separator)
for i := 0; i < len(alives); i++ {
fmt.Printf("alives[%d]: %t\n", i, alives[i])
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i := 0; i < len(zero); i++ {
fmt.Printf("zero[%d]: %d\n", i, zero[i])
}
// =========================================================================
// you know how this works :) don't be freaked out!
fmt.Printf(`
%s
FOR RANGES
%[1]s
`, strings.Repeat("~", 30))
fmt.Print("names", separator)
for i, v := range names {
fmt.Printf("names[%d]: %q\n", i, v)
}
fmt.Print("\ndistances", separator)
for i, v := range distances {
fmt.Printf("distances[%d]: %d\n", i, v)
}
fmt.Print("\ndata", separator)
for i, v := range data {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, v)
}
fmt.Print("\nratios", separator)
for i, v := range ratios {
fmt.Printf("ratios[%d]: %.2f\n", i, v)
}
fmt.Print("\nalives", separator)
for i, v := range alives {
fmt.Printf("alives[%d]: %t\n", i, v)
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i, v := range zero {
fmt.Printf("zero[%d]: %d\n", i, v)
}
}

View File

@ -0,0 +1,20 @@
package main
// ---------------------------------------------------------
// EXERCISE: Refactor to Array Literals
//
// 1. Use the 02-get-set-arrays exercise
//
// 2. Refactor the array assignments to array literals
//
// 1. You would need to change the array declarations to array literals
//
// 2. Then, you would need to move the right-hand side of the assignments,
// into the array literals.
//
// EXPECTED OUTPUT
// The output should be the same as the 02-get-set-arrays exercise.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,121 @@
// 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"
"strings"
)
func main() {
// The names of your best three friends
names := [3]string{
"Einstein",
"Tesla",
"Shepard",
}
// The distances to five different locations
distances := [5]int{50, 40, 75, 30, 125}
// A data buffer with five bytes of capacity
data := [5]byte{'H', 'E', 'L', 'L', 'O'}
// Currency exchange ratios only for a single currency
ratios := [1]float64{3.14145}
// Up/Down status of four different web servers
alives := [4]bool{true, false, true, false}
// A byte array that doesn't occupy memory space
//
// Don't do this:
// zero := [0]byte{}
//
// Do this (when you don't assign elements):
var zero [0]byte
// =========================================================================
separator := "\n" + strings.Repeat("=", 20) + "\n"
fmt.Print("names", separator)
for i := 0; i < len(names); i++ {
fmt.Printf("names[%d]: %q\n", i, names[i])
}
fmt.Print("\ndistances", separator)
for i := 0; i < len(distances); i++ {
fmt.Printf("distances[%d]: %d\n", i, distances[i])
}
fmt.Print("\ndata", separator)
for i := 0; i < len(data); i++ {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, data[i])
}
fmt.Print("\nratios", separator)
for i := 0; i < len(ratios); i++ {
fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i])
}
fmt.Print("\nalives", separator)
for i := 0; i < len(alives); i++ {
fmt.Printf("alives[%d]: %t\n", i, alives[i])
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i := 0; i < len(zero); i++ {
fmt.Printf("zero[%d]: %d\n", i, zero[i])
}
// =========================================================================
// you know how this works :) don't be freaked out!
fmt.Printf(`
%s
FOR RANGES
%[1]s
`, strings.Repeat("~", 30))
fmt.Print("names", separator)
for i, v := range names {
fmt.Printf("names[%d]: %q\n", i, v)
}
fmt.Print("\ndistances", separator)
for i, v := range distances {
fmt.Printf("distances[%d]: %d\n", i, v)
}
fmt.Print("\ndata", separator)
for i, v := range data {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, v)
}
fmt.Print("\nratios", separator)
for i, v := range ratios {
fmt.Printf("ratios[%d]: %.2f\n", i, v)
}
fmt.Print("\nalives", separator)
for i, v := range alives {
fmt.Printf("alives[%d]: %t\n", i, v)
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i, v := range zero {
fmt.Printf("zero[%d]: %d\n", i, v)
}
}

View File

@ -0,0 +1,18 @@
package main
// ---------------------------------------------------------
// EXERCISE: Refactor to Ellipsis
//
// 1. Use the 03-array-literal exercise
//
// 2. Refactor the length of the array literals to ellipsis
//
// This means: Use the ellipsis instead of defining the array's length
// manually.
//
// EXPECTED OUTPUT
// The output should be the same as the 03-array-literal exercise.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,117 @@
// 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"
"strings"
)
func main() {
// The names of your best three friends
names := [...]string{
"Einstein",
"Tesla",
"Shepard",
}
// The distances to five different locations
distances := [...]int{50, 40, 75, 30, 125}
// A data buffer with five bytes of capacity
data := [...]byte{'H', 'E', 'L', 'L', 'O'}
// Currency exchange ratios only for a single currency
ratios := [...]float64{3.14145}
// Up/Down status of four different web servers
alives := [...]bool{true, false, true, false}
// A byte array that doesn't occupy memory space
// Obviously, do not use ellipsis on this one
var zero []byte
// =========================================================================
separator := "\n" + strings.Repeat("=", 20) + "\n"
fmt.Print("names", separator)
for i := 0; i < len(names); i++ {
fmt.Printf("names[%d]: %q\n", i, names[i])
}
fmt.Print("\ndistances", separator)
for i := 0; i < len(distances); i++ {
fmt.Printf("distances[%d]: %d\n", i, distances[i])
}
fmt.Print("\ndata", separator)
for i := 0; i < len(data); i++ {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, data[i])
}
fmt.Print("\nratios", separator)
for i := 0; i < len(ratios); i++ {
fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i])
}
fmt.Print("\nalives", separator)
for i := 0; i < len(alives); i++ {
fmt.Printf("alives[%d]: %t\n", i, alives[i])
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i := 0; i < len(zero); i++ {
fmt.Printf("zero[%d]: %d\n", i, zero[i])
}
// =========================================================================
// you know how this works :) don't be freaked out!
fmt.Printf(`
%s
FOR RANGES
%[1]s
`, strings.Repeat("~", 30))
fmt.Print("names", separator)
for i, v := range names {
fmt.Printf("names[%d]: %q\n", i, v)
}
fmt.Print("\ndistances", separator)
for i, v := range distances {
fmt.Printf("distances[%d]: %d\n", i, v)
}
fmt.Print("\ndata", separator)
for i, v := range data {
// try the %c verb
fmt.Printf("data[%d]: %d\n", i, v)
}
fmt.Print("\nratios", separator)
for i, v := range ratios {
fmt.Printf("ratios[%d]: %.2f\n", i, v)
}
fmt.Print("\nalives", separator)
for i, v := range alives {
fmt.Printf("alives[%d]: %t\n", i, v)
}
// no loop for zero elements
fmt.Print("\nzero", separator)
for i, v := range zero {
fmt.Printf("zero[%d]: %d\n", i, v)
}
}

View File

@ -0,0 +1,13 @@
# Array Exercises
- get data from command-line
- into a fixed array; see how it blows beyond its len
- add items
- get items
- check the length
- reverse the array
- shuffle the items
- find the first item that contains x
- find the last item that contains y
- find the duplicate items

View File

@ -1,20 +1,19 @@
# Array Exercises
## Basic Exercises
1. **[Declare Empty Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/01-declare-empty)**
- get data from command-line
- into a fixed array; see how it blows beyond its len
2. **[Get and Set Array Elements](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/02-get-set-arrays)**
- add items
- get items
- check the length
- print the items
- reverse the array
- shuffle the items
- find the first item that contains x
- find the last item that contains y
- find the duplicate items
3. **[Refactor to Array Literals](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/03-array-literal)**
1. **[text](https://github.com/inancgumus/learngo/tree/master/)**
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
text
---
## Program Exercises
????. **[text](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/)**
?