Skip to main content
deleted 176 characters in body
Source Link
Ludisposed
  • 11.6k
  • 2
  • 39
  • 91

As I completely misunderstood the question before, here is my second try.

from operator import itemgetter
from itertools import groupby

def r_reduce(r):   
    # Building set items in the range
    range_set = set()
    for sublist in r:
        range_set = range_set | set(range(sublist[0], sublist[1]+1))

    # ! When using the example input !
    # At this stage sets is {2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17}
    
    # And you can clearly see this is a full range
    # Now we can chop the set up in smaller sub_ranges

    # Making ranges of the set
    reduced = []    
    for key, group in groupby(enumerate(sets), lambda i: i[0] - i[1]):        
        r = list(map(itemgetter(1), group))
        reduced.append([r[0], r[-1]])
    return reduced

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop

As I completely misunderstood the question before, here is my second try.

from operator import itemgetter
from itertools import groupby

def r_reduce(r):   
    # Building set items in the range
    range_set = set()
    for sublist in r:
        range_set = range_set | set(range(sublist[0], sublist[1]+1))

    # ! When using the example input !
    # At this stage sets is {2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17}
    
    # And you can clearly see this is a full range
    # Now we can chop the set up in smaller sub_ranges

    # Making ranges of the set
    reduced = []    
    for key, group in groupby(enumerate(sets), lambda i: i[0] - i[1]):        
        r = list(map(itemgetter(1), group))
        reduced.append([r[0], r[-1]])
    return reduced

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop

As I completely misunderstood the question before, here is my second try.

from operator import itemgetter
from itertools import groupby

def r_reduce(r):   
    # Building set items in the range
    range_set = set()
    for sublist in r:
        range_set = range_set | set(range(sublist[0], sublist[1]+1))

    # ! When using the example input !
    # At this stage sets is {2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17}
    
    # And you can clearly see this is a full range
    # Now we can chop the set up in smaller sub_ranges

    # Making ranges of the set
    reduced = []    
    for key, group in groupby(enumerate(sets), lambda i: i[0] - i[1]):        
        r = list(map(itemgetter(1), group))
        reduced.append([r[0], r[-1]])
    return reduced

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
Post Undeleted by Ludisposed
added 318 characters in body
Source Link
Ludisposed
  • 11.6k
  • 2
  • 39
  • 91

As I might be completely misunderstandingmisunderstood the question before, but to me thishere is written way to complicatedmy second try. This entire function can be replaced by a single one-liner:

inpfrom operator import itemgetter
from itertools import groupby

def r_reduce(r):   
    # Building set items in the range
    range_set = [[14set()
    for sublist in r:
        range_set = range_set | set(range(sublist[0],17] sublist[1]+1))

    # ! When using the example input !
    # At this stage sets is {2, [43,7] 4, [25,5] 6, [107,12] 8, [159,16] 11, [412,9] 13, [1114,13]] 15, 16, 17}
    
    # thisAnd doesyou exactlycan whatclearly yoursee functionthis does.is a full range
    # thisNow we can alsochop bethe doneset withup itemgetterin insteadsmaller sub_ranges

    # Making ranges of lambdathe set
zip_range    reduced = lambda[] li: ([[min(li, key = 
 lambda x: x[0])[0], max(li,for key, =group in groupby(enumerate(sets), lambda xi: x[1])[1]]]i[0] - i[1])
 :        
out        r = zip_rangelist(inpmap(itemgetter(1), group))
# out = [[2     reduced.append([r[0], 17]]r[-1]])
    return reduced

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop

I might be completely misunderstanding the question, but to me this is written way to complicated. This entire function can be replaced by a single one-liner:

inp = [[14,17], [4,7], [2,5], [10,12] , [15,16], [4,9], [11,13]]

# this does exactly what your function does.
# this can also be done with itemgetter instead of lambda
zip_range = lambda li: ([[min(li, key = lambda x: x[0])[0], max(li, key = lambda x: x[1])[1]]])
 
out = zip_range(inp)
# out = [[2, 17]]

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop

As I completely misunderstood the question before, here is my second try.

from operator import itemgetter
from itertools import groupby

def r_reduce(r):   
    # Building set items in the range
    range_set = set()
    for sublist in r:
        range_set = range_set | set(range(sublist[0], sublist[1]+1))

    # ! When using the example input !
    # At this stage sets is {2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17}
    
    # And you can clearly see this is a full range
    # Now we can chop the set up in smaller sub_ranges

    # Making ranges of the set
    reduced = []     
    for key, group in groupby(enumerate(sets), lambda i: i[0] - i[1]):        
        r = list(map(itemgetter(1), group))
        reduced.append([r[0], r[-1]])
    return reduced

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop
Post Deleted by Ludisposed
Source Link
Ludisposed
  • 11.6k
  • 2
  • 39
  • 91

I might be completely misunderstanding the question, but to me this is written way to complicated. This entire function can be replaced by a single one-liner:

inp = [[14,17], [4,7], [2,5], [10,12] , [15,16], [4,9], [11,13]]

# this does exactly what your function does.
# this can also be done with itemgetter instead of lambda
zip_range = lambda li: ([[min(li, key = lambda x: x[0])[0], max(li, key = lambda x: x[1])[1]]])

out = zip_range(inp)
# out = [[2, 17]]

When you are overcomplicating stuff things get progressively more difficult and prone to bugs. Shorter most of the time really is better.

Now let's break down your code and review:

  1. while loops per se are not a bad idea, it is just using a while loop for this example is overcomplicating stuff. That makes it a bad idea.
  2. reduced_zip_ranges[-1] is awkward because you keep appending that item to the list and refering last element, you could also have stored the range as a variable and keep overwritting it.
  3. Again overcomplicating stuff, why is there a need to check those ranges in the first place?
  4. You can avoid this, by doing it in one loop, instead of looping in a loop