add: slice adv. ops. exercises

This commit is contained in:
Inanc Gumus
2019-03-05 23:32:32 +03:00
parent e379976af4
commit 9c96082326
19 changed files with 810 additions and 99 deletions

View File

@ -0,0 +1,104 @@
package main
// ---------------------------------------------------------
// EXERCISE: Print daily requests
//
// You've got a requests log for a system in a slice: `reqs`.
// The log contains the total requests counts per 8 hours.
//
// The reqs slice is a single-dimensional slice but you need
// to group the daily into a slice named: `daily`.
//
// Please follow the instructions inside the code to solve
// the exercise.
//
//
// EXPECTED OUTPUT
//
// Please run `solution/main.go` to see the expected
// output.
//
// go run solution/main.go
//
// ---------------------------------------------------------
func main() {
//
// #1: DAILY REQUESTS DATA
//
// . The system collects and groups the requests per 8 hours
//
// . So there are 3 requests totals per day
//
// . Your code should be robust enough to work with
// insufficient data. For example, in the last day
// there are only two request totals: 100 and 150.
//
// So, don't forget to handle that edge case as well.
//
// . Uncomment the code below and start
//
// reqs := []int{
// 500, 600, 250,
// 200, 400, 50,
// 900, 800, 600,
// 750, 250, 100,
// 100, 150,
// }
//
// #2: Group the `reqs` per day into a slice named: `daily`
//
// . Create the daily slice using the `make` function
//
// . Anticipate the length argument to the make function using this data:
//
// + The length of the reqs slice
// + There are 3 requests totals per day
// + There are residual elements (the last day)
//
// ! So, do not blindly allocate a slice.
//
// ! Allocate the slice efficiently with the exact size needed.
//
// . Then append to it the daily requests in a "loop"
//
//
// #3: Print the header:
//
// Day Requests
// ====================
//
//
// #4: Print the data per day along with the totals:
//
// 1 500
// 1 600
// 1 250
// --------------------
// 1350 --> Print the daily total requests
//
// 2 200
// 2 400
// 2 50
// --------------------
// 650
//
// 2000 --> Also print the grand total
// ------------------------------------------------------------------------
//
// ❤️ NOTE ❤️
//
// If you could't solve this challenge. Please do not get discouraged.
//
// Look at the solution, then try to solve it again. This is valuable too.
//
// Then change the request data, the number of requests per day (now it's 3), etc
// and then try to solve it again.
//
// ------------------------------------------------------------------------
}

View File

@ -0,0 +1,81 @@
// 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"
"strings"
)
func main() {
//
// DAILY REQUESTS DATA
//
reqs := []int{
500, 600, 250,
200, 400, 50,
900, 800, 600,
750, 250, 100,
100, 150,
}
//
// There are 3 requests per day
//
const N = 3
//
// Allocate the slice efficiently with the exact size needed
//
l := len(reqs)
size := l / N
if l%N != 0 {
size++
}
daily := make([][]int, 0, size)
//
// Group the `reqs` per day into a slice named: `daily`
//
for N < len(reqs) {
daily = append(daily, reqs[:N]) // add the current batch of nums to the `groups`
reqs = reqs[N:] // move the slice pointer for the next batch
}
//
// Add the residual elements to the group (len(reqs) % N)
//
daily = append(daily, reqs)
//
// Print the header:
//
fmt.Printf("%-10s%-10s\n", "Day", "Requests")
fmt.Println(strings.Repeat("=", 20))
//
// Print the data per day along with the totals:
//
var grand int
for i, d := range daily {
var sum int
for _, q := range d {
sum += q
fmt.Printf("%-10d%-10d\n", i+1, q)
}
fmt.Println(strings.Repeat("-", 20))
fmt.Printf("%10s%-10d\n\n", "", sum)
grand += sum
}
fmt.Printf("%10s%-10d\n", "", grand)
}