Egorsky
Egorsky

Reputation: 333

How to make a list of a minimum values only

There's a task. The user enters 's' values which represent the amount of the available space in the imaginary database, after that 'n' the number of users should be inputted. After this action user inputs the amount of "space" each user requires. The program should calculate how many users can fit the database. My logic was the following: First of all, after the "space requirement" the input list of inputs gets sorted. Then 'minn' list appears to gather the values in the future. Second, if the sum of the required space equals to 's' it prints the number of users 'n'. If not it should go through all the items in 'numUsers' and if the sum of them is less then 's' they should be appended to the list 'minn'. But this is not working.

s, n = list(map(int, input().split()))
numUsers = []
count = 0
for i in range(n):
    numUsers.append(int(input()))
    
numUsers.sort()

minn = []

if sum(numUsers) == s:
    print(n)
elif (s - sum(numUsers) < 0):
    for i in numUsers:
        while sum(minn) < s:
            minn.append(i)
            count += 1
print(count)

for the input:

According to the logic, I have 130 as sum(numUsers) which is way bigger than 's'. It is sorted so it must be [30, 50, 50]. Probably there's something goes wrong after it gets 90 or something but I can't figure out what exactly. I'm somehow getting 4 instead of 2. Can you please give me advice on what is wrong with the code or with the logic I used in solving the task. Thank you very much.

Upvotes: 0

Views: 120

Answers (2)

Egorsky
Egorsky

Reputation: 333

Possible changes:

summ = 0
newList = sorted(numUsers)

for i in range(len(newList)):
    summ += newList[i]
    count += 1
    if summ > s:
        count -= 1
        break
print(count)

Upvotes: 0

trincot
trincot

Reputation: 351218

This expression:

while sum(minn) < s:

...does not include the number you are about to add, so the condition should really be:

sum(minn) + i <= s:

Then, the while loop could potentially add the same value of i twice or more to the minn list, so it should just be an if.

if sum(minn) + i <= s:

There is another problem as well: when sum(numUsers) is less than s, your code returns 0.

You should change this:

if sum(numUsers) == s:
    print(n)
elif (s - sum(numUsers) < 0):

to this:

if sum(numUsers) <= s:
    print(n)
else:

... but you might even decide to skip those three lines completely, and just use what is in the else block.

Also, you do not really use minn for the output, so you could just keep a running sum instead of a list.

To avoid confusion, here is the full code with the above mentioned changes:

def solve(s, n, numUsers):
    numUsers.sort()
    count = 0
    runningSum = 0
    for i in numUsers:
        if runningSum + i > s:
            break
        runningSum += i
        count += 1
    return count

Example call:

s = 100 
n = 2
numUsers = [200, 30]
print(solve(s, n, numUsers))  # 1

Upvotes: 3

Related Questions