Home‎ > ‎ProgComp‎ > ‎ProgComp 2013 Example Answers‎ > ‎

Task 2. Safe-T-Cam








Algorithm:







Example Code
#-------------------------------------------------------------------------------
# Name:        Task 2. Safe-T-Cam
# Purpose:
#
# Author:      Joseph.Lai
#
# Created:     25/05/2014
# Copyright:   (c) Joseph.Lai 2014
# Licence:     <your licence>
#-------------------------------------------------------------------------------

from datetime import datetime

# Distance to Checkpoint
distance = {
    2:133.0,
    3:57.5
    }

# List here instead of File Read for Testing purposes
list1 = [

]

def doGetFile():
    # Here is where you enter code to read from a file. This has been done before in other examples
    # Choose the list to process - list1 is the test, list2 is actual data
    #return list1

    # Open a file

    fileName = "Input Task 2 Safe-T-Cam2.txt"

    fo = open(fileName, "r")
    file=fo.read();
    fo.close();
    inp = file.splitlines(); # creates a list from the file input separated by \n (default)
    return inp

def doReorgLog(sentence):
    # Re organise Speed Log list by Rego order, Checkpoint and Time
    i = -1
    newSentence = ""
    words = sentence.split(" ")
    #print(words)
    newSentence = [words[2],words[1],words[0]]
    return newSentence


# ==================================================================================
first=True
count=0
maxcount = 0
maxLen=100
maxSpeed = 110         #maximum num of letters in sentence
list = doGetFile()
maxRecords = 251

# Process the List
# ==================================================================================
newList = []

# Get the Speed Log into a List to SORT
for sentence in list:
    if len(sentence)>maxLen:
        print("{} is too long by {} chars".format(sentence, len(sentence)-maxLen))
    if count < maxRecords:
        if first:
            maxcount = sentence
            first = False
        else:
            newSentence = doReorgLog(sentence)
            #print("{} : {}".format(sentence,newSentence))
            newList.append(newSentence)
            count+=1

    else:
        print("Maximum Records Reached")
        break
# Sort by Rego, Checkpoint, Time
newList.sort()

# =============================================================================
# Work out Speed between each checkpoint for each Vehicle
rego = ""
outList = []
for reading in newList:
    if reading[0] == rego:
        checkpoint = int(reading[1])
        timeStamp2 = datetime.strptime(reading[2], "%H:%M:%S")
        diff = timeStamp2 - timeStamp1
        speed = round(distance[checkpoint] / (diff.seconds/(3600)),1)
        #print("{} : {} - {} {} : diff {} {} : dist {} : speed {}".format(rego,checkpoint,timeStamp1.strftime('%H:%M:%S'),timeStamp2.strftime('%H:%M:%S'), diff, diff.seconds/(3600),distance[checkpoint],speed))
        timeStamp1 = timeStamp2
        if speed > maxSpeed: # only store excessive speed list - reorganised by time, checkpoint, rego and speed
            outList.append([timeStamp2.strftime('%H:%M:%S'), checkpoint, rego, speed])
    else:
        rego = reading[0]
        timeStamp1 = datetime.strptime(reading[2], "%H:%M:%S")

# ==============================================================================
# Sort Excessive Speed List by Time, Checkpoint and Rego and Speed
outList.sort()
for out in (outList):
    print("{} {} {}\t{}".format(out[0],out[1],out[2],out[3]))

print("Records Read: {} , Max: {}".format(count,maxcount))


Input File:

