Saturday, January 27, 2024

Convertion format XLSX into CSV on Fedora 39 (Problem 22 USE 2024)

 Original source has been taken from https://inf-ege.sdamgia.ru/problem?id=47604

Following below is updated and working version

(.env) boris@fedora:~/KEGE2024/D22$ cat djs22.py

import openpyxl
import csv
 

from csv import reader

# input excel file path
inputExcel = '22_23.xlsx'
newWorkbook = openpyxl.load_workbook(inputExcel)
# getting the active workbook sheet(Bydefault-->Sheet1)
worksheet = newWorkbook.active
# Opening a output csv file in write mode
F = open("22_23.csv", 'w')
OutCsv = csv.writer(F,delimiter=";")
for eachrow in worksheet.rows:
    OutCsv.writerow([cell.value for cell in eachrow])
F.close()

"""

 Original code version proposed which requires manual Excel spreadsheet conversion to csv format 

"""

def f(d):
    if d[2] == [0]:
        return d[1]
    else:
        maxx = 0
        for i in d[2]:
            if maxx < f(index[i - 1]):
                maxx = f(index[i - 1])
        return maxx + d[1]
 
with open("22_23.csv") as F:
    s = reader(F, delimiter=';', quotechar='"')
    next(s)
    index = []
    for i in s:
        print(i)
        index.append([int(i[0]), int(i[1]), list(map(int, str(i[2]).split(';')))])
    for i in range(len(index)):
        print(i + 1, f(index[i])) 

(.env) boris@fedora:~/KEGE2024/D22$ python djs22.py
['1', '9', '0']
['2', '9', '1']
['3', '8', '2']
['4', '4', '1;2']
['5', '4', '1;4']
['6', '7', '4']
['7', '7', '0']
['8', '7', '3;5']
['9', '4', '6;7']
['10', '6', '8;9']
['11', '9', '2;3']
['12', '3', '8;9']
['13', '8', '6;7']
['14', '5', '8;10']
['15', '8', '4;11']
1 9
2 18
3 26
4 22
5 26
6 29
7 7
8 33
9 33
10 39
11 35
12 36
13 37
14 44
15 43