Files

115 lines
3.0 KiB
Go
Raw Permalink Normal View History

2019-10-30 19:34:44 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
2019-03-05 23:32:32 +03:00
package main
2019-08-18 14:43:37 +03:00
import (
"fmt"
"strings"
)
2019-03-05 23:32:32 +03:00
// ---------------------------------------------------------
// EXERCISE: Print daily requests
//
2019-08-18 14:43:37 +03:00
// You've got request logs of a web server. The log data
2019-08-22 21:21:26 +03:00
// contains 8-hourly totals per each day. It is stored
// in the `reqs` slice.
//
// Find and print the total requests per day, as well as
// the grand total.
2019-03-05 23:32:32 +03:00
//
2019-08-18 14:43:37 +03:00
// See the `reqs` slice and the steps in the code below.
2019-03-05 23:32:32 +03:00
//
//
// RESTRICTIONS
//
// 1. You need to produce the daily slice, don't just loop
// and print the element totals directly. The goal is
// gaining more experience in slice operations.
//
// 2. Your code should work even if you add to or remove the
// existing elements from the `reqs` slice.
//
// For example, after solving the exercise, try it with
// this new data:
//
// reqs := []int{
// 500, 600, 250,
// 200, 400, 50,
// 900, 800, 600,
// 750, 250, 100,
// 150, 654, 235,
// 320, 534, 765,
// 121, 876, 285,
// 543, 642,
// // the last element is missing (your code should be able to handle this)
// // that is why you shouldn't calculate the `size` below manually.
// }
//
// The grand total of the new data should be 10525.
2019-08-22 21:21:26 +03:00
//
//
2019-03-05 23:32:32 +03:00
// EXPECTED OUTPUT
//
// Please run `solution/main.go` to see the expected
// output.
//
// go run solution/main.go
//
// ---------------------------------------------------------
func main() {
2019-08-18 14:43:37 +03:00
// There are 3 request totals per day (8-hourly)
const N = 3
2019-03-05 23:32:32 +03:00
2019-08-18 14:43:37 +03:00
// DAILY REQUESTS DATA (8-HOURLY TOTALS PER DAY)
reqs := []int{
500, 600, 250, // 1st day: 1350 requests
200, 400, 50, // 2nd day: 650 requests
900, 800, 600, // 3rd day: 2300 requests
750, 250, 100, // 4th day: 1100 requests
// grand total: 5400 requests
}
2019-08-22 21:21:26 +03:00
2019-08-18 14:43:37 +03:00
// ================================================
2019-08-22 21:21:26 +03:00
// #1: Make a new slice with the exact size needed.
2019-03-05 23:32:32 +03:00
2019-08-18 14:43:37 +03:00
_ = reqs // remove this when you start
2019-03-05 23:32:32 +03:00
2019-08-18 14:43:37 +03:00
size := 0 // you need to find the size.
daily := make([][]int, 0, size)
2019-08-22 21:21:26 +03:00
2019-08-18 14:43:37 +03:00
// ================================================
2019-03-05 23:32:32 +03:00
2019-08-18 14:43:37 +03:00
// ================================================
2019-08-22 21:21:26 +03:00
// #2: Group the `reqs` per day into the slice: `daily`.
2019-03-05 23:32:32 +03:00
//
2019-08-18 14:43:37 +03:00
// So the daily will be:
// [
// [500, 600, 250]
// [200, 400, 50]
// [900, 800, 600]
// [750, 250, 100]
// ]
2019-08-22 21:21:26 +03:00
_ = daily // remove this when you start
2019-08-18 14:43:37 +03:00
// ================================================
2019-08-22 21:21:26 +03:00
// #3: Print the results
2019-08-18 14:43:37 +03:00
2019-08-22 21:21:26 +03:00
// Print a header
2019-08-18 14:43:37 +03:00
fmt.Printf("%-10s%-10s\n", "Day", "Requests")
fmt.Println(strings.Repeat("=", 20))
2019-08-22 21:21:26 +03:00
// Loop over the daily slice and its inner slices to find
// the daily totals and the grand total.
// ...
2019-08-18 14:43:37 +03:00
// ================================================
2019-03-05 23:32:32 +03:00
}