Home > Software design >  Your code send nothing back test error python?
Your code send nothing back test error python?

Time:12-15

I actually pass a test and I have here to calculate the lowest total carbon footprint of all possible routes. I know there is multiple way to do this but I tried something like this:

compositionAller = []
compositionRetour = []
for element in range(int(lines[0])):
    date = lines[element   1].split(" ")[0]
    heure, minutes, secondes = map(int, date.split(":"))
    ville = lines[element   1].split(" ")[1]
    emmission = int(lines[element   1].split(" ")[2])
    if ville == "Paris-Lyon":
        compositionAller.append([heure, minutes, secondes, ville, emmission])
    else:
        compositionRetour.append([heure, minutes, secondes, ville, emmission])

MeilleurTrajet = None
for aller in compositionAller:
    for retour in compositionRetour:
        if aller[0] == retour[0] and aller[1] < retour[1]or aller[0] < retour[0] or aller[0] == retour[0] and aller[1] == retour[1] and aller[2] <= retour[2] :
            if not MeilleurTrajet or MeilleurTrajet > aller[-1]   retour[-1]:
                MeilleurTrajet = aller[-1]   retour[-1]

print(MeilleurTrajet)

But arrived at test case 7 they always said there is an error your code send nothing back and I don't have access to the list to try it by my side. So if someone can help me to know where is the error. Here is an example of the table that we can get:

[
"500",
"22:24:09 Paris-Lyon 487",
"09:39:29 Paris-Lyon 2",
"10:20:32 Lyon-Paris 3",
]

I tried to change the condition to check hours minutes and secondes but nothing works.

CodePudding user response:

You wrote

for aller in compositionAller:
    for retour in compositionRetour:
        if aller[0] == retour[0] and aller[1] < retour[1] or aller[0] < retour[0] or aller[0] == retour[0] and aller[1] == retour[1] and aller[2] <= retour[2] :
            if not MeilleurTrajet or MeilleurTrajet > aller[-1]   retour[-1]:

This seems very inconvenient. Humans will read / debug this code; humans are good at story telling. Much better to use words ("seconde") than numbers ([0]).

Also, I would prefer an identifier of meilleurEmmission for consistency with code above, to clarify it has CO2 units, but that's a minor point. (And PEP-8 asks for meilleur_emmission, whatever.)

The inconvenience starts here:

        compositionAller.append([heure, minutes, secondes, ville, emmission])

Tack on an import.

import datetime as dt

We know we're going to have to compare H,M,S against H,M,S. Why make it painful, why do it piecemeal? Here's a pair of better ways to represent same thing:

  1. Keep H,M,S, but as a tuple: .append([(heure, minutes, secondes), ville, emmission])
  2. Abandon H,M, preferring SI units: .append([dt.timeinterval(hours=heure, minutes=minutes, seconds=secondes), ville, emmission])

If you prefer integers, then dt.timeinterval(...).total_seconds() would give you e.g. 300 seconds for a five-minute journey.

Ok, suddenly that giant if expression has become more manageable, it is very easy to ask which journey has shorter duration.


You say your code doesn't generalize over all test cases.

Here is one more expression in the original code that worries me:

    if ville == "Paris-Lyon":

That seems very hard-coded, very brittle.

I don't know what the original problem statement is, and what valid city names might be.

But I worry that you need

    start, end = ville.split("-")

and that you may need some fancier logic, perhaps with a dict lookup, to keep track of outbound and inbound routes for multiple cities which aren't necessarily Paris or Lyon. Also, if we must handle a Paris -> Auxerre followed by Auxerre -> Lyon compound route, then a much more complex approach to graph traversal will be needed.

Bonne chance!

  • Related