Python - Programming Fundamentals Mid Exam Retake 12 August 2020 Problem 2. The Lift
Здравейте, колеги нещо забих!Моля ,за помощ!
Здравейте, колеги нещо забих!Моля ,за помощ!
Проблемът е в логиката, определяща какво да се отпечата като резултат и по специално случаите, когато броят на туристите е равен на броя свободни места. Например при вход:
4
0
Програмата ще изведе:
There isn't enough space! 0 people in a queue!
4
Вместо:
4
Фикс:
if tourists > 0:
print(f"There isn't enough space! {tourists} people in a queue!")
elif counter < len(lift):
print(f"The lift has empty spots!")
print(" ".join(lift_list))
Ето и алтернативно решение:
people = int(input())
lift = [int(cart) for cart in input().split(" ")]
for i in range(len(lift)):
can_load = min(4 - lift[i], people)
lift[i] += can_load
people -= can_load
if people > 0:
print(f"There isn't enough space! {people} people in a queue!")
elif len([cart for cart in lift if cart < 4]) > 0:
print("The lift has empty spots!")
print(" ".join([str(cart) for cart in lift]))
И едно от мен:
queue = int(input())
lift = [int(x) for x in input().split()]
output = ''
while queue != 0:
if all(x == 4 for x in lift):
print(f"There isn't enough space! {queue} people in a queue!\n" +
" ".join(str(w) for w in lift))
break
for i in range(len(lift)):
diff = 4 - lift[i]
if queue >= diff:
lift[i] += diff
queue -= diff
elif queue < diff:
lift[i] += queue
queue -= queue
else:
if any(x < 4 for x in lift):
print('The lift has empty spots!\n' +
' '.join(str(w) for w in lift))
elif all(x == 4 for x in lift):
print(' '.join(str(w) for w in lift))