def combine_overlapping_ranges(ranges):
    # Sort the ranges based on the starting value
    ranges.sort(key=lambda x: x[0])
    
    merged = []
    explanations = []
    
    for current in ranges:
        # If merged is empty or there is no overlap, add the current range
        if not merged or merged[-1][1] < current[0]:
            merged.append(current)
        else:
            # There is an overlap, so we merge the current range
            last_range = merged[-1]
            explanations.append(f"Ranges {last_range} and {current} overlap, so they are combined into [{last_range[0]}, {max(last_range[1], current[1])}].")
            last_range[1] = max(last_range[1], current[1])
    
    return merged, explanations

def main():
    # Prompt the user to enter ranges
    user_input = input("ranges = ")
    
    try:
        # Evaluate the user input to a list of lists
        ranges = eval(user_input)
        
        # Check if the input is in the correct format
        if not all(isinstance(r, list) and len(r) == 2 for r in ranges):
            raise ValueError("Invalid input format. Each range must be a list of two integers.")
        
        # Combine the overlapping ranges
        result, explanations = combine_overlapping_ranges(ranges)
        
        # Print the result
        print("Output:", result)
        
        # Print explanations
        if explanations:
            for explanation in explanations:
                print(explanation)
        else:
            print("No overlaps found.")

    except Exception as e:
        print("Error:", e)

if __name__ == "__main__":
    main()
