refactor: slices 26th exercise
This commit is contained in:
@ -1,16 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Print daily requests
|
||||
//
|
||||
// You've got a requests log for a system in a slice: `reqs`.
|
||||
// The log contains the total request counts per 8 hours.
|
||||
// You've got request logs of a web server. The log data
|
||||
// contains 8-hourly totals per each day. Find and print
|
||||
// the total requests per day, as well as the grand total.
|
||||
//
|
||||
// The reqs slice is a single-dimensional slice but you need
|
||||
// to group the requests daily into a slice named: `daily`.
|
||||
//
|
||||
// Please follow the instructions inside the code to solve
|
||||
// the exercise.
|
||||
// See the `reqs` slice and the steps in the code below.
|
||||
//
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
@ -23,87 +25,77 @@ package main
|
||||
// ---------------------------------------------------------
|
||||
|
||||
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
|
||||
//
|
||||
// ================================================
|
||||
// Don't touch this code.
|
||||
|
||||
// reqs := []int{
|
||||
// // 1st day
|
||||
// 500, 600, 250,
|
||||
// // 2nd day
|
||||
// 200, 400, 50,
|
||||
// // 3rd day
|
||||
// 900, 800, 600,
|
||||
// // 4th day
|
||||
// 750, 250, 100,
|
||||
// // last day
|
||||
// 100, 150,
|
||||
// There are 3 request totals per day (8-hourly)
|
||||
const N = 3
|
||||
|
||||
// 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
|
||||
}
|
||||
// ================================================
|
||||
|
||||
_ = reqs // remove this when you start
|
||||
|
||||
// ================================================
|
||||
// Allocate a slice efficiently with the exact size needed.
|
||||
//
|
||||
// Find the `size` automatically using the `reqs` slice.
|
||||
// Do not type it manually.
|
||||
//
|
||||
// Change this code:
|
||||
size := 0 // you need to find the size.
|
||||
daily := make([][]int, 0, size)
|
||||
//
|
||||
// ================================================
|
||||
|
||||
// ================================================
|
||||
// Group the `reqs` per day into the slice: `daily`.
|
||||
//
|
||||
// So the daily will be:
|
||||
// [
|
||||
// [500, 600, 250]
|
||||
// [200, 400, 50]
|
||||
// [900, 800, 600]
|
||||
// [750, 250, 100]
|
||||
// ]
|
||||
//
|
||||
// Change this code:
|
||||
//
|
||||
// for N < len(reqs) {
|
||||
// append the daily requests to `daily` per 8-hour groups
|
||||
// }
|
||||
// ================================================
|
||||
|
||||
//
|
||||
// #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"
|
||||
//
|
||||
// ================================================
|
||||
// Don't touch the following code:
|
||||
|
||||
//
|
||||
// #3: Print the header:
|
||||
//
|
||||
// Day Requests
|
||||
// ====================
|
||||
//
|
||||
// Print the header
|
||||
fmt.Printf("%-10s%-10s\n", "Day", "Requests")
|
||||
fmt.Println(strings.Repeat("=", 20))
|
||||
|
||||
//
|
||||
// #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
|
||||
// Print the data per day along with the totals
|
||||
var grand int
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
//
|
||||
// ❤️ 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.
|
||||
//
|
||||
// ------------------------------------------------------------------------
|
||||
for i, day := range daily {
|
||||
var sum int
|
||||
|
||||
for _, req := range day {
|
||||
sum += req
|
||||
fmt.Printf("%-10d%-10d\n", i+1, req)
|
||||
}
|
||||
|
||||
fmt.Printf("%9s %-10d\n\n", "TOTAL:", sum)
|
||||
|
||||
grand += sum
|
||||
}
|
||||
fmt.Printf("%9s %-10d\n", "GRAND:", grand)
|
||||
|
||||
// ================================================
|
||||
}
|
||||
|
@ -13,24 +13,22 @@ import (
|
||||
)
|
||||
|
||||
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
|
||||
//
|
||||
// There are 3 request totals per day
|
||||
const N = 3
|
||||
|
||||
// DAILY REQUESTS DATA (PER 8-HOUR)
|
||||
reqs := []int{
|
||||
500, 600, 250, // 1st day
|
||||
200, 400, 50, // 2nd day
|
||||
900, 800, 600, // 3rd day
|
||||
750, 250, 100, // 4th day
|
||||
}
|
||||
|
||||
// ================================================
|
||||
// Allocate a slice efficiently with the exact size needed.
|
||||
//
|
||||
// Allocate the slice efficiently with the exact size needed
|
||||
// Find the `size` automatically using the `reqs` slice.
|
||||
// Do not type it manually.
|
||||
//
|
||||
l := len(reqs)
|
||||
size := l / N
|
||||
@ -38,44 +36,49 @@ func main() {
|
||||
size++
|
||||
}
|
||||
daily := make([][]int, 0, size)
|
||||
//
|
||||
// ================================================
|
||||
|
||||
// ================================================
|
||||
// Group the `reqs` per day into the slice: `daily`.
|
||||
//
|
||||
// Group the `reqs` per day into a slice named: `daily`
|
||||
// So the daily will be:
|
||||
// [
|
||||
// [500, 600, 250]
|
||||
// [200, 400, 50]
|
||||
// [900, 800, 600]
|
||||
// [750, 250, 100]
|
||||
// ]
|
||||
//
|
||||
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
|
||||
for N <= len(reqs) {
|
||||
daily = append(daily, reqs[:N]) // append the daily requests
|
||||
reqs = reqs[N:] // move the slice pointer for the next day
|
||||
}
|
||||
// ================================================
|
||||
|
||||
//
|
||||
// Add the residual elements to the group (len(reqs) % N)
|
||||
//
|
||||
daily = append(daily, reqs)
|
||||
// ================================================
|
||||
// Don't touch the following code:
|
||||
|
||||
//
|
||||
// Print the header:
|
||||
//
|
||||
// Print the header
|
||||
fmt.Printf("%-10s%-10s\n", "Day", "Requests")
|
||||
fmt.Println(strings.Repeat("=", 20))
|
||||
|
||||
//
|
||||
// Print the data per day along with the totals:
|
||||
//
|
||||
// Print the data per day along with the totals
|
||||
var grand int
|
||||
|
||||
for i, d := range daily {
|
||||
for i, day := range daily {
|
||||
var sum int
|
||||
|
||||
for _, q := range d {
|
||||
sum += q
|
||||
fmt.Printf("%-10d%-10d\n", i+1, q)
|
||||
for _, req := range day {
|
||||
sum += req
|
||||
fmt.Printf("%-10d%-10d\n", i+1, req)
|
||||
}
|
||||
|
||||
fmt.Println(strings.Repeat("-", 20))
|
||||
fmt.Printf("%10s%-10d\n\n", "", sum)
|
||||
fmt.Printf("%9s %-10d\n\n", "TOTAL:", sum)
|
||||
|
||||
grand += sum
|
||||
}
|
||||
|
||||
fmt.Printf("%10s%-10d\n", "", grand)
|
||||
fmt.Printf("%9s %-10d\n", "GRAND:", grand)
|
||||
// ================================================
|
||||
}
|
||||
|
Reference in New Issue
Block a user