17
1
Oof! You've been coding the whole day and you even had no time for Stack Exchange!
Now, you just want to rest and answer some questions. You have T minutes of free time. You enter the site and see N new questions. To write an answer for each you'll need ti minutes. Of course, as a dedicated reputation gatherer, you want to answer as many questions as you can.
Can you write a program to calculate which questions do you have to answer to write maximum posts in T minutes?
Input
First line of input consists T
minutes you have for answering, and N
, how many new questions are on the site.
The second line has N numbers: time you need to answer qi question.
Output
Write either an array or numbers split with space: indexes of questions(counting from 0 or 1 - what is better for you) you should answer in order to write as many answers as you can. If you can't answer any questions, write nothing or anything to express that it's impossible. If there are several variants, write any.
Examples
Inputs Possible outputs
60 5
30 5 10 20 3
0 1 2 4
, 0 1 3 4
or 1 2 3 4
10 5
1 9 5 7 2
0 2 4
or 0 3 4
5 5
1 1 1 1 1
0 1 2 3 4
60 5
48 15 20 40 3
1 2 4
or 1 3 4
5 1
10
1 0
And of course it's code-golf, so the shortest code in bytes wins.
One easy golf is to use only a single space for the indents: https://tio.run/##TYxNCsIwFIT3OcVbJvgoqaWKxZykZBFJgmmb9C9Ci3j2GrtQYYZhvoEZ1njvQ7Ft2liwtMWOVQRG9KKWyAnYfgKHDbgAcz9Fo@nTVc0/NuHhzaSioR17ZS4aP1OGrVlFp/xNK1iqpc7l5xb8QTQpnAV/FW01ZmoYTNDUMQLD5EKkI9ssPXGsC45QIuQpjsmFZMTS1Ooc4bJP57TstNzhT1/IJdve
– 79037662 – 2019-11-28T19:40:02.057Wouldn't that be -7 bytes? – Redwolf Programs – 2019-11-28T19:42:36.867
1@game0ver Oh, nvm...you originally had -1 total in the message. I only counted -1 byte per indent level instead of -3. – Redwolf Programs – 2019-11-28T20:11:11.103
You can replace
dict(enumerate(l)).items()
with justenumerate(l)
to save 14 bytes. You're converting alist
oftuple
s to adict
, then iterating over thedict_item
s, and converting each one back to a tuple, which is pretty roundabout. – mypetlion – 2019-11-28T21:52:45.683@mypetlion Good point, thanks! – game0ver – 2019-11-28T22:01:04.270
@JoKing Thank you very much, that's better now! – game0ver – 2019-11-28T22:35:13.070
In the Python 3 answer, why is the function only taking two arguments instead of 3? – justhalf – 2019-11-29T10:42:03.817
@justhalf Simply because it doesn't need the 3rd parameter and as per the rules it can be omitted. Actually almost none of the answers provided use the 3rd parameter.
– game0ver – 2019-11-29T10:45:19.177Cool, didn't know that. Here's -5 bytes for you by modifying the if into array multiplication :)
– justhalf – 2019-11-29T10:49:00.310@justhalf Thanks! That's a cool trick! – game0ver – 2019-11-29T10:53:45.143
1
Another -3 bytes by modifying
– justhalf – 2019-11-29T11:00:10.877k
instead ofm
. This trick can be applied to the Python 3.8 solution, too. Reducing it by 4 bytes by removingm
.Python 3.8 75 bytes – justhalf – 2019-11-29T11:04:13.727
1@justhalf That's brilliant, never crossed my mind to modify
k
instead :) Thank you! – game0ver – 2019-11-29T11:06:13.070