add: exercises and quiz for appending and slicing
This commit is contained in:
54
16-slices/exercises/13-slicing-basics/main.go
Normal file
54
16-slices/exercises/13-slicing-basics/main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Slice the numbers
|
||||
//
|
||||
// We've a string that contains even and odd numbers.
|
||||
//
|
||||
// 1. Convert the string to an []int
|
||||
//
|
||||
// 2. Print the slice
|
||||
//
|
||||
// 3. Slice it for the even numbers and print it (assign it to a new slice variable)
|
||||
//
|
||||
// 4. Slice it for the odd numbers and print it (assign it to a new slice variable)
|
||||
//
|
||||
// 5. Slice it for the two numbers at the middle
|
||||
//
|
||||
// 6. Slice it for the first two numbers
|
||||
//
|
||||
// 7. Slice it for the last two numbers (use the len function)
|
||||
//
|
||||
// 8. Slice the evens slice for the last one number
|
||||
//
|
||||
// 9. Slice the odds slice for the last two numbers
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go run main.go
|
||||
//
|
||||
// nums : [2 4 6 1 3 5]
|
||||
// evens : [2 4 6]
|
||||
// odds : [1 3 5]
|
||||
// middle : [6 1]
|
||||
// first 2 : [2 4]
|
||||
// last 2 : [3 5]
|
||||
// evens last 1: [6]
|
||||
// odds last 2 : [4 6]
|
||||
//
|
||||
//
|
||||
// NOTE
|
||||
//
|
||||
// You can also use my prettyslice package for printing the slices.
|
||||
//
|
||||
//
|
||||
// HINT
|
||||
//
|
||||
// Find a function in the strings package for splitting the string into []string
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// uncomment the declaration below
|
||||
// data := "2 4 6 1 3 5"
|
||||
}
|
40
16-slices/exercises/13-slicing-basics/solution/main.go
Normal file
40
16-slices/exercises/13-slicing-basics/solution/main.go
Normal file
@ -0,0 +1,40 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
data := "2 4 6 1 3 5"
|
||||
splitted := strings.Fields(data)
|
||||
|
||||
var nums []int
|
||||
for _, s := range splitted {
|
||||
n, _ := strconv.Atoi(s)
|
||||
nums = append(nums, n)
|
||||
}
|
||||
|
||||
// rest of the code for this exercise
|
||||
fmt.Println("nums :", nums)
|
||||
|
||||
evens, odds := nums[:3], nums[3:]
|
||||
|
||||
fmt.Println("evens :", evens)
|
||||
fmt.Println("odds :", odds)
|
||||
|
||||
fmt.Println("middle :", nums[2:4])
|
||||
fmt.Println("first 2 :", nums[:2])
|
||||
fmt.Println("last 2 :", nums[len(nums)-2:])
|
||||
|
||||
fmt.Println("evens last 1:", evens[len(evens)-1:])
|
||||
fmt.Println("odds last 2 :", evens[len(evens)-2:])
|
||||
}
|
Reference in New Issue
Block a user