1) Problem description:
Suppose you are managing files. You are given a text file, where in the first line are storage's volume of the server and the amount of users in total that is in the text file. Each line from the second to the end shows volume of individual user's storage. Based on the server's volume limit you need to find the maximum amount of users that can be fit in the database and the maximum volume among them.
100 4 80 30 50 40
3) Sample output:
4) Sample output's explanation: The maximum amount of users that can be fit in the database is
2 (more than 2 will be more than
100 in total). All possible pairs, which meet volume's requirements, are
50 40 (each makes less than
100). The maximum number among these pairs is
9691 1894 89 24 33 11 ...
2) My solution:
fName = open('../root_files/26_7791633.txt') param = fName.readline().split() data = [int(el) for el in fName.readlines()] fName.close() TARGET = int(param) data.sort() target_disposable = TARGET max_num = 0 quantity = 0 # finding maximum amount while target_disposable - data[quantity] >= 0: target_disposable -= data[quantity] quantity += 1 new_list = data[0:quantity] element_in_the_end = new_list.pop() remainder = TARGET - sum(new_list) # finding the very maximum number based on quantity for i in reversed(range(remainder + 1)): if i > element_in_the_end: max_num = i break print(quantity, max_num)
34 601 Solution accepted Runtime: 55ms
4) My question: Is there a way to make this code more concise, maybe even improving its runtime? Thank you in advance.