Files
learngo/16-slices/exercises/26-print-daily-requests/main.go

110 lines
2.6 KiB
Go
Raw Normal View History

2019-03-05 23:32:32 +03:00
package main
// ---------------------------------------------------------
// EXERCISE: Print daily requests
//
// You've got a requests log for a system in a slice: `reqs`.
2019-03-05 23:49:54 +03:00
// The log contains the total request counts per 8 hours.
2019-03-05 23:32:32 +03:00
//
// The reqs slice is a single-dimensional slice but you need
2019-03-05 23:49:54 +03:00
// to group the requests daily into a slice named: `daily`.
2019-03-05 23:32:32 +03:00
//
// 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{
2019-03-05 23:49:54 +03:00
// // 1st day
// 500, 600, 250,
// // 2nd day
// 200, 400, 50,
// // 3rd day
// 900, 800, 600,
// // 4th day
// 750, 250, 100,
// // last day
// 100, 150,
2019-03-05 23:32:32 +03:00
// }
//
// #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 ❤️
//
2019-03-05 23:49:54 +03:00
// If you could't solve this challenge, please do not get discouraged.
2019-03-05 23:32:32 +03:00
//
// Look at the solution, then try to solve it again. This is valuable too.
//
2019-03-05 23:49:54 +03:00
// Then change the request data, the number of requests per day (now it's 3),
// etc., and then try to solve it again.
2019-03-05 23:32:32 +03:00
//
// ------------------------------------------------------------------------
}