Slope of the Best-Fit Line
Copperleaf Coffee is deciding whether to put another person behind the counter during the morning rush. From a run of recent shifts the manager has, for each shift, the number of staff on the counter and the orders served per hour, paired by position. Every branch sends its own run of shifts, and the branches are nothing alike.
The finance question is not whether staffing and throughput are related — everyone can see that they are. It is the exchange rate: one number saying how many extra orders an hour each additional person on the counter is worth, so it can be set against what that person costs.
Task: Print the slope of the least-squares straight line predicting the orders from the staffing — the line whose total squared vertical distance from the points is as small as possible — rounded to 2 decimals.
Input
Three lines. The first holds one integer n, the number of shifts (at least 2). The second holds the n staffing counts, separated by spaces. The third holds the n orders-per-hour figures, in the same shift order. Values are decimal numbers, and the staffing counts are not all the same.
Output
One line holding the slope, rounded to 2 decimals and printed the way Python prints a float — 2.0, never 2.00.
Example:
Input:
5
1 2 3 4 5
3 5 7 9 11
Output:
2.0
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in