250
13:07:15 1 RA58EC
13:07:25 1 TS67NH
13:07:48 1 WZ11ZN
13:07:53 1 DM13OR
13:08:41 1 UM17HA
13:08:57 1 DU07WI
13:09:57 1 NF00OD
13:10:07 1 ZOOM98
13:10:32 1 AD78YQ
13:10:34 1 LM91XR
13:12:20 1 JA61RD
13:13:00 1 STQLN
13:13:06 1 VX53MY
13:13:15 1 JX64XY
13:14:14 1 ZI31OE
13:14:28 1 ZOOM99
13:15:49 1 RP75XK
13:16:23 1 UA37TY
13:18:06 1 OS61YP
13:18:17 1 LEMANS
13:18:42 1 JX33RT
13:18:54 1 VW48PU
13:19:19 1 BT73EC
13:19:24 1 YK65QD
13:19:40 1 LINFOX
13:19:58 1 PB4UGO
13:20:00 1 TB31YK
13:20:04 1 US00GP
13:21:07 1 OJ74JG
13:21:14 1 RM08PC
13:21:24 1 KN90YF
13:21:46 1 SY14LJ
13:22:04 1 CN15EE
13:22:24 1 MYBMW
13:23:01 1 QW20QT
13:23:15 1 50BER
13:24:06 1 FG43KY
13:24:23 1 THSTIG
13:24:40 1 SC77NC
13:24:52 1 PF85NJ
13:26:23 1 OP22QI
13:27:19 1 OZ26WC
13:29:03 1 GI47PC
13:29:12 1 NR80VS
13:29:14 1 UL45LZ
13:29:21 1 VZ89KR
13:29:25 1 SE21VN
13:29:53 1 EF04DY
13:30:21 1 HB06VI
13:31:08 1 H8COPS
13:31:10 1 NP84HG
13:32:27 1 XD22YM
13:32:29 1 QO43DJ
13:32:52 1 SK93IQ
13:32:59 1 XY13XS
13:33:20 1 PY43JF
13:34:46 1 TO56HT
13:35:42 1 ZX38BC
13:36:00 1 LB34KH
13:36:11 1 JQ59CG
13:36:16 1 BH59CW
13:36:20 1 VC38LK
13:36:26 1 WW54SN
13:37:00 1 RC07GN
13:37:15 1 VV60FW
13:38:53 1 IR99TT
13:39:08 1 HP10MT
13:39:52 1 RH80OK
13:40:12 1 JT50UU
13:41:20 1 NQ34LX
13:41:41 1 SS80CY
13:41:52 1 4MYHSC
13:42:05 1 OM46VU
13:42:27 1 VF31XW
13:42:47 1 PC2013
13:43:19 1 BJ13VY
13:43:45 1 HN43VZ
13:44:00 1 EI25KB
13:44:07 1 ZJ46HV
13:44:36 1 JS98BQ
13:45:14 1 EJ04YX
13:45:45 1 XP92CE
13:47:02 1 SXYCAR
13:48:09 1 KK09LY
14:08:27 2 GB72NM
14:16:11 2 VW46BL
14:18:09 2 KG06AF
14:18:09 2 TS67NH
14:19:55 2 HT70JW
14:21:52 2 STQLN
14:22:11 2 UM17HA
14:24:21 2 JX34SZ
14:25:14 2 ZOOM99
14:25:41 2 WZ11ZN
14:27:50 2 LEMANS
14:27:58 2 FS19UM
14:28:06 2 JX64XY
14:29:09 2 TK72GR
14:30:21 2 DM13OR
14:31:20 2 RM08PC
14:33:06 2 DU07WI
14:33:31 2 NF00OD
14:33:48 2 JX33RT
14:34:34 2 THSTIG
14:34:39 2 ZOOM98
14:34:41 2 TB31YK
14:35:01 2 AD78YQ
14:35:06 2 DK82BX
14:35:14 2 LINFOX
14:35:24 2 UA37TY
14:35:31 2 VX53MY
14:36:37 2 YK65QD
14:36:38 2 CN15EE
14:37:07 2 FG43KY
14:38:17 2 US00GP
14:38:39 3 GB72NM
14:38:42 2 UU91CK
14:39:26 2 QW20QT
14:39:28 2 RA58EC
14:39:31 2 JA61RD
14:39:49 2 BW74CO
14:40:28 2 MX62VO
14:40:44 2 RTFM
14:41:03 2 OS61YP
14:43:11 2 VQ11GF
14:44:09 2 50BER
14:44:54 2 OZ26WC
14:45:05 2 UL45LZ
14:45:36 2 VW48PU
14:46:00 2 EF04DY
14:46:19 2 NP84HG
14:46:20 2 OJ74JG
14:46:46 2 GI47PC
14:46:47 2 H8COPS
14:47:04 2 BT73EC
14:47:52 2 KN90YF
14:47:52 2 XD22YM
14:48:00 3 VW46BL
14:48:38 2 ZI31OE
14:49:03 2 QO43DJ
14:50:23 2 OP22QI
14:50:59 2 BH59CW
14:51:09 3 KG06AF
14:51:21 2 PB4UGO
14:51:52 3 TS67NH
14:52:16 2 BG54GL
14:53:21 2 SY14LJ
14:53:34 3 HT70JW
14:53:42 3 UM17HA
14:53:47 2 NR80VS
14:53:54 3 ZOOM99
14:54:22 2 PC2013
14:55:07 2 VZ89KR
14:55:14 2 MYBMW
14:55:19 2 SE21VN
14:55:58 2 4MYHSC
14:56:00 3 STQLN
14:57:36 2 VV60FW
14:57:47 2 SXYCAR
14:57:58 2 SC77NC
14:58:16 3 JX64XY
14:58:17 3 JX34SZ
14:58:18 2 OM46VU
14:58:26 3 LEMANS
14:58:28 2 ZX38BC
14:59:55 2 WW54SN
14:59:56 3 WZ11ZN
15:00:00 3 IMLATE
15:01:14 2 SK93IQ
15:02:11 3 FS19UM
15:02:12 2 RP75XK
15:02:17 2 BJ13VY
15:02:21 2 TO56HT
15:04:41 2 VC38LK
15:04:46 2 RC07GN
15:04:47 2 EJ04YX
15:04:48 3 RM08PC
15:05:08 2 KK09LY
15:05:48 2 JT50UU
15:05:50 3 THSTIG
15:05:54 3 LINFOX
15:06:11 2 VF31XW
15:06:28 2 PY43JF
15:06:31 2 PF85NJ
15:06:51 3 TB31YK
15:07:02 2 JQ59CG
15:07:29 3 DU07WI
15:07:32 3 TK72GR
15:08:00 3 SC44IP
15:08:14 2 HN43VZ
15:08:15 2 XY13XS
15:08:23 3 NF00OD
15:08:43 2 XP92CE
15:09:24 3 JX33RT
15:09:35 3 DM13OR
15:09:40 2 LB34KH
15:09:57 2 NQ34LX
15:10:20 3 ZOOM98
15:10:41 2 JS98BQ
15:10:43 2 LM91XR
15:10:50 2 SS80CY
15:10:53 3 AD78YQ
15:11:08 3 YK65QD
15:11:23 3 UA37TY
15:12:27 3 FG43KY
15:13:14 3 MX62VO
15:13:18 3 US00GP
15:14:00 3 CN15EE
15:14:25 2 HP10MT
15:15:19 2 RH80OK
15:15:33 3 OZ26WC
15:15:36 2 HB06VI
15:15:50 3 VQ11GF
15:16:25 3 QW20QT
15:16:48 3 DK82BX
15:17:09 3 H8COPS
15:18:14 3 50BER
15:19:22 3 JA61RD
15:19:41 3 UU91CK
15:19:50 3 RTFM
15:20:16 3 OS61YP
15:20:20 3 NP84HG
15:20:41 3 UL45LZ
15:20:57 3 GI47PC
15:21:01 3 EF04DY
15:21:12 3 RA58EC
15:21:31 3 OJ74JG
15:21:39 3 VW48PU
15:21:59 3 XD22YM
15:22:09 3 BW74CO
15:22:28 2 ZJ46HV
15:22:33 3 QO43DJ
15:24:58 3 OP22QI
15:25:25 3 BH59CW
15:26:04 3 BT73EC
15:26:08 2 EI25KB
15:27:10 3 PC2013
15:27:18 3 PB4UGO
15:28:51 3 ZI31OE
15:29:49 3 SXYCAR
15:30:18 3 NR80VS
15:30:49 3 VZ89KR
15:31:57 3 SE21VN
15:34:09 3 BG54GL
15:34:34 3 OM46VU
15:34:42 3 BJ13VY
15:34:46 3 VV60FW
15:35:14 3 MYBMW
15:35:22 3 SY14LJ
15:35:29 3 KK09LY




Comments