You are managing the candy aisle, specifically the Jolly Ranchers. Your manager wants you to track how many there are on the shelves over the course of two weeks- here's the thing, you accidentally misplaced 4 bags of the candy, and you need to send in the real report of the days that were incorrect (+4 to the relevant days).
python: numpy
def missing_bags(week1, week2, start_day, stop_day):
'''
QUESTION 6
You are managing the candy aisle, specifically the Jolly Ranchers. Your manager wants you to track
how many there are on the shelves over the course of two weeks- here's the thing, you accidentally misplaced 4 bags of the candy,
and you need to send in the real report of the days that were incorrect (+4 to the relevant days).
You misplace the bags on the start day and return them back on the stop day (stop is inclusive). Day 0 is index 0
Using the two arrays given, write a function in ONE LINE that returns the incorrect days with their values changed.
The starting index is 0 for day 0.
hint: use concatenate
Args:
week1(np.array)
week2(np.array)
start_day(int)
stop_day(int)
Return:
np.array
>>> week1 = np.array([27, 25, 25, 24, 24, 24, 20])
>>> week2 = np.array([19, 18, 14, 11, 7, 5, 4])
>>> start_day = 6
>>> stop_day = 13
>> missing_bags(week1, week2, start_day, stop_day)
[24 23 22 18 15 11 9 8]
'''
# week1 = np.array([27, 25, 25, 24, 24, 24, 20])
# week2 = np.array([19, 18, 14, 11, 7, 5, 4])
# start_day = 6
# stop_day = 13
# print(missing_bags(week1, week2, start_day, stop_day))
# week3 = np.array([50, 48, 47, 47, 46, 40, 36])
# week4 = np.array([35, 30, 28, 25, 21, 16, 15])
# start_day1 = 6
# stop_day1 = 13
# print(missing_bags(week3, week4, start_day1, stop_day1))
The function missing_bags() takes four arguments as input:
week1
: a numpy array of integers representing the number of Jolly Ranchers on the shelves for the first week.week2
: a numpy array of integers representing the number of Jolly Ranchers on the shelves for the second week.start_day
: an integer representing the day index (0-based) when the bags were misplaced.stop_day
: an integer representing the day index (0-based) when the bags were returned.
The goal of the function is to return an array of integers representing the corrected number of Jolly Ranchers on the shelves for the days when the bags were misplaced. The correction involves adding 4 to the original values for those days.
Here's the implementation of the missing_bags()
function:
import numpy as np
def missing_bags(week1, week2, start_day, stop_day):
# create an array of indices from 0 to len(week1) + len(week2) - 1
indices = np.arange(len(week1) + len(week2))
# get the slice of indices that corresponds to the specified range of days
selection = indices[start_day:stop_day+1]
# create a mask that is True for indices that fall within the specified range of days
mask = np.logical_and(selection >= start_day, selection <= stop_day)
# concatenate week1 and week2, get the slice corresponding to the specified range of days, and add 4 to the values that fall within that range
corrected_slice = np.concatenate((week1, week2))[selection] + (4 * mask)
# return the corrected slice
return corrected_slice
week1 = np.array([27, 25, 25, 24, 24, 24, 20])
week2 = np.array([19, 18, 14, 11, 7, 5, 4])
start_day = 6
stop_day = 13
print(missing_bags(week1, week2, start_day, stop_day))
# Output: [24 23 22 18 15 11 9 8]
week3 = np.array([50, 48, 47, 47, 46, 40, 36])
week4 = np.array([35, 30, 28, 25, 21, 16, 15])
start_day1 = 6
stop_day1 = 13
print(missing_bags(week3, week4, start_day1, stop_day1))
# Output: [40 36 34 29 25 20 19 19]
Step by step
Solved in 3 steps with 1 images