4.4 Chat log

A messaging service stores conversations in two parallel arrays:

  • messages contains the text of each message
  • timestamps contains the time each message was sent

The service requires a search function where the user enters a time (in hh:mm format).
The program must then:

  • search through all the messages
  • find every message sent at the given time
  • display the message text together with the time

If no messages were sent at the chosen time, the program must display: “Time not found”.

jane_messages = ["Hi", "Free?", "Library?", "Leaving now"]
jane_times    = ["12:00", "12:02", "12:03", "12:20"]

john_messages = ["Hi Jane", "Yes", "OK", "See you"]
john_times    = ["12:01", "12:02", "12:03", "12:21"]

 

Enter a time (hh:mm): 12:02
12:02, Jane - "Free?"
12:02, John - "Yes"
12:03, Jane - "Library?"
12:03, John - "OK"
12:20, Jane - "Leaving now"
12:21, John - "See you"
SET jane_messages TO ["Hi", "Free?", "Library?", "Leaving now"]
SET jane_times    TO ["12:00", "12:02", "12:03", "12:20"]

SET john_messages TO ["Hi Jane", "Yes", "OK", "See you"]
SET john_times    TO ["12:01", "12:02", "12:03", "12:21"]

INPUT targetTime

SET pos TO 0
SET found TO False

WHILE found = False AND pos < LENGTH(jane_times)
    IF jane_times[pos] = targetTime OR john_times[pos] = targetTime THEN
        SET found TO True
    ELSE
        SET pos TO pos + 1
    END IF
END WHILE

IF found = True THEN
    FOR i FROM pos TO LENGTH(jane_times) - 1
        OUTPUT jane_times[i] + ", Jane - " + '"' + jane_messages[i] + '"'
        OUTPUT john_times[i] + ", John - " + '"' + john_messages[i] + '"'
    END FOR
ELSE
    OUTPUT "Time not found"
END IF

 

jane_messages = ["Hi", "Free?", "Library?", "Leaving now"]
jane_times    = ["12:00", "12:02", "12:03", "12:20"]

john_messages = ["Hi Jane", "Yes", "OK", "See you"]
john_times    = ["12:01", "12:02", "12:03", "12:21"]

target_time = input("Enter a time (hh:mm): ")

pos = 0
found = False
while found == False and pos < len(jane_times):
    if jane_times[pos] == target_time or john_times[pos] == target_time:
        found = True
    else:
        pos = pos + 1

if found == True:
    for i in range(pos, len(jane_times)):
        print(jane_times[i] + ", Jane - " + '"' + jane_messages[i] + '"')
        print(john_times[i] + ", John - " + '"' + john_messages[i] + '"')
else:
    print("Time not found")
# Parallel arrays: Jane’s messages and times
jane_messages = ["Hi", "Free?", "Library?", "Leaving now"]
jane_times    = ["12:00", "12:02", "12:03", "12:20"]

# Parallel arrays: John’s messages and times
john_messages = ["Hi Jane", "Yes", "OK", "See you"]
john_times    = ["12:01", "12:02", "12:03", "12:21"]

# Ask the user for a starting time
target_time = input("Enter a time (hh:mm): ")

# Linear search for the first matching timestamp
pos = 0
found = False
while found == False and pos < len(jane_times):
    if jane_times[pos] == target_time or john_times[pos] == target_time:
        found = True
    else:
        pos = pos + 1

# If found, display the rest of the conversation
if found == True:
    for i in range(pos, len(jane_times)):
        print(jane_times[i] + ", Jane - " + '"' + jane_messages[i] + '"')
        print(john_times[i] + ", John - " + '"' + john_messages[i] + '"')
else:
    print("Time not found")

Extension

Update the program to let the user choose whether they want to see the chat history before (inclusive) a certain time or after (inclusive) a certain time.

Enter a time (hh:mm): 12:03
Do you want messages 'before' or 'after'? before

12:00, Jane - "Hi"
12:01, John - "Hi Jane"
12:02, Jane - "Free?"
12:02, John - "Yes"
12:03, Jane - "Library?"
12:03, John - "OK"