https://www.hackerrank.com/challenges/collections-counter/problem
collections.Counter() | HackerRank
Use a counter to sum the amount of money earned by the shoe shop owner.
www.hackerrank.com
Goal
You are given X number of shoes and a list containing the size of each shoe.
Additoinally, you are give N number of customers who are willing to pay X(i) amount of money only if they get the shoe their desired size.
So, you need to compute how much money you can earn.
You need to use collection.Counter() function to solve this task.
Solution
The script you provided uses the collections library in Python to solve a problem related to sales of shoes.
The problem involves keeping track of the available shoe sizes and the prices of shoes, and then calculating the total amount of money earned by selling shoes to a group of customers.
The script starts by importing the Counter function from the collections library.
The Counter function is used to count the frequency of elements in a list.
Next, the script prompts the user to input the number of shoes in stock and their sizes. The input() function is used to take user input, and the int() and map() functions are used to convert the resulting string of shoe sizes into a list of integers.
The number of shoes in stock is assigned to the variable n_shoes, and the list of shoe sizes is assigned to the variable shoe_sizes.
The script then prompts the user to input the number of customers and the size and price of the shoes they wish to purchase.
The input() function is used to take user input, and the int() and map() functions are used to convert the resulting string of size and price into two separate integers.
The number of customers is assigned to the variable n_customers, and the total price of all shoes sold is assigned to the variable total_price.
A for loop is then used to iterate over each customer.
For each customer, a Counter object is created using the shoe_sizes list. The Counter object counts the frequency of each shoe size in the shoe_sizes list.
The customer's desired shoe size and its price are then read from standard input and assigned to the variables size and price, respectively.
If the desired shoe size is in the shoe_sizes list, the price of the shoe is added to the total_price variable, and the desired shoe size is removed from the shoe_sizes list using the remove() method.
Finally, the total price of all shoes sold is printed to standard output using the print() function.
In summary, the script uses the Counter function from the collections library to count the frequency of shoe sizes in a list of shoes, and uses a for loop to sell shoes to customers and calculate the total price of all shoes sold. This script can be useful in scenarios where it is necessary to keep track of inventory and sales in a retail environment.
# collections.Counter()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import Counter
n_shoes = int(input())
shoe_sizes = list(map(int, input().split()))
n_customers = int(input())
total_price = 0
for i in range(n_customers):
shoes_collection = Counter(shoe_sizes)
size, price = list(map(int, input().split()))
if size in shoes_collection.keys():
total_price += price
shoe_sizes.remove(size)
print(total_price)
'software Engineering > (Python)HackerRank' 카테고리의 다른 글
[Python] HackerRank - Introduction to Sets (0) | 2023.02.24 |
---|---|
[Python] HackerRank - itertools.permutations() (0) | 2023.02.23 |
[Python] HackerRank - itertools.product() (0) | 2023.02.21 |
[Python] HackerRank - Capitalize! (0) | 2023.02.19 |
[Python] HackerRank - String Formatting (0) | 2023.02.18 |