mercredi 4 novembre 2020

is it possible not to use if-statement too much in python? one python tutorial

I did this python tutorial lately but I think I used too much if statement... is there any way to improve my code? I'm teaching myself but feel like this is wrong

from datetime import time

logs = [
    "woni request 09:12:29",
    "brown request 09:23:11",
    "brown leave 09:23:44",
    "jason request 09:33:51",
    "jun request 09:33:56",
    "cu request 09:34:02"        
]


def solution(logs):
    line =[]
    process = []
    success = []

    for i in logs:        
        line = i.split(' ')
        if line[0] in success:
            continue

        time1 = line[2].split(':')
        h = int(time1[0])
        m = int(time1[1])
        s = int(time1[2])
        if time(h, m, s) > time(9, 0, 0):

            if process != []:

                if line[1] == 'leave':
                    process = []

                if line[1] == 'request':
                    
                    time2 = process[2].split(':')
                    h2 = int(time2[0])
                    m2 = int(time2[1]) + 1
                    s2 = int(time2[2])

                    if time(h2, m2, s2) <= time(h, m, s):
                        success.append(str(process[0]))
                        process = line
            
            if line[1] == 'request':
                process = line
            
    if logs[-1] == process[0]:

        time3 = process[2].split(':')
        h3 = int(time3[0])
        m3 = int(time3[1])
        s3 = int(time3[2])

        if time(h3, m3, s3) <= time(9, 59, 0):
            success.append(str(process[0]))

    else:
        success.append(str(process[0]))


    return success

print(solution(logs))

Here is the question!

we decided to create ticketing system.

When the total number of tickets totalTicket and the log arrangement logs for access/cancellation are given, refer to the simulation description below and apply for ticketing. Complete the solution method to extract and return the ID list of successful users.

Simulation description Request log is left when connection is attempted, and leave log is left when connection is terminated within 1 minute after connection. (Purchase failure after request at 09:00:00 and leave at 09:00:59, purchase success when request at 09:00:00 and leave at 09:01:00) If one user is already connected, other users cannot connect and must try to connect again. To purchase a ticket, you need to keep 1 minute after connecting to the server. Users who successfully purchase tickets can access but cannot purchase again.

case 1
Woni, who made a request at 09:12:29, stayed connected for 1 minute and successfully purchased the ticket.
case 2
Brown, who requested at 09:23:11, could not maintain the connection for 1 minute and terminated the connection at 09:23:44, leaving a leave log and failing to purchase a ticket.
case 3
jason, jun, and cu tried to connect, but jason, who tried to connect the earliest, connected, and jun and cu that tried to connect after that failed to connect.

Purchase success
Request and maintain access for 1 minute when no user is purchasing (there is no leave log)

Purchase failure
If it fails to keep 1 minute after access (leave log exists)
When a request is made while another user is connected

Restrictions
Ticketing time is from 9:00 to 10:00. (The server shuts down at 10:00:0)
Logs of the same time (hour, minute, second) are not generated.
Logs have only request type and leave type.
The total number of tickets is an integer from 0 to 10,000.
id can only be in lowercase alphabets.
Please return the id in ascending order.

input
Total number of tickets
Connection attempt/cancellation log array (id, action[request/leave], hour, minute, second hh:mm:ss)

input sample
totalTicket = 2000
logs = [
    "woni request 09:12:29",
    "brown request 09:23:11",
    "brown leave 09:23:44",
    "jason request 09:33:51",
    "jun request 09:33:56",
    "cu request 09:34:02"
]

Print
ID array for successful ticketing

output sample
[
    "jason",
    "woni"
]

Aucun commentaire:

Enregistrer un commentaire