diff --git a/x-tba/slices/exercises/01-declare-nil/main.go b/16-slices/exercises/01-declare-nil/main.go similarity index 91% rename from x-tba/slices/exercises/01-declare-nil/main.go rename to 16-slices/exercises/01-declare-nil/main.go index 2616f4f..417861c 100644 --- a/x-tba/slices/exercises/01-declare-nil/main.go +++ b/16-slices/exercises/01-declare-nil/main.go @@ -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) // diff --git a/x-tba/slices/exercises/01-declare-nil/solution/main.go b/16-slices/exercises/01-declare-nil/solution/main.go similarity index 100% rename from x-tba/slices/exercises/01-declare-nil/solution/main.go rename to 16-slices/exercises/01-declare-nil/solution/main.go diff --git a/x-tba/slices/exercises/02-empty/main.go b/16-slices/exercises/02-empty/main.go similarity index 97% rename from x-tba/slices/exercises/02-empty/main.go rename to 16-slices/exercises/02-empty/main.go index fd023f3..dd87b20 100644 --- a/x-tba/slices/exercises/02-empty/main.go +++ b/16-slices/exercises/02-empty/main.go @@ -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. diff --git a/x-tba/slices/exercises/02-empty/solution/main.go b/16-slices/exercises/02-empty/solution/main.go similarity index 100% rename from x-tba/slices/exercises/02-empty/solution/main.go rename to 16-slices/exercises/02-empty/solution/main.go diff --git a/x-tba/slices/exercises/03-slice-literal/main.go b/16-slices/exercises/03-slice-literal/main.go similarity index 98% rename from x-tba/slices/exercises/03-slice-literal/main.go rename to 16-slices/exercises/03-slice-literal/main.go index f758422..85f77fd 100644 --- a/x-tba/slices/exercises/03-slice-literal/main.go +++ b/16-slices/exercises/03-slice-literal/main.go @@ -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. diff --git a/x-tba/slices/exercises/03-slice-literal/solution/main.go b/16-slices/exercises/03-slice-literal/solution/main.go similarity index 100% rename from x-tba/slices/exercises/03-slice-literal/solution/main.go rename to 16-slices/exercises/03-slice-literal/solution/main.go diff --git a/x-tba/slices/exercises/04-declares-arrays-as-slices/main.go b/16-slices/exercises/04-declare-arrays-as-slices/main.go similarity index 100% rename from x-tba/slices/exercises/04-declares-arrays-as-slices/main.go rename to 16-slices/exercises/04-declare-arrays-as-slices/main.go diff --git a/x-tba/slices/exercises/04-declares-arrays-as-slices/solution/main.go b/16-slices/exercises/04-declare-arrays-as-slices/solution/main.go similarity index 100% rename from x-tba/slices/exercises/04-declares-arrays-as-slices/solution/main.go rename to 16-slices/exercises/04-declare-arrays-as-slices/solution/main.go diff --git a/16-slices/exercises/05-fix-the-problems/main.go b/16-slices/exercises/05-fix-the-problems/main.go new file mode 100644 index 0000000..63d1385 --- /dev/null +++ b/16-slices/exercises/05-fix-the-problems/main.go @@ -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) +} diff --git a/16-slices/exercises/05-fix-the-problems/solution/main.go b/16-slices/exercises/05-fix-the-problems/solution/main.go new file mode 100644 index 0000000..2f114ac --- /dev/null +++ b/16-slices/exercises/05-fix-the-problems/solution/main.go @@ -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) +} diff --git a/16-slices/exercises/06-compare-the-slices/main.go b/16-slices/exercises/06-compare-the-slices/main.go new file mode 100644 index 0000000..ed7d8fb --- /dev/null +++ b/16-slices/exercises/06-compare-the-slices/main.go @@ -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"} +} diff --git a/16-slices/exercises/06-compare-the-slices/solution/main.go b/16-slices/exercises/06-compare-the-slices/solution/main.go new file mode 100644 index 0000000..a4e6d49 --- /dev/null +++ b/16-slices/exercises/06-compare-the-slices/solution/main.go @@ -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.") + } +} diff --git a/16-slices/exercises/README.md b/16-slices/exercises/README.md new file mode 100644 index 0000000..77f2210 --- /dev/null +++ b/16-slices/exercises/README.md @@ -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)** \ No newline at end of file diff --git a/x-tba/slices/exercises/README.md b/x-tba/slices/exercises/README.md index 9512c04..726bb56 100644 --- a/x-tba/slices/exercises/README.md +++ b/x-tba/slices/exercises/README.md @@ -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 diff --git a/x-tba/slices/exercises/README2.md b/x-tba/slices/exercises/README2.md deleted file mode 100644 index 726bb56..0000000 --- a/x-tba/slices/exercises/README2.md +++ /dev/null @@ -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)**