Hanwool Codes RSS Tag Admin Write Guestbook
전체 글 (45)
2023-02-22 05:00:19

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)
2023-02-21 06:13:03

itertools.product() | HackerRank

 

itertools.product() | HackerRank

Find the cartesian product of 2 sets.

www.hackerrank.com

 

Goal

You are given a two lists  A and B. Your task is to compute their cartesian product A x B by using itertools.product()

 

Solution

 

The script you provided utilizes the Python itertools library to generate the Cartesian product of two lists A and B, where each element in A is paired with each element in B.

The resulting pairs are printed to standard output.

The script begins by importing the product function from the itertools library.

The product function generates the Cartesian product of multiple iterables, which in this case are the lists A and B.

Next, the script prompts the user to input the elements of list A and list B.

The input() function takes user input as a string, and the map() function applies the int() function to each element of the resulting string to convert them into integers.

The resulting lists of integers are assigned to the variables A and B, respectively.

The product function is then called with A and B as arguments.

The resulting pairs are stored in the result variable as a list of tuples.

The script then iterates over the result list using a for loop.

The range() function is used to iterate from 0 to the length of result minus 1.

For each iteration, the current tuple in result is printed to standard output with a space separator between its elements. The end parameter of the print() function is set to a single space, so that each printed tuple is followed by a space instead of a newline character.

When the loop reaches the last tuple in result, the end parameter is not set, so the final tuple is printed on its own line.

 

In summary, the script generates the Cartesian product of two lists using the product function from the itertools library and prints the resulting pairs to standard output. This script can be useful in scenarios where it is necessary to combine every element in one list with every element in another list, such as when generating all possible combinations of variables in a simulation.

 

# itertools.product()
# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import product

A = list(map(int, input().split()))
B = list(map(int, input().split()))

result = list(product(A,B))

for i in range(len(result)):
    
    if i < len(result) - 1 :
        print(result[i], end=' ')
    else:
        print(result[i])
2023-02-19 16:11:52

https://www.hackerrank.com/challenges/capitalize/problem

 

Capitalize! | HackerRank

Capitalize Each Word.

www.hackerrank.com

 

Goal

 

You are asked to ensure that the first and last names of people begin with a capital letter in their passports.

For example, alison heck should be capitalised correctly as Alison Heck.


Given a full name, your task is to capitalize the name appropriately.

 

 

Solution

 

The solve function takes a string s as an input and returns a new string where the first letter of each word in the original string is capitalized.

The function first initializes an empty list called new_string.

Then, it uses the split method to split the input string s into a list of individual words. The split method separates the string at each occurrence of a space character, creating a list of strings.

Next, the function iterates through each word in the list, capitalizes the first letter of the word using the capitalize() method, and adds the modified word to the new_string list.

Finally, the function joins the list of capitalized words into a single string using the join method, with a space character as the separator, and returns the resulting string.

 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the solve function below.
def solve(s):
    
    new_string = []
    
    for name in s.split(' '):
        new_string.append(name.capitalize())
    
    
    return ' '.join(new_string)
    
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = solve(s)

    fptr.write(result + '\n')

    fptr.close()


Hanwool Codes. Designed by 코딩재개발.