add: exercises and quiz for appending and slicing
This commit is contained in:
38
16-slices/exercises/10-append-sort-nums/main.go
Normal file
38
16-slices/exercises/10-append-sort-nums/main.go
Normal file
@ -0,0 +1,38 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Append and Sort Numbers
|
||||
//
|
||||
// We'll have a []string that should contain numbers.
|
||||
//
|
||||
// Your task is to convert the []string to an int slice.
|
||||
//
|
||||
// 1. Get the numbers from the command-line
|
||||
//
|
||||
// 2. Append them to an []int
|
||||
//
|
||||
// 3. Sort the numbers
|
||||
//
|
||||
// 4. Print them
|
||||
//
|
||||
// 5. Handle the error cases
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// go run main.go
|
||||
// provide a few numbers
|
||||
//
|
||||
// go run main.go 4 6 1 3 0 9 2
|
||||
// [0 1 2 3 4 6 9]
|
||||
//
|
||||
// go run main.go a b c
|
||||
// []
|
||||
//
|
||||
// go run main.go 4 a 1 c d 9
|
||||
// [1 4 9]
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
36
16-slices/exercises/10-append-sort-nums/solution/main.go
Normal file
36
16-slices/exercises/10-append-sort-nums/solution/main.go
Normal file
@ -0,0 +1,36 @@
|
||||
// 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"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
fmt.Println("provide a few numbers")
|
||||
return
|
||||
}
|
||||
|
||||
var nums []int
|
||||
for _, s := range args {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
nums = append(nums, n)
|
||||
}
|
||||
|
||||
sort.Ints(nums)
|
||||
fmt.Println(nums)
|
||||
}
|
Reference in New Issue
Block a user