Question: Only 8 test cases passed with the code, how to get all test cases passed? def cutThemAll ( lengths , minLength ) : # Write
Only test cases passed with the code, how to get all test cases passed?
def cutThemAlllengths minLength:
# Write your code here
# Sort lengths in descending order
lengths.sortreverseTrue
# Check if the total length is less than minLength
if sumlengths minLength:
return "Impossible"
# Check if the longest rod is long enough for the first cut
if lengths minLength:
return "Impossible"
# Check if we can make cuts to achieve minLength
while lengths:
currentlength lengths.pop
# If the current length is exactly minLength, we can make the cuts
if currentlength minLength:
return "Possible"
# If the current length is longer than minLength, make the cuts
elif currentlength minLength:
lengths.appendcurrentlength minLength
# If the current length is shorter than minLength,
# try to combine with the next rod to make cuts
else:
if lenlengths:
return "Impossible"
nextlength lengths.pop
combinedlength currentlength nextlength
if combinedlength minLength:
lengths.appendcombinedlength minLength
else:
return "Impossible"
return "Possible"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
