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).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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))

Expert Solution
Step 1

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]

 

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Analysis of Performance Measurement
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education