add: slice exercises
This commit is contained in:
		@@ -8,9 +8,9 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
// ---------------------------------------------------------
 | 
			
		||||
// EXERCISE: ?
 | 
			
		||||
// EXERCISE: Declare nil slices
 | 
			
		||||
//
 | 
			
		||||
//  1. Declare the following slices:
 | 
			
		||||
//  1. Declare the following slices as nil slices:
 | 
			
		||||
//
 | 
			
		||||
//    1. The names of your friends (names slice)
 | 
			
		||||
//
 | 
			
		||||
@@ -10,7 +10,7 @@ package main
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
// ---------------------------------------------------------
 | 
			
		||||
// EXERCISE: ?
 | 
			
		||||
// EXERCISE: Assign empty slices
 | 
			
		||||
//
 | 
			
		||||
//   Assign empty slices to all the slices that you've declared in the previous
 | 
			
		||||
//   exercise, and print them here.
 | 
			
		||||
@@ -10,7 +10,7 @@ package main
 | 
			
		||||
import "fmt"
 | 
			
		||||
 | 
			
		||||
// ---------------------------------------------------------
 | 
			
		||||
// EXERCISE: ?
 | 
			
		||||
// EXERCISE: Assign slice literals
 | 
			
		||||
//
 | 
			
		||||
//   1. Assign the following data using slice literals to the slices that
 | 
			
		||||
//      you've declared in the first exercise.
 | 
			
		||||
							
								
								
									
										57
									
								
								16-slices/exercises/05-fix-the-problems/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								16-slices/exercises/05-fix-the-problems/main.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								16-slices/exercises/05-fix-the-problems/solution/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								16-slices/exercises/05-fix-the-problems/solution/main.go
									
									
									
									
									
										Normal 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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								16-slices/exercises/06-compare-the-slices/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								16-slices/exercises/06-compare-the-slices/main.go
									
									
									
									
									
										Normal 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"}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										33
									
								
								16-slices/exercises/06-compare-the-slices/solution/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								16-slices/exercises/06-compare-the-slices/solution/main.go
									
									
									
									
									
										Normal 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.")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								16-slices/exercises/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								16-slices/exercises/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
# 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)**
 | 
			
		||||
@@ -1,14 +1,41 @@
 | 
			
		||||
# Slice Exercises
 | 
			
		||||
 | 
			
		||||
// TODO:
 | 
			
		||||
//     + Fix the problem
 | 
			
		||||
//       -> Don't compare using nil, use the len(s)
 | 
			
		||||
//
 | 
			
		||||
//   + Compare two slices whether they're equal (using a loop)
 | 
			
		||||
//     + Compare arrays and slices using for loops
 | 
			
		||||
//
 | 
			
		||||
//   + Try to compare a slice to an array
 | 
			
		||||
## TODO
 | 
			
		||||
* append to slices
 | 
			
		||||
  * append to a nil slice
 | 
			
		||||
  * append to an empty slice
 | 
			
		||||
 | 
			
		||||
  * check their length — see how they grow
 | 
			
		||||
 | 
			
		||||
  * fix the problem (forgetten overwriting to the same slice)
 | 
			
		||||
 | 
			
		||||
  * get arguments from command line and make them uppercase
 | 
			
		||||
  * multiply the numbers
 | 
			
		||||
 | 
			
		||||
* slicing
 | 
			
		||||
  * slice exercises... n:m.. using len etc..
 | 
			
		||||
  * create a pagination
 | 
			
		||||
  * use the same slice variable
 | 
			
		||||
 | 
			
		||||
* internals
 | 
			
		||||
  * shared array: implicit/explicit
 | 
			
		||||
  * appending to a nil array
 | 
			
		||||
  * sorts package sorting
 | 
			
		||||
 | 
			
		||||
  * exercises about capacity
 | 
			
		||||
  * exercises about the mechanics of append
 | 
			
		||||
    * growing
 | 
			
		||||
    * adding elements at the middle etc
 | 
			
		||||
  * exercises about full slice expressions
 | 
			
		||||
 | 
			
		||||
  * questions:
 | 
			
		||||
    * slice header questions
 | 
			
		||||
    * slice and ask what's the pointer field, len, cap etc
 | 
			
		||||
    * when a new backing array is allocated: nil, empty, no capacity
 | 
			
		||||
    * when to use a full slice expression
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## Exercises Level I - Basics
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,71 +0,0 @@
 | 
			
		||||
# Slice Exercises
 | 
			
		||||
 | 
			
		||||
## TODO
 | 
			
		||||
* append to slices
 | 
			
		||||
  * append to a nil slice
 | 
			
		||||
  * append to an empty slice
 | 
			
		||||
 | 
			
		||||
  * check their length — see how they grow
 | 
			
		||||
 | 
			
		||||
  * fix the problem (forgetten overwriting to the same slice)
 | 
			
		||||
 | 
			
		||||
  * get arguments from command line and make them uppercase
 | 
			
		||||
  * multiply the numbers
 | 
			
		||||
 | 
			
		||||
* slicing
 | 
			
		||||
  * slice exercises... n:m.. using len etc..
 | 
			
		||||
  * create a pagination
 | 
			
		||||
  * use the same slice variable
 | 
			
		||||
 | 
			
		||||
* internals
 | 
			
		||||
  * shared array: implicit/explicit
 | 
			
		||||
  * appending to a nil array
 | 
			
		||||
  * sorts package sorting
 | 
			
		||||
 | 
			
		||||
  * exercises about capacity
 | 
			
		||||
  * exercises about the mechanics of append
 | 
			
		||||
    * growing
 | 
			
		||||
    * adding elements at the middle etc
 | 
			
		||||
  * exercises about full slice expressions
 | 
			
		||||
 | 
			
		||||
  * questions:
 | 
			
		||||
    * slice header questions
 | 
			
		||||
    * slice and ask what's the pointer field, len, cap etc
 | 
			
		||||
    * when a new backing array is allocated: nil, empty, no capacity
 | 
			
		||||
    * when to use a full slice expression
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## Exercises Level I - Basics
 | 
			
		||||
 | 
			
		||||
1. **[???](https://github.com/inancgumus/learngo/tree/master/16-slices/exercises/???)**
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
2. **[Get and Set Array Elements](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/02-get-set-arrays)**
 | 
			
		||||
 | 
			
		||||
3. **[Refactor to Array Literals](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/03-array-literal)**
 | 
			
		||||
 | 
			
		||||
4. **[Refactor to Ellipsis](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/04-ellipsis)**
 | 
			
		||||
 | 
			
		||||
5. **[Fix](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/05-fix)**
 | 
			
		||||
 | 
			
		||||
6. **[Compare the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/06-compare)**
 | 
			
		||||
 | 
			
		||||
7. **[Assign the Arrays](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/07-assign)**
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## Exercises Level II
 | 
			
		||||
 | 
			
		||||
1. **[Wizard Printer](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/08-wizard-printer)**
 | 
			
		||||
 | 
			
		||||
2. **[Currency Converter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/09-currency-converter)**
 | 
			
		||||
 | 
			
		||||
3. **[Hipster's Bookstore Search Engine](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/10-hipsters-love-search)**
 | 
			
		||||
 | 
			
		||||
4. **[Find the Average](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/11-average)**
 | 
			
		||||
 | 
			
		||||
5. **[Number Sorter](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/12-sorter)**
 | 
			
		||||
   
 | 
			
		||||
6. **[Word Finder](https://github.com/inancgumus/learngo/tree/master/14-arrays/exercises/13-word-finder)**
 | 
			
		||||
		Reference in New Issue
	
	Block a user