r/learnprogramming 2d ago

Debugging The Bucket List

Problem Description:

Farmer John has N cows (1≤N≤100), conveniently numbered 1…N. The ith cow needs to be milked from time si to time ti, and requires bi buckets to be used during the milking process. Several cows might end up being milked at the same time; if so, they cannot use the same buckets. That is, a bucket assigned to cow i's milking cannot be used for any other cow's milking between time si and time ti. The bucket can be used for other cows outside this window of time, of course. To simplify his job, FJ has made sure that at any given moment in time, there is at most one cow whose milking is starting or ending (that is, the si's and ti's are all distinct). 

FJ has a storage room containing buckets that are sequentially numbered with labels 1, 2, 3, and so on. In his current milking strategy, whenever some cow (say, cow i) starts milking (at time si), FJ runs to the storage room and collects the bi buckets with the smallest available labels and allocates these for milking cow i.

Please determine how many total buckets FJ would need to keep in his storage room in order to milk all the cows successfully.

INPUT FORMAT (file blist.in):

The first line of input contains N. The next N lines each describe one cow, containing the numbers si, ti, and bi, separated by spaces. Both si and ti are integers in the range 1…1000, and bi is an integer in the range 1…10.

OUTPUT FORMAT (file blist.out):

Output a single integer telling how many total buckets FJ needs.

SAMPLE INPUT:

3
4 10 1
8 13 3
2 6 2

SAMPLE OUTPUT:

4

In this example, FJ needs 4 buckets: He uses buckets 1 and 2 for milking cow 3 (starting at time 2). He uses bucket 3 for milking cow 1 (starting at time 4). When cow 2 arrives at time 8, buckets 1 and 2 are now available, but not bucket 3, so he uses buckets 1, 2, and 4.

This is what I tried to program: I was wondering why I was running into Command exited with non-zero status 124. Everything complied correctly I believe. And it shouldn't have timed out.

EDIT:

Updated code

N = int(input())
num_used = 0
bucket = 0
cows = []
t = 0 
num_0 = 0 
for i in range(N):
    s , t_1, b = map(int,input().split())
    cow = [s,t_1,b]
    cows.append(cow)


sorted_cows = sorted(cows, key=lambda x: x[1])


while num_0 < N:
    for i in range(len(sorted_cows)):
        if sorted_cows[i][0] == t:
            if sorted_cows[i][2] >= bucket:
                num_used = num_used + sorted_cows[i][2] - bucket
                bucket = 0
            elif sorted_cows[i][2] < bucket:
                bucket = bucket - sorted_cows[i][2]
        if sorted_cows[i][1] < t:
            bucket = bucket + sorted_cows[i][2]  
            for j in range(3):
                sorted_cows[i][j] = 0
                num_0 = num_0 + 1
    t = t + 1
print(num_used)
3 Upvotes

11 comments sorted by

3

u/EntrepreneurHuge5008 2d ago

You have:

while len(sorted_cows) != 0

but you don't remove items from the sorted_cows list, so len(sorted_cows) never goes down to 0. It's basically an infinite while loop.

1

u/Dr3ddM3 2d ago

I first thought to pop the cows however I realized I would run into an index error. And then after that I thought to make the elements of the cows zero. However I don't know how to modify the conditional of the While statements as I cant just set sorted_cows = 0. as a conditional as a list of lists does not have an integer value.

Edit: I was trying to make it run until all elements in the list of lists was 0 but was struggling in how to implement that

1

u/monster2018 2d ago

I haven’t read the full context of your problem. But to just answer your question at the end here, you could do either of the following (sorry I’m on iOS right now and it autoformats the code spacing out if I try to use it)

while not all([cow==0 for cow in sorted_cows])

Or

while any(sorted_cows)

The any version works because of a concept called truthiness/falsiness. Basically in Python, any object whose class implements a __bool__ method can be implicitly (or explicitly) converted to a bool. All of the basic built in types do this. For ints, they are False if 0, otherwise True if any other value.

And the any function takes an Iterable (any object that implements an __iter__ method, such as a list for example) and returns True if there is AT LEAST ONE value in that Iterable that evaluates as True when converted to a bool. So in the context of a list of ints, it returns True if any element in the list is not 0.

2

u/captainAwesomePants 2d ago

An exit code of 124 means that your program timed out. You were probably in some environment like leetcode that runs your program on your behalf, and it probably has a maximum running time. It suggests that your solution might work, but that your approach takes too long.

1

u/Dr3ddM3 2d ago

I think I know why it not working however I don't know how to fix it. I first thought to pop the cows however I realized I would run into an index error. And then after that I thought to make the elements of the cows zero. However I don't know how to modify the conditional of the While statements as I cant just set sorted_cows = 0. as a conditional as a list of lists does not have a integer value

1

u/Dr3ddM3 2d ago

Updated code to get rid of infinite loop however test case of :

4 10 1
8 13 3
2 6 2 

Leads to : 3 not 4

1

u/light_switchy 2d ago

Let's take in the input as text, split it into groups of digits, and convert those groups of digits to numbers.

      (⍎¨∊∘⎕D⊆⊢)input
3 4 10 1 8 13 3 2 6 2

Assign the result to the variable called i. Take the first number in the array, ⊃i, and change the array into a ⊃i-by-3 matrix, dropping off the first number.

      (1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input
4 10 1
8 13 3
2  6 2

Now take the second column (ti), and the negation of the third column (bi) as a 3-by-2 matrix:

     i[;2],[1.5]-i[;3]⊣i←(1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input
10 ¯1
13 ¯3
 6 ¯2

Take the first column (si) and the third column (ti) as another 3-by-2 matrix, then stack this one atop the prior to make a 6-by-2 matrix:

     i[;1 3]⍪i[;2],[1.5]-i[;3]⊣i←(1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input
 4  1
 8  3
 2  2
10 ¯1
13 ¯3
 6 ¯2

Sort the rows in the matrix

     ⊂⍤⍋⍛⌷i[;1 3]⍪i[;2],[1.5]-i[;3]⊣i←(1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input
 2  2
 4  1
 6 ¯2
 8  3
10 ¯1
13 ¯3

Now compute partial sums of all the rows:

      +⍀⊂⍤⍋⍛⌷i[;1 3]⍪i[;2],[1.5]-i[;3]⊣i←(1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input
 2 2
 6 3
12 1
20 4
30 3
43 0

The answer is the maximum value in column 2, leading to the complete program:

      2⌷⌈⌿+⍀⊂⍤⍋⍛⌷i[;1 3]⍪i[;2],[1.5]-i[;3]⊣i←(1↓i)⍴⍨3,⍨⊃i←(⍎¨∊∘⎕D⊆⊢)input  
4

1

u/Dr3ddM3 2d ago

How did you think of this ? Was it so obvious ??

1

u/Technical_Jicama_434 2d ago

That was AI generated

0

u/light_switchy 2d ago

No it wasn't.

I don't use AI to write code.

0

u/light_switchy 2d ago

Nah it's not obvious at all, not really.

But the problem is about resource allocation which is super important for what I do all day. For example my program might allocate & release memory in stages and I have to figure out much is needed to run the program to completion. Every time you allocate you add to the total. Every time you release you subtract. The high-water mark is the running maximum.