| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | // 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() { | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// There are 3 request totals per day | 
					
						
							|  |  |  | 	const N = 3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// DAILY REQUESTS DATA (PER 8-HOUR) | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	reqs := []int{ | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 		500, 600, 250, // 1st day | 
					
						
							|  |  |  | 		200, 400, 50, // 2nd day | 
					
						
							|  |  |  | 		900, 800, 600, // 3rd day | 
					
						
							|  |  |  | 		750, 250, 100, // 4th day | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// ================================================ | 
					
						
							|  |  |  | 	// Allocate a slice efficiently with the exact size needed. | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	// | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// Find the `size` automatically using the `reqs` slice. | 
					
						
							|  |  |  | 	// Do not type it manually. | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	// | 
					
						
							|  |  |  | 	l := len(reqs) | 
					
						
							|  |  |  | 	size := l / N | 
					
						
							|  |  |  | 	if l%N != 0 { | 
					
						
							|  |  |  | 		size++ | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	daily := make([][]int, 0, size) | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// | 
					
						
							|  |  |  | 	// ================================================ | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// ================================================ | 
					
						
							|  |  |  | 	// 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-03-05 23:32:32 +03:00
										 |  |  | 	// | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	for N <= len(reqs) { | 
					
						
							|  |  |  | 		daily = append(daily, reqs[:N]) // append the daily requests | 
					
						
							|  |  |  | 		reqs = reqs[N:]                 // move the slice pointer for the next day | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +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
										 |  |  | 	// ================================================ | 
					
						
							|  |  |  | 	// Don't touch the following code: | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// Print the header | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	fmt.Printf("%-10s%-10s\n", "Day", "Requests") | 
					
						
							|  |  |  | 	fmt.Println(strings.Repeat("=", 20)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	// Print the data per day along with the totals | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 	var grand int | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	for i, day := range daily { | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 		var sum int | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 		for _, req := range day { | 
					
						
							|  |  |  | 			sum += req | 
					
						
							|  |  |  | 			fmt.Printf("%-10d%-10d\n", i+1, req) | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 		fmt.Printf("%9s %-10d\n\n", "TOTAL:", sum) | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		grand += sum | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 14:43:37 +03:00
										 |  |  | 	fmt.Printf("%9s %-10d\n", "GRAND:", grand) | 
					
						
							|  |  |  | 	// ================================================ | 
					
						
							| 
									
										
										
										
											2019-03-05 23:32:32 +03:00
										 |  |  | } |