0

I got one problem with sorting and output. I have no idea how should I do that.

The main idea of the program:

  1. Input total amount of passengers.
  2. Input total amount of cells.
  3. Input passengers's data n-times (based on the first input) - ex.: Qwe 11:25 12:34.
  4. Output sorted data. Output should be like this: name of the passenger, cell number.
n = int(input())
k = int(input())
data = []

for x in range(1,n+1):
    data.append(input().replace(":"," ").split(" "))


for elem in data:
    elem.append(int(elem[1])*60+int(elem[2]))
    elem.append(int(elem[3])*60+int(elem[4]))

    while len(elem)>3:
        elem.pop(1)

    if elem[1]>elem[2]:
        raise SystemExit("time of issuing can not be lower than time putting")

Example input of the program:

4    
2     
Qwe 12:45 16:30    
Wer 13:55 17:50    
Ert 6:25 12:55    
Rew 22:55 23:30

Output:

Ert 1    
Qwe 2
Wer 2    
Rew 1

"Wer" also can be skipped, because of cells limit (it's a beginning exercise, so it doesn't really mean you need such a good code that will cover everything). I can try to explain how cells work, but I think it's pretty clear from the example output - it's based on time of issuing and time putting comparation.

I'm trying to learn Python, and will be grateful if anyone will answer what should I probably do to complete my script, or rather use another idea for it.

Upd: I got something like this:

data = sorted(data, key=lambda elem: elem[1])
for elem in data: #Just visual check
    print(elem)

for x in data:
    if j<=k:
        if data[i][2]>data[i+1][1]:
            print(data[i][0], j+1)
            j+=1
            i+=1
        else:
            j=0
            print(data[i][0], j+1)
    else:
        j=0

But it's still not quite what I want. Any ideas?

1 Answer 1

1

You can learn the basics of Python in about five minutes from the Sorting HOWTO.

In your script, it appears you are properly parsing the times but are losing the passenger info at elem[0]. You will need that for the final sort. Also, you've appended all the data into one list instead of keeping the row separate (which is necessary for sorting).

Spending a few minutes with the Sorting HowTo will help you see how to organize your data to make it sortable.

Sign up to request clarification or add additional context in comments.

1 Comment

Got something like data = sorted(data, key=lambda elem: elem[1]) but any idea how can I track the cell number properly? I mean, need to compare elem[2] with next elem[1] and repeat it? I'm pretty unsure what should I do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.