Coding/Basic Skills(Python)

[Programmers, Level 1] 푸드 파이트 대회 Food Fight Contest

xeohyuni 2023. 7. 16. 00:50

Description:

Suwoong is organizing a food fight competition every month where participants have to eat the given food quickly. In this competition, the players compete against each other one-on-one, and the type and quantity of food change with each match. The competition proceeds by arranging the prepared food in a row, with one player starting from the left and eating the food from left to right, while the other player starts from the right and eats the food from right to left. In the center, water is placed, and the player who drinks water first wins.
In order to ensure fairness in the competition, the types and quantities of food eaten by the two players must be the same, and the order of eating the food must also be the same. In addition, starting from this competition, they want to arrange the low-calorie food to be eaten first to enable the players to eat the food better. Suwoong has ordered the food for this competition, but due to not considering the conditions of the competition, some of the food cannot be used in the competition.

For example, if there are three types of food prepared, with the order of increasing calories: 3 portions of food 1, 4 portions of food 2, and 6 portions of food 3. For convenience, let's call water 0. In this case, both players will eat 1 portion of food 1, 2 portions of food 2, and 3 portions of food 3. So the arrangement of food will be "1223330333221". Therefore, 1 portion of food 1 cannot be used in the competition.

Given an integer array called food that represents the quantity of food prepared by Suwoong in the order of increasing calories, complete the solution function to return the string representing the arrangement of food for the competition.


Restrictions:

  • 2 ≤ Length of food ≤ 9
  • 1 ≤ Each element of food ≤ 1,000
  • food contains the quantity of food in the order of increasing calories.
  • food[i] represents the quantity of the 'i'th food.
  • food[0] represents the quantity of water prepared by Suwoong and is always 1.
  • The input will only contain cases where the length of the correct answer is at least 3.

Input/Output Example:

Food Result
[1, 7, 1, 2] "111303111"

 

Explanation:

The two players will eat 3 portions of food 1 and 1 portion of food 3. Therefore, the arrangement of food will be "111303111".

 

Code:

def solution(food):
    answer = ''
    for i in range(1,len(food)):
        answer += str(i)*(food[i]//2)
    temp = ''.join(reversed(list(answer)))
    return answer+'0'+temp

Explanation: 

The function takes an input list called food, which represents the quantity of food prepared in the order of increasing calories. The first element of the food list represents water, and it is always set to 1.

The code iterates over the food list starting from the second element. For each food item, it calculates half of its quantity (food[i] // 2) and appends the string representation of the index i repeated that many times to the answer string. This ensures that lower-calorie food items are consumed first.

After the loop, the code reverses the answer string and stores it in the temp variable. Finally, the function returns the concatenation of the answer string, a '0' (representing water in the center), and the reversed temp string. This represents the final arrangement of food for the competition, where the players eat from both ends towards the center.

 

It was important to undertsand the main pattern/arrangement of given direction and the use of 'i'(the element) in the function.