Python ticket control script

Asked

Viewed 319 times

3

What would be a more appropriate solution to this problem?

The purpose of this Python script is to process possible ticket sales, analyzing each of them, indicating whether it is possible to SELL or NOT to SELL.

The requirements for the script are:

4 cities (A, B, C and D ) with ticket sales points.

The bus runs from A to D, passing by B and C and has 45 seats.

The script contains input, processing and output.

Input data (100 attempts to purchase tickets):

X>Y, X>Y, X>Y,X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, X>Y, ...

Where X and Y indicate, respectively, Origin City and Destination City.

tentativas = ['A>C','A>C','A>C','B>D','B>D','B>D','B>D','B>D','A>B',
              'A>B','A>B','A>B','B>C','B>C','B>C','B>C','B>C','A>D',
              'A>D','A>D','A>D','A>D','A>D','A>D','A>D','A>D','C>D',
              'C>D','C>D','C>D','C>D','C>D','C>D','C>D','B>D','B>D',
              'B>D','B>D','B>C','B>C','B>C','A>C','A>B','A>B','B>C',
              'B>C','B>C','A>C','C>D','C>D','C>D','C>D','C>D','C>D',
              'C>D','B>D','B>D','B>D','B>D','A>B','A>B','A>B','A>B',
              'A>B','A>B','A>B','A>B','A>B','A>B','A>B','A>C','A>C',
              'A>C','A>C','A>C','A>C','A>C','A>C','A>C','A>C','A>D',
              'C>D','C>D','C>D','C>D','B>D','B>D','B>D','B>D','B>D',
              'A>B','A>B','A>C','A>C','A>C','C>D','C>D','A>B','A>B',
              'A>D']


ab = []
bc = []
cd = []

poltronas = 45

i = 0

for tentativa in tentativas:
    i = i + 1

    if (tentativa == 'A>B' and len(ab) < poltronas): 
        ab += [i]

    if (tentativa == 'A>C' and len(ab) < poltronas and len(bc) < poltronas):
        ab += [i]
        bc += [i]

    if (tentativa == 'A>D' and len(ab) < poltronas and len(bc) < poltronas and len(cd) < poltronas):
        ab += [i] 
        bc += [i]
        cd += [i]

    if (tentativa == 'B>C' and len(bc) < poltronas):
        bc += [i]

    if (tentativa == 'B>D' and len(bc) < poltronas and len(cd) < poltronas):
        bc += [i]
        cd += [i]

    if (tentativa == 'C>D' and len(cd) < poltronas):
        cd += [i]

    if (i in ab or i in bc or i in cd):
        print (i , ' - ', tentativa , ' - VENDER')
    else:
        print (i , ' - ', tentativa , ' - NAO VENDER')
  • The question is not very clear. Is the idea to improve the execution time? Make the code more readable and "elegant"?

  • But legible and elegant.

  • One possibility would be to create a class onibus that would make the route and passage control for each bus of the line and for each time.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.