Problem solution · Python

CCC 2015 J4 - Wait Time

CCC 2015 J4 - Wait Time: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
75 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2015 J4 - Wait Time, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 75 lines of Python from the credited upstream file ccc15j4.py.
  • The implementation visibly relies on sequence storage.
  • No explicit loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2015 J4 - Wait Time · PythonPython
Use this to learn the idea, then write your own version.
# CCC 2015 Junior 4: Wait Time## Consider this a 2D array problem, or an array of records## For this I use and array of elements called "database"# Each element is itself an array of 3 numbers: #     the friend #, time of message, and the total wait time for this friend## I need it organized like this so it can be sorted by friend number at the end.## Also the time of message will be set to -1 when answered, # so at the end I can detect if there are unanswered messages.## eg [12,6,2] means friend 12 has made a call at time 6 (its not replied to so far)#             and he/she has a wait time of 2 from previous message(s) file = open("j4.3.in", "r")m = int(file.readline()) database = []time = -1for i in range(m):    line = file.readline().split()    letter = line[0]    friend = int(line[1])     if letter == "W":        time = time + friend - 1   # in this case friend is really a time                                   # the -1 is there because +1 will be added next time    else:        time = time + 1     if letter != "W":        # search database for this friend        found = False        k = 0        while not found and k < len(database):            if friend == database[k][0]:                found = True            else:                k = k + 1         if letter == "R":                   #if found, put inthe time of the message, otherwise add a new element to the database            if found:                database[k][1] = time            else:                database.append ([friend, time, 0])        elif letter == "S":            # it will be found, so no check is done            database[k][2] = database[k][2] + (time - database[k][1])            database[k][1] = -1 # Before printing the results the database must be sorted by friend number# A simple bubble sort is usedfor i in range(len(database)-1):    for j in range(i+1, len(database)):        if database[i][0] > database[j][0]:            temp = database[i]            database[i] = database[j]            database[j] = temp # Print resultsfor i in range(len(database)):    if database[i][1] == -1:        print database[i][0], database[i][2]    else:        print database[i][0], -1                         

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