Union of multiple ranges

Let’s say, (7, 10) and (11, 13) result into (7, 13): a = [(7, 10), (11, 13), (11, 15), (14, 20), (23, 39)] b = [] for begin,end in sorted(a): if b and b[-1][1] >= begin – 1: b[-1] = (b[-1][0], end) else: b.append((begin, end)) b is now [(7, 20), (23, 39)] EDIT: As @CentAu … Read more

How to set the subplot axis range

You have pylab.ylim: pylab.ylim([0,1000]) Note: The command has to be executed after the plot! Update 2021 Since the use of pylab is now strongly discouraged by matplotlib, you should instead use pyplot: from matplotlib import pyplot as plt plt.ylim(0, 100) #corresponding function for the x-axis plt.xlim(1, 1000)

Collapse continuous integer runs to strings of ranges

I think diff is the solution. You might need some additional fiddling to deal with the singletons, but: lapply(z, function(x) { diffs <- c(1, diff(x)) start_indexes <- c(1, which(diffs > 1)) end_indexes <- c(start_indexes – 1, length(x)) coloned <- paste(x[start_indexes], x[end_indexes], sep=”:”) paste0(coloned, collapse=”, “) }) $greg [1] “7:11, 20:24, 30:33, 49:49” $researcher [1] “42:48” … Read more

Creating a collapsed range from a pixel position in FF/Webkit

Here is my implementation of caretRangeFromPoint for old browsers: if (!document.caretRangeFromPoint) { document.caretRangeFromPoint = function(x, y) { var log = “”; function inRect(x, y, rect) { return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom; } function inObject(x, y, object) { var rects = object.getClientRects(); for (var i … Read more

How to create range in Swift?

Updated for Swift 4 Swift ranges are more complex than NSRange, and they didn’t get any easier in Swift 3. If you want to try to understand the reasoning behind some of this complexity, read this and this. I’ll just show you how to create them and when you might use them. Closed Ranges: a…b … Read more

Combined total for multiple jQuery-UI Sliders

Well here ya go: var sliders = $(“#sliders .slider”); sliders.each(function() { var value = parseInt($(this).text(), 10), availableTotal = 400; $(this).empty().slider({ value: 0, min: 0, max: 400, range: “max”, step: 10, slide: function(event, ui) { // Update display to current value $(this).siblings().text(ui.value); // Get current total var total = 0; sliders.not(this).each(function() { total += $(this).slider(“option”, “value”); … Read more