Coding/Basic Skills(Python)

[Programmers, Level 1] Cooking Hamburger 햄버거 만들기

xeohyuni 2023. 6. 22. 22:31

Description:

상수 working at a hamburger restaurant is responsible for packaging the hamburgers. The other employees who work together cook the ingredients that will go into the hamburgers. The cooked ingredients are stacked in front of 상수 in the order they are prepared, from bottom to top. The 상수 then moves the stacked ingredients separately to package the completed hamburgers. The 상수's job is to package hamburgers that are stacked in a predetermined order (from bottom to top: bread - vegetables - meat - bread). Since 상수is very fast, there are no additional ingredients coming in while 상수 is packaging. The height of the ingredients is disregarded, so there are no cases where the task becomes difficult due to the height of the ingredients.

For example, if the order of ingredients stacked in front of 상수 is [vegetables, bread, bread, vegetables, meat, bread, vegetables, meat, bread], 상수 will package a hamburger using the third ingredient up to the sixth ingredient when the sixth ingredient is stacked. And when the ninth ingredient is stacked, 상수 will package a hamburger using the second ingredient and the seventh ingredient up to the ninth ingredient. In other words, two hamburgers will be packaged.

Given an integer array 'ingredients' representing the information of the ingredients passed to 상수, complete the 'solution' function to return the number of hamburgers 상수will package.

Restrictions:

  • 1 ≤ The length of 'ingredient' ≤ 1,000,000.
  • The elements of 'ingredient' are either 1, 2, or 3, representing bread, vegetables, and meat, respectively, in order.

Input/Output Example:

Ingredient Result
[2, 1, 1, 2, 3, 1, 2, 3, 1] 2
[1, 3, 2, 1, 2, 1, 3, 1, 2] 0

Explanation Input(1):

It is already explained in the description

Explanation Input(2):

There are no hamburgers that the 상수 can package.

Code:

def solution(ingredient):
    answer = 0
    hbg = []
    for igt in ingredient:
        hbg.append(igt)
        if hbg[-4:] == [1,2,3,1]:
            answer += 1
            del hbg[-4:]
    return answer

Discussion:

The provided code aims to determine the number of hamburgers that can be packaged by the constant working at the hamburger restaurant. It uses a list called ingredient to represent the order of ingredients received by the constant. The code initializes an answer variable to keep track of the number of hamburgers packaged.

To determine the number of hamburgers, the code iterates through each element in the ingredient list. For each ingredient, it is added to a temporary list called hbg. The code then checks if the last four elements in hbg match the pattern [1, 2, 3, 1], which represents a complete hamburger (bread - vegetables - meat - bread).

If the pattern is matched, it means a complete hamburger can be packaged. In this case, the code increments the answer variable by 1 to keep track of the total number of hamburgers packaged. Additionally, the last four elements are removed from hbg to make space for the next set of ingredients.

The iteration continues until all elements in the ingredient list have been processed. Finally, the code returns the value of answer, representing the total number of hamburgers that can be packaged by the constant.

In summary, the code uses a sliding window approach to identify the pattern of ingredients corresponding to a complete hamburger. By tracking the number of times this pattern occurs, it determines the total number of hamburgers that can be packaged.