add: exercises and quiz for appending and slicing

This commit is contained in:
Inanc Gumus
2019-01-30 16:47:30 +03:00
parent 1068a0b9cd
commit 8ec394bc10
23 changed files with 928 additions and 63 deletions

View File

@@ -0,0 +1,71 @@
package main
// ---------------------------------------------------------
// EXERCISE: Housing Prices
//
// We have received housing prices. Your task is to load the data into
// appropriately typed slices then print them.
//
// 1. Check out the expected output
//
//
// 2. Check out the code below
//
// 1. header : stores the column headers
// 2. data : stores the real data related to each column
// 3. separator: you will use it to separate the data
//
//
// 3. Parse the data
//
// 1. Separate it into rows by using the newline character ("\n")
//
// 2. For each row, separate it by using the separator (",")
//
//
// 4. Load the data into distinct slices
//
// 1. Load the locations into a []string
// 2. Load the sizes into []int
// 3. Load the beds into []int
// 4. Load the baths into []int
// 5. Load the prices into []int
//
//
// 5. Print the header
//
// 1. Separate it by using the separator
//
// 2. Print each column 15 character wide ("%-15s")
//
//
// 6. Print the rows from the slices that you've created, line by line
//
//
// EXPECTED OUTPUT
//
// Location Size Beds Baths Price
// ===========================================================================
// New York 100 2 1 100000
// New York 150 3 2 200000
// Paris 200 4 3 400000
// Istanbul 500 10 5 1000000
//
//
// HINTS
//
// + strings.Split function can separate a string into a []string
//
// ---------------------------------------------------------
func main() {
const (
header = "Location,Size,Beds,Baths,Price"
data = `New York,100,2,1,100000
New York,150,3,2,200000
Paris,200,4,3,400000
Istanbul,500,10,5,1000000`
separator = ","
)
}

View File

@@ -0,0 +1,65 @@
// 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() {
const (
header = "Location,Size,Beds,Baths,Price"
data = `New York,100,2,1,100000
New York,150,3,2,200000
Paris,200,4,3,400000
Istanbul,500,10,5,1000000`
separator = ","
)
var (
locs []string
sizes, beds, baths, prices []int
)
rows := strings.Split(data, "\n")
for _, row := range rows {
cols := strings.Split(row, separator)
locs = append(locs, cols[0])
n, _ := strconv.Atoi(cols[1])
sizes = append(sizes, n)
n, _ = strconv.Atoi(cols[2])
beds = append(beds, n)
n, _ = strconv.Atoi(cols[3])
baths = append(baths, n)
n, _ = strconv.Atoi(cols[4])
prices = append(prices, n)
}
for _, h := range strings.Split(header, separator) {
fmt.Printf("%-15s", h)
}
fmt.Printf("\n%s\n", strings.Repeat("=", 75))
for i := range rows {
fmt.Printf("%-15s", locs[i])
fmt.Printf("%-15d", sizes[i])
fmt.Printf("%-15d", beds[i])
fmt.Printf("%-15d", baths[i])
fmt.Printf("%-15d", prices[i])
fmt.Println()
}
}