Python’da Liste Üzerinde Dön ve Eşleşmeleri Say
Senaryo
Python’da count_open_rooms(seat_counts) fonksiyonunu boş, tek öğeli ve karma listelerde test et.
def count_open_rooms(seat_counts: list[int]) -> int:
open_rooms = 0
for seats in seat_counts:
if seats > 0:
open_rooms += 1
return open_rooms
assert count_open_rooms([]) == 0
assert count_open_rooms([1]) == 1
assert count_open_rooms([0, 2, 0, 1]) == 2Sıra sende
Python’un beklenen tam sayımı döndürdüğünü doğrulamak için her girdiyi karşılaştır.
| Seçenek | Sonuç |
|---|---|
| count_open_rooms([]) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
| count_open_rooms([1]) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
| count_open_rooms([0, 2, 0, 1]) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
Açıklamayı göster
- count_open_rooms([])
0 — döngü 0 kez çalışır ve 0 döner.
- count_open_rooms([1])
1 — son koltuğu olan tek oda açık sayılır.
- count_open_rooms([0, 2, 0, 1])
2 — 2 ve 1 koltuklu odaları sayar, sıfırları atlar.
return satırını for döngüsünün içine değil dışına hizala ve çok öğeli listelerden önce boş [] testini mutlaka ekle.
Önce gözden geçir
Ortak test durumlarını indir
Sıra sende
Python’da count_passing(scores) yaz ve [], [60], [59, 60, 85] için 3 assert ekle.
Sık yapılan hata return satırını for bloğunun içine girintilemek.
Çalışmanı kontrol et
assert count_passing([]) == 0; assert count_passing([60]) == 1; assert count_passing([59, 60, 85]) == 2.
Kısa kontrol
Tarayıcı ilerlemeni kaydedemedi. Değişiklikler bu sayfadan ayrılana kadar geçerli.