“Sometimes you have to make the hardest climb to see the most
beautiful sunrise. I read that
once on an old lady’s decorative pillow, but it is really how I feel
today. I’ve climbed a very weird and rocky
mountain, and it was a pain in the ass, and my legs are
tired, and I’m starving. But the sun is rising over
a sea of love and waffles and possibility. So I’m just gonna relax and
take a deep breath and enjoy this view
for as long as I possibly can.” – Leslie Knope (from Parks and
Recreation)
Homework | Midterm | Final Exam | Final Project |
---|---|---|---|
18% | 30% | 40% | 12% |
Customer needs can differ greatly from the technical delivery, especially if the customer’s ideas are not clear or precise.
Make sure to be clear and concise with instructions. Just because you understand something, doesn’t mean the other person does.
1. Prepare the Bread:
Spread a thin layer of mayonnaise on one side of each slice of bread.
This helps achieve a golden, crispy crust. Alternatively, you can use
softened butter.
2. Add Cheese:
On the non-buttered side of one slice, spread a thin layer of Dijon
mustard (if using), and then layer your grated cheese on top. Use about
1 to 2 ounces of cheese per slice.
3. Assemble the Sandwich:
Place the second slice of bread on top, ensuring the buttered/mayo side
is facing outwards.
4. Heat the Pan:
Preheat a nonstick or cast-iron skillet over medium-low heat. Add a
little butter to the pan to coat it evenly.
5. Cook the Sandwich:
Place the sandwich in the skillet and cook for about 2 to 3 minutes
until the bottom is golden brown. Press down lightly with a spatula to
ensure even cooking.
Flip the sandwich carefully and cover the pan with a lid to help melt
the cheese. Cook for another 1 to 3 minutes until the other side is also
golden brown and the cheese has melted.
6. Serve:
Remove the sandwich from the pan, let it cool slightly, then cut in half
and serve warm.
America’s Test Kitchen: America’s Test Kitchen
Use R to compute the following quantities. Include parentheses to clarify the order of operations.
(6^8) / (16^5)
## [1] 1.601807
101^(-8/7)
## [1] 0.005120908
result <- 8 + (3 * pi) - 3 + (4/2 )* -1
result
## [1] 12.42478
Use the built-in functions in R to compute the following quantities.
result <- log(54, base = 3)
result
## [1] 3.63093
result <- exp(exp(sqrt(2)))
result
## [1] 61.14514
result <- (exp(pi) + exp(-pi)) / 2
result
## [1] 11.59195
result <- 119 %/% 25
result
## [1] 4
remainder <- 119 %% 25
remainder
## [1] 19
The objective of this question is to give practice with object assignment and writing a function with one argument.
Recall that the volume of a sphere of radius \(r\) is \(V = \frac{4}{3} \pi r^3\).
vol_1
.vol_1 <- (4/3) * pi * (1^3)
vol_1
## [1] 4.18879
vol_4
.vol_4 <- (4/3) * pi * (4^3)
vol_4
## [1] 268.0826
Write a function called vol_sphere()
that inputs an
argument r
and outputs the volume of a sphere of radius
r
. Set the default of r
to be 1.
vol_sphere <- function(r = 1) {
(4/3) * pi * (r^3)
}
vol_sphere()
(i.e., calling the
function without changing the default argument) computes the same value
as vol_1
.vol_sphere <- function(r = 1) {
(4/3) * pi * (r^3)
}
# Volume of sphere with radius 1
vol_1 <- vol_sphere(1)
# Print the function result (with the default argument, r = 1)
cat("vol_sphere with default r = 1:", vol_sphere(), "\n")
## vol_sphere with default r = 1: 4.18879
# Print the value of vol_1
cat("vol_1 =", vol_1, "\n")
## vol_1 = 4.18879
# Compute the volume for r = 4 using the vol_sphere function
vol_sphere_4 <- vol_sphere(4)
# Print the result from the function for r = 4
cat("vol_sphere with r = 4:", vol_sphere_4, "\n")
## vol_sphere with r = 4: 268.0826
# Print the stored value of vol_4 to compare
cat("vol_4 =", vol_4, "\n")
## vol_4 = 268.0826
The objective of this question is to give practice with writing a function with several arguments and creating local objects.
Write a function called z_prop()
which calculates the
z-statistic given the following values/arguments:
Hint: As a reminder, the z-statistic is given by the formula:
\[ z = \frac{\hat{p} - p_0}{\sqrt{\frac{p_0 (1 - p_0)}{n}}} \]
where \(\hat{p}\) denotes the sample proportion. How is \(\hat{p}\) related to the input arguments?
# Define the z_prop function
z_prop <- function(x, n, p0) {
# Calculate the sample proportion
p_hat <- x / n
# Calculate the z-statistic
z <- (p_hat - p0) / sqrt((p0 * (1 - p0)) / n)
return(z)
}
# Example usage of z_prop
x <- 50 # Number of successes
n <- 100 # Number of trials
p0 <- 0.5 # Hypothesized population proportion
# Calculate the z-statistic
z_statistic <- z_prop(x, n, p0)
cat("The z-statistic is:", z_statistic, "\n")
## The z-statistic is: 0
Suppose that 60% of residents in the city of Pawnee, Indiana, prefer
waffles over pancakes. Out of a sample of 13 Pawnee residents, 10 of
them prefer waffles. Use your z_prop()
function from (a) to
calculate the z-statistic for this sample.
Hint: The input argument p0
is a
proportion, not a percentage.
# Given values for part (b)
x_sample <- 10 # Number of successes (waffle preference)
n_sample <- 13 # Number of trials (sample size)
p0_population <- 0.60 # Hypothesized population proportion (60%)
# Calculate the z-statistic using the z_prop function
z_statistic_b <- z_prop(x_sample, n_sample, p0_population)
# Print the z-statistic
cat("The z-statistic for the sample is:", z_statistic_b, "\n")
## The z-statistic for the sample is: 1.245505
Interpret the value of the z-statistic from (b) in the context of the data.
The z-statistic shows a significant difference between the sample proportion and the hypothesized proportion.
# Given values for part (d)
x_sample_d <- 30 # Number of successes (waffle preference)
n_sample_d <- 39 # Number of trials (sample size)
# Calculate the z-statistic using the z_prop function for the new sample
z_statistic_d <- z_prop(x_sample_d, n_sample_d, p0_population)
# Print the z-statistic for the new sample
cat("The z-statistic for the second sample is:", z_statistic_d, "\n")
## The z-statistic for the second sample is: 2.157277
# Function to calculate monthly lease payment
lease_calc <- function(msrp, price, down, n = 36, res = 0.60, mf = 0.001, tax = 0.095) {
# Calculate capitalized cost (sale price - down payment)
cap_cost <- price - down
# Calculate residual value (MSRP * residual percentage)
residual_value <- msrp * res
# Calculate monthly depreciation ((cap_cost - residual_value) / number of months)
monthly_depreciation <- (cap_cost - residual_value) / n
# Calculate monthly finance charge ((cap_cost + residual_value) * money factor)
monthly_finance_charge <- (cap_cost + residual_value) * mf
# Calculate sub-total (monthly depreciation + monthly finance charge)
sub_total <- monthly_depreciation + monthly_finance_charge
# Calculate total payment (sub-total + taxes)
total <- sub_total * (1 + tax)
# Output the total monthly lease payment
return(total)
}
# Given values for the car lease calculation
msrp <- 31495 # Manufacturer’s suggested retail price
price <- 29895 # Sale price of the car
down <- 2500 # Down payment
n <- 36 # Duration of the lease in months
res <- 0.52 # Residual percentage (52%)
mf <- 0.0016 # Money factor
tax <- 0.095 # Local sales tax in Los Angeles (9.5%)
# Calculate the monthly lease payment using the lease_calc function
monthly_payment <- lease_calc(msrp, price, down, n, res, mf, tax)
# Print the result
cat("The monthly lease payment is:", monthly_payment, "\n")
## The monthly lease payment is: 411.8079
The minimum number of coins is 47:
Total: 5 coins
Start with the biggest coin: Divide the total cents by the largest coin (like quarters). This tells you how many of that coin you need. Keep track of what’s left after using those coins.
Move to the next coin: Take whatever is left and divide by the next biggest coin (like dimes). Again, note how many you need and what’s left.
Keep going: Do the same thing for nickels, then pennies, until there’s nothing left.
Add up the coins: Once you’ve gone through all the coins, just add up how many of each you used. That’s the minimum number of coins.
# Function to calculate the minimum number of coins
get_minimum_coins <- function(cents) {
# Define the coin denominations
quarters <- 25
dimes <- 10
nickels <- 5
pennies <- 1
# Calculate number of quarters
num_quarters <- cents %/% quarters
remaining_cents <- cents %% quarters
# Calculate number of dimes
num_dimes <- remaining_cents %/% dimes
remaining_cents <- remaining_cents %% dimes
# Calculate number of nickels
num_nickels <- remaining_cents %/% nickels
remaining_cents <- remaining_cents %% nickels
# Calculate number of pennies
num_pennies <- remaining_cents %/% pennies
# Total number of coins
total_coins <- num_quarters + num_dimes + num_nickels + num_pennies
return(total_coins)
}
# Test cases
cat("(i) 31 cents: ", get_minimum_coins(31), "coins\n")
## (i) 31 cents: 3 coins
cat("(ii) 48 cents: ", get_minimum_coins(48), "coins\n")
## (ii) 48 cents: 6 coins
cat("(iii) 139 cents: ", get_minimum_coins(139), "coins\n")
## (iii) 139 cents: 10 coins
# calculate the number of coins for each cent value from 1 to 99
cents <- 1:99
coin_counts <- sapply(cents, get_minimum_coins)
# Find the maximum number of coins
max_coins <- max(coin_counts)
# Find the corresponding cents value using match()
max_cents <- cents[match(max_coins, coin_counts)]
cat("The number of cents under 100 that requires the most coins is:",
max_cents, "with", max_coins, "coins.\n")
## The number of cents under 100 that requires the most coins is: 94 with 9 coins.
exp1(): Uses the global values of x and y. exp2(): Uses the local value of x (x=2) and global value y.
exp_y() returns a new function without calculating anything immediately.Only when we provide a value for y then it calculates x^y.
Outer function: exp_y(x) takes a number x as an argument.
Inner function: Inside exp_y, there’s another function that takes a different number y. This inner function does the actual operation x^y.
exp_y <- function(x) {
function(y) {
x^y
}
}
\[15^4\]
exp_y(15)(4)
## [1] 50625