def load_table(file_path, table_number):
with open(file_path, 'r') as file:
content = file.read()
code
# Rozdělení obsahu na tabulky
tables = content.split("Table")
if table_number < len(tables):
# Získání dat z požadované tabulky
table_data = tables[table_number].strip()
# Odstranění nežádoucích znaků a převod na seznam čísel
numbers = [int(num.strip()) for num in table_data.replace(',', ' ').replace(';', ' ').split() if num.strip().isdigit()]
return numbers
else:
return []
def main():
# Načtení čísel z obou tabulek
table1 = load_table('data.txt', 1)
table2 = load_table('data.txt', 2)
code
# Odstranění shodných čísel
unique_table1 = [num for num in table1 if num not in table2]
unique_table2 = [num for num in table2 if num not in table1]
# Počty pro každou tabulku
original_count_table1 = len(table1)
removed_count_table1 = original_count_table1 - len(unique_table1)
remaining_count_table1 = len(unique_table1)
original_count_table2 = len(table2)
removed_count_table2 = original_count_table2 - len(unique_table2)
remaining_count_table2 = len(unique_table2)
# Zapsání výsledků do souboru
with open('results.txt', 'w') as result_file:
result_file.write("Table 1:\n")
result_file.write(", ".join(map(str, unique_table1)) + "\n")
result_file.write(f"In the table there were originally {original_count_table1} year(s), {removed_count_table1} year(s) were removed, {remaining_count_table1} year(s) remained.\n\n")
result_file.write("Table 2:\n")
result_file.write(", ".join(map(str, unique_table2)) + "\n")
result_file.write(f"In the table there were originally {original_count_table2} year(s), {removed_count_table2} year(s) were removed, {remaining_count_table2} year(s) remained.\n")
Assistant
if name == "main":
main()
tohle je vystup, který je spatne
Table 1:
1, 1634, 1729, 1824, 2071, 2166, 2261, 2356, 2603, 2698
In the table there was originally 272 year(s), 262 year(s) was removed, 10 year(s) remained.
Table 2:
2
In the table there was originally 263 year(s), 262 year(s) was removed, 1 year(s) remained.
takhle ma vypadat spravny vystup
Table 1:
1634, 1729, 1824, 2071, 2166, 2261, 2356, 2603, 2698
In the table there was originally 271 year(s), 262 year(s) was removed, 9 year(s) remained.
Table 2:
In the table there was originally 262 year(s), 262 year(s) was removed, 0 year(s) remained
Zdá se, že problém ve vašem skriptu spočívá v tom, že při načítání tabulky se také načítá číslo tabulky (např. „1“ pro Table 1), což vede k nesprávnému výsledku. Abychom to opravili, je třeba při načítání dat z tabulky přeskočit první číslo, které reprezentuje číslo tabulky.
Níže je upravený skript, který tuto změnu implementuje:
python
def load_table(file_path, table_number):
with open(file_path, 'r') as file:
content = file.read()
# Rozdělení obsahu na tabulky
tables = content.split("Table")
if table_number < len(tables):
# Získání dat z požadované tabulky
table_data = tables[table_number].strip()
# Rozdělení dat na jednotlivé tokeny
tokens = table_data.replace(',', ' ').replace(';', ' ').split()
# Odstranění prvního tokenu, který je číslem tabulky
if tokens and tokens[0].isdigit():
tokens = tokens[1:]
# Převod zbylých tokenů na seznam čísel
numbers = [int(num) for num in tokens if num.isdigit()]
return numbers
else:
return []
def main():
# Načtení čísel z obou tabulek
table1 = load_table('data.txt', 1)
table2 = load_table('data.txt', 2)
# Odstranění shodných čísel
unique_table1 = [num for num in table1 if num not in table2]
unique_table2 = [num for num in table2 if num not in table1]
# Počty pro každou tabulku
original_count_table1 = len(table1)
removed_count_table1 = original_count_table1 - len(unique_table1)
remaining_count_table1 = len(unique_table1)
original_count_table2 = len(table2)
removed_count_table2 = original_count_table2 - len(unique_table2)
remaining_count_table2 = len(unique_table2)
# Zapsání výsledků do souboru
with open('results.txt', 'w') as result_file:
result_file.write("Table 1:\n")
if unique_table1:
result_file.write(", ".join(map(str, unique_table1)) + "\n")
else:
result_file.write("\n")
result_file.write(f"In the table there were originally {original_count_table1} year(s), {removed_count_table1} year(s) were removed, {remaining_count_table1} year(s) remained.\n\n")
result_file.write("Table 2:\n")
if unique_table2:
result_file.write(", ".join(map(str, unique_table2)) + "\n")
else:
result_file.write("\n")
result_file.write(f"In the table there were originally {original_count_table2} year(s), {removed_count_table2} year(s) were removed, {remaining_count_table2} year(s) remained.\n")
if __name__ == "__main__":
main()
Změny, které byly provedeny:
Odstranění čísla tabulky při načítání:
V load_table funkci jsme přidali krok, který odstraní první token (číslo tabulky) ze seznamu tokenů před převodem na čísla.
To zajišťuje, že číslo tabulky (např. „1“ pro Table 1) nebude zahrnuto v výsledném seznamu čísel.
Zpracování prázdných tabulek:
Pokud po odstranění čísla tabulky nejsou žádná čísla v tabulce, skript nyní správně vypíše prázdnou řádku pro danou tabulku.
Formátování výstupu:
Přidal jsem podmínky, které kontrolují, zda jsou v unique_table1 nebo unique_table2 nějaká čísla před tím, než je zapíšou do souboru. Pokud nejsou, zapíše se prázdná řádka místo seznamu čísel.
Očekávaný výstup:
Po provedení těchto změn by měl skript generovat správný výstup:
code
Table 1:
1634, 1729, 1824, 2071, 2166, 2261, 2356, 2603, 2698
In the table there was originally 271 year(s), 262 year(s) were removed, 9 year(s) remained.
Table 2:
In the table there was originally 262 year(s), 262 year(s) were removed, 0 year(s) remained.
Další doporučení:
Validace vstupních dat:
Ujistěte se, že soubor data.txt je správně formátován a že každá tabulka začíná slovy „Table“ následovanými číslem tabulky a odpovídajícími daty.
Lepší správa chyb:
Můžete přidat více kontrol a chybových hlášení, pokud například soubor neexistuje, nebo pokud formát tabulky neodpovídá očekávanému.
Doufám, že tyto úpravy vyřeší váš problém a skript bude nyní fungovat podle očekávání!