0

I'm studying algorithms and one of the questions asks me to build an in-house calendar tool which stores data as a tuple of integers. My goal is to write a function that takes a list of multiple meeting time ranges and returns a list of condensed ranges.

So far, I've written pseudo-code and some real code. However, I'm trying to append overlapping times (represented as integers) and I'm stuck. Here's what I have so far:

# Variable containing list of tuples
meeting_times = [(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]

# Sort meetings by start time
ordered_list = sorted(meeting_times)

# For each number in the variable
for m in range(ordered_list):

    # If number overlaps with another number in variable
    if m[0:] >= m[:-1]:

        # Append start time of first number to end time of last number
        append_meeting_time =
    else:
        # Continue on and loop through variable.

While it's not complete, I'd like to know if I'm the right path. If not, how can I improve my answer?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Adam Smock
  • 61
  • 6
  • Can you use libraries or is this some sort of constrained exercise? – Stephen Rauch Sep 03 '18 at 00:15
  • I don't see the benefit of writing pseudo code alongside real code in a snippet this small. Either write pseudo code, or write real python. This snippet is easy enough to test! Try things out, see what works and what doesn't. By the time you've received an answer, you could've probably solved the issue by just _trying some stuff_. – Bram Vanroy Sep 03 '18 at 00:23
  • Nah, I can't use libraries. I'm doing these exercises to prepare for the inevitable technical interview. It's going to have to be vanilla Python. – Adam Smock Sep 03 '18 at 00:25
  • 1
    More on topic, though. Your if-statement does not make sense. You don't need slices, either, because you know you only have two items per tuple. You probably want something like this instead: `if ordered_list[m][1] >= ordered_list[m+1][0]`. You still have to make sure that **1** this does not trigger on the last index, **2** takes into account more than two overlapping tuples, **3** the correct output to be generated. – Bram Vanroy Sep 03 '18 at 00:27
  • `for m in range(ordered_list):` this is not functional, perhaps you mean `for m in range(len(ordered_list)):` but then you have to call `odered_list[m][0:]` – vash_the_stampede Sep 03 '18 at 01:02
  • I dont think you are applying slices how you intended, again how @BramVanroy said you want soemthing more like `[m][0] > [m][-1]` – vash_the_stampede Sep 03 '18 at 01:11
  • [It's unclear what "Append start time of first number to end time of last number" means. What's the expected output?](http://idownvotedbecau.se/unclearquestion). – ivan_pozdeev Sep 03 '18 at 01:54
  • Possible duplicate of https://stackoverflow.com/questions/5679638/merging-a-list-of-time-range-tuples-that-have-overlapping-time-ranges – ivan_pozdeev Sep 03 '18 at 01:54

1 Answers1

0

Assuming your meeting times will not overlap each other because you cannot be in two places in the same time. Thus, (1,4) (3,5) cannot happen.

One way to do this is to iterate with a variable that remembers the previous tuple, in this case I'll name it "previous", I'll keep your variable "m" as it is.

Every time you go on the next tuple in your meeting times, you compare the ending meeting time from previous tuple with current starting meeting time. Then you remove the previous tuple because it will be a duplicate. If no condense operation, then you can just append to the list and then set "m" to "previous" for the next iteration.

# Variable containing list of tuples
meeting_times = [(0, 1), (3, 5), (4, 8), (10, 12), (9, 10)]

# Sort meetings by start time
ordered_list = sorted(meeting_times)

print('Given time tuple: ', ordered_list)


def condensed_meeting_time(append_meeting_time, previous):
    # For each number in the variable
    for m in ordered_list:
        if previous: # This is to catch empty tuple that was initial
            if m[0] == previous[1]: # If current starting meeting time is equal to previous ending meeting time ...
                del append_meeting_time[-1] # Remove the previous element
                append_meeting_time.append( (previous[0], m[1]) ) # Create new tuple to merge previous start time to current ending time
            else: # If the time does not overlap, append normally and set current meeting time to 'previous' for next iteration
                append_meeting_time.append(m)
                previous = m
        else: # Base case to append the first element.
            append_meeting_time.append(m)
            previous = m
    return append_meeting_time

print('After condensed range: ',condensed_meeting_time([], ()))

This is the output I've got:

Given time tuple:  [(0, 1), (3, 5), (4, 8), (9, 10), (10, 12)]
After condensed range:  [(0, 1), (3, 5), (4, 8), (9, 12)]

I hope this helps.

PizzaPatz
  • 58
  • 7
  • Thanks a bunch, Pizza! I really hope I get something close to your solution if I get asked this question at a technical interview. – Adam Smock Sep 03 '18 at 03:20