Професионална програма
Loading...
+ Нов въпрос
V3lin avatar V3lin 1 Точки

Problem 2 - Treasure Hunt

Здравейте, 

Решавам следната задача от Mid exam Retake:

The pirates need to carry a treasure chest safely back to the ship, looting along the way.

Create a program that manages the state of the treasure chest along the way. On the first line, you will receive the initial loot of the treasure chest, which is a string of items separated by a "|".

"{loot1}|{loot2}|{loot3} … {lootn}"

The following lines represent commands until "Yohoho!" which ends the treasure hunt:

  1. "Loot {item1} {item2}…{itemn}":
    • Pick up treasure loot along the way. Insert the items at the beginning of the chest.
    • If an item is already contained, don't insert it.
  2. "Drop {index}":
    • Remove the loot at the given position and add it at the end of the treasure chest.
    • If the index is invalid, skip the command.
  3. "Steal {count}":
    • Someone steals the last count loot items. If there are fewer items than the given count, remove as much as there are.
    • Print the stolen items separated by ", ":

"{item1}, {item2}, {item3} … {itemn}"

In the end, output the average treasure gain, which is the sum of all treasure items length divided by the count of all items inside the chest formatted to the second decimal point:

"Average treasure gain: {averageGain} pirate credits."

If the chest is empty, print the following message:

"Failed treasure hunt."

Input

  1. On the 1st line, you are going to receive the initial treasure chest (loot separated by "|")
  2. On the following lines, until "Yohoho!", you will be receiving commands.

Output

  1. Print the output in the format described above.

Constraints

  1. The loot items will be strings containing any ASCII code.
  2. The indexes will be integers in the range [-200200]
  3. The count will be an integer in the range [1….100]

Examples

Input

Output

Gold|Silver|Bronze|Medallion|Cup

Loot Wood Gold Coins

Loot Silver Pistol

Drop 3

Steal 3

Yohoho!

Medallion, Cup, Gold

Average treasure gain: 5.40 pirate credits.

Comments

The first command "Loot Wood Gold Coins" adds Wood and Coins to the chest but omits Gold since it is already contained. The chest now has the following items:

Coins Wood Gold Silver Bronze Medallion Cup

The second command adds only Pistol to the chest

The third command "Drop 3" removes the Gold from the chest, but immediately adds it at the end:

Pistol Coins Wood Silver Bronze Medallion Cup Gold

The fourth command "Steal 3" removes the last 3 items Medallion, Cup, Gold from the chest and prints them.

In the end calculate the average treasure gain which is the sum of all items length Pistol(6) + Coins(5) + Wood(4)  + Silver(6) + Bronze(6) = 27 and divide it by the count 27 / 5 = 5.4 and format it to the second decimal point.

 

Input                                                                                            Output

Diamonds|Silver|Shotgun|Gold

Loot Silver Medals Coal

Drop -1

Drop 1

Steal 6

Yohoho!

Coal, Diamonds, Silver, Shotgun, Gold, Medals

Failed treasure hunt.

Example input и output минават, но Judge ми дава 30/100 и не мога да открия грешката.

Линк към Judge: https://judge.softuni.org/Contests/Practice/Index/1773#1

Линк към кодa ми: https://pastebin.com/VWx9BXMj

Моля за съдействие.

Благодаря предварително

Тагове:
1
Python Fundamentals 23/11/2021 13:11:07
krum_43 avatar krum_43 670 Точки

Здравей колега,

Не съм фен на змийският език Python,но имам работещ код на C#.

Mожеш да го погледнеш ако желаеш.

Лек и успешен ден.

0
icowwww avatar icowwww 1676 Точки

Здравей,

Това не вярно във всички случай:

if item not in loot and item != command[0]:

Пропусни първия елемент само:

if i == 0:
    continue

Премахни новия ред при принтирането

Тук трябва да има едно равно

if n >= len(loot):

При достигане на празен chest директно принтирай съобщението и прекъсни програмата:

if n >= len(loot):
    removed = loot
    print(', '.join(removed))
    print('Failed treasure hunt.')
    exit()

https://pastebin.com/PvP1dbXz

0
24/11/2021 19:04:03