Demo entry 3517639

compute.py

   

Submitted by wilhelm olejnik on Jan 08, 2016 at 05:48
Language: Python 3. Code size: 4.0 kB.

from enum import Enum
from operator import attrgetter
import sys

class lineType(Enum):
    """typ linii: pionowa lub pozioma"""
    VER = 1
    HOR = 2
    
class Line:
    """linia wzdłuż której można złamać czekoladę"""
    def __init__(self, direction, cost):
        self.direction = direction #kierunek
        self.cost = cost           #koszt

class Chocolate:
    """tabliczka czekolady"""
    def __init__(self, textline):
        self.textInfo = textline.split()
        self.width = int(self.textInfo[0])   #wysokość czekolady
        self.height = int(self.textInfo[1])  #szerokość czekolady
        self.verLineNum = self.width - 1     #ile linii pionowych
        self.horLineNum = self.height - 1    #ile linii poziomych
        self.verCosts = ""                   #pionowe koszty (string)
        self.horCosts = ""                   #poziome koszty (string)
        self.lines = []                      #linie jako obiekty Line
        self.total = 0                       #koszt całkowity11

        #wszystkie linie zostaja zapisane na liscie
        for i in range(2, 2 + self.verLineNum, 1):
            self.lines.append(Line(lineType.VER,int(self.textInfo[i])))
        for i in range(2 + self.verLineNum, len(self.textInfo)):
            self.lines.append(Line(lineType.HOR,int(self.textInfo[i])))
            
        #tworzymy stringi z wypisanymi kosztami przełamań pion/poz
        for line in self.lines:
            if line.direction == lineType.VER:
                self.verCosts += (str(line.cost) + "  ")
            if line.direction == lineType.HOR:
                self.horCosts += (str(line.cost) + "  ")
                
        #obliczamy koszt całkowity wszystkich przełamań
        self.total = self.calculateCost()

    def calculateCost(self):
        total = 0      #koszt całkowity
        verBreaks = 0  #wykonane przełamania pionowe
        horBreaks = 0  #wykonane przełamania poziome
        lines = self.lines #linie w postaci obiektów Line

        while len(self.lines) > 0:
            maxcost = max(self.lines, key=attrgetter('cost')).cost
            for line in lines:
                if line.cost == maxcost:
                    if line.direction == lineType.VER:
                        total += line.cost*(horBreaks+1)
                        verBreaks += 1
                    if line.direction == lineType.HOR:
                        total += line.cost*(verBreaks+1)
                        horBreaks +=1
                    lines.remove(line)
                    break
        return total

def makeWebsite(chocolateList, outFilename):
    outFile = open(outFilename, "w")
    outFile.write("<html>\n<head>\n")
    outFile.write("""<link rel="stylesheet" href="style.css">\n""")
    outFile.write("</head>\n<body>\n")
    outFile.write("""<table align="center">\n<tr>\n""")
    outFile.write("<th>Rozmiar</th>\n")
    outFile.write("<th>Pionowe koszty przełamań</th>\n")
    outFile.write("<th>Poziome koszty przełamań</th>\n")
    outFile.write("<th>Minimalny koszt połamania całej czekolady</th>\n")
    outFile.write("</tr>\n")
    for c in chocolateList:
        outFile.write("<tr>\n")
        outFile.write("<td>" + str(c.width) + " x " + str(c.height) + "</td>\n")
        outFile.write("<td>" + c.verCosts + "</td>\n")
        outFile.write("<td>" + c.horCosts + "</td>\n")
        outFile.write("<td>" + str(c.total) + "</td>\n")
        outFile.write("</tr>\n")
    outFile.write("</table>\n</body>\n</html>")
        
### MAIN ###
if (len(sys.argv) > 2):
    inFilename = sys.argv[-2]
    outFilename = sys.argv[-1]
else:
    inFilename0 = "input.txt"
    outFilename = "output.html"
                  
inFile = open(inFilename)

chocolateList = []
lines = inFile.read().splitlines()
for line in lines:
    chocolateList.append(Chocolate(line))
inFile.close()

makeWebsite(chocolateList, outFilename)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).