[Python] A program that finds the maximum number of toys that can be purchased with your money

[Python] A program that finds the maximum number of toys that can be purchased with your money

This is a memo for myself.

▼ Question

--A list containing the prices of each toy is given. --Calculate the maximum number of toys that can be purchased with your money (k).

URL

▼sample input

python


prices = [1,12,5,111,200,1000,10]
k=50

▼sample output

python


4

▼my answer

python


def maximumToys(prices, k):
    ans=total=0
    if min(prices) > k:
        return 0
    for price in sorted(prices):
        total += price
        if total <= k:
            ans+=1
    return ans

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    nk = input().split()
    n = int(nk[0])
    k = int(nk[1])
    prices = list(map(int, input().rstrip().split()))
    result = maximumToys(prices, k)
    fptr.write(str(result) + '\n')
    fptr.close()

Consider patterns that you cannot buy even one.

Recommended Posts

[Python] A program that finds the maximum number of toys that can be purchased with your money
[Python] A program to find the number of apples and oranges that can be harvested
[Python] A program that calculates the number of socks to be paired
[Python] A program that finds the shortest number of steps in a game that crosses clouds
[Python] A program that calculates the number of chocolate segments that meet the conditions
[Python] A program that finds a pair that can be divided by a specified value
Understand the probabilities and statistics that can be used for progress management with a python program
[Python] A program that finds the minimum and maximum values without using methods
[Python] A program that calculates the number of updates of the highest and lowest records
[Python] A program that compares the positions of kangaroos.
[Python] A program that finds the most common bird types
Create a program that can generate your favorite images with Selenium
[Python] Make a graph that can be moved around with Plotly
I made a shuffle that can be reset (reverted) with Python
The story of making a module that skips mail with python
[Python] A program that rotates the contents of the list to the left
Create a compatibility judgment program with the random module of python.
Predict the number of cushions that can be received as laughter respondents with Word2Vec + Random Forest
[Python] A program that creates stairs with #
[Python] A program that rounds the score
Maximum number of function parameters that can be defined in each language
[Python] Note: A self-made function that finds the area of the normal distribution
[Python] Code that can be written with brain death at the beginning when scraping as a beginner
Calculate the total number of combinations with python
[Python] The movement of the decorator that can be understood this time ② The decorator that receives the argument
A class for PYTHON that can be operated without being aware of LDAP
Around the authentication of PyDrive2, a package that operates Google Drive with Python
I made a familiar function that can be used in statistics with Python
[Python] Precautions when finding the maximum and minimum values in a numpy array with a small number of elements
A python script that gets the number of jobs for a specified condition from indeed.com
How to identify the element with the smallest number of characters in a Python list?
[Python] I examined the practice of asynchronous processing that can be executed in parallel with the main thread (multiprocessing, asyncio).
A script that can perform stress tests according to the number of CPU cores
A program that summarizes the transaction history csv data of SBI SECURITIES stocks [Python3]
[Python] Introduction to web scraping | Summary of methods that can be used with webdriver
A memo that I touched the Datastore with python
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Can be used with AtCoder! A collection of techniques for drawing short code in Python!
A Python script that allows you to check the status of the server from your browser
Format summary of formats that can be serialized with gensim
Is your dll a dll that can take advantage of multi-core?
Run the intellisense of your own python library with VScode.
Get the number of specific elements in a python list
Get a list of purchased DMM eBooks with Python + Selenium
[Homology] Count the number of holes in data with Python
I investigated the pretreatment that can be done with PyCaret
[Golang] A program that determines the turn with random numbers
Let's make a diagram that can be clicked with IPython
A Python script that compares the contents of two directories
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
[Python] Draw elevation data on a sphere with Plotly and draw a globe that can be rotated round and round
The mystery of the number that can be seen just by arranging 1s-The number of repunits and mysterious properties-
Here's a summary of things that might be useful when dealing with complex numbers in Python
I bought and analyzed the year-end jumbo lottery with Python that can be executed in Colaboratory
About the matter that torch summary can be really used when building a model with Pytorch
The story of creating a bot that displays active members in a specific channel of slack with python
I made a package that can compare morphological analyzers with Python
Save the result of the life game as a gif with python
Investigation of DC power supplies that can be controlled by Python
[python, ruby] fetch the contents of a web page with selenium-webdriver
A story that struggled to handle the Python package of PocketSphinx