def loi_muc_tieu(thao_tac): cache = False for ten in thao_tac: if ten == "bat_cache": cache = True if ten == "don_cache": cache = False if ten == "nhap_lai" and cache: return True return False def thu_nho(du_lieu): assert loi_muc_tieu(du_lieu) while True: for i in range(len(du_lieu)): ung_vien = du_lieu[:i] + du_lieu[i + 1:] if loi_muc_tieu(ung_vien): du_lieu = ung_vien break else: return du_lieu ket_qua = thu_nho(["mo", "bat_cache", "cuon", "nhap_lai", "dong"]) assert ket_qua == ["bat_cache", "nhap_lai"] assert all(not loi_muc_tieu(ket_qua[:i] + ket_qua[i+1:]) for i in range(len(ket_qua))) print(ket_qua)