Frage

I have seen this question been asked before but I'm looking for an answer with a twist:

Consider I have a range like 1-100 and i want to generate a list, with a specific stepsize like: numbers(1,100,5). This would return [1,25,50,75,100]. However, I would like it to return [1,100,50,25,75] or [1,100,50,75,25] Another example would be numbers(1,10,10) which would give me something similar to [1,10,5,2,7,3,8,4,9].

Is this even possible to do? The reason for this would be to be able to render image sequences without going from frame 1 to frame 2 to frame 3 and so forth. Instead I want to render the first frame, the last frame, the middle frame, the middle of the middle frame until all frames are accounted for.

War es hilfreich?

Lösung

Your question is kind of underspecified, but this should help you get started.

def numbers(first, last, count):
    nums = [first, last]
    width = last - first
    while len(nums) < count:
        width /= 2
        stepper = first
        while stepper < last:
            rounded = round(stepper)
            if rounded not in nums:
                nums.append(rounded)
                if len(nums) == count:
                    break
            stepper += width
    return nums

Then:

>>> numbers(0, 100, 5)
[0, 100, 50, 25, 75]
>>> numbers(0, 10, 10)
[0, 10, 5, 2, 8, 1, 4, 6, 9, 3, 7]
>>> numbers(0, 50, 50)
[0, 50, 25, 12, 38, 6, 19, 31, 44, 3, 9, 16, 22, 28, 34, 41, 47, 2, 5, 8, 11, 14, 17, 20, 23, 27, 30, 33, 36, 39, 42, 45, 48, 1, 4, 7, 10, 13, 15, 18, 21, 24, 26, 29, 32, 35, 37, 40, 43, 46]

The basic algorithm is as follows:

  • Start with a list of nums containing the two endpoints
  • Initialize width to the distance between the two endpoints
  • Then, loop:
    • Halve width
    • Step through first, first+width, first+2*width, ..., last-width, last, and add whichever among those are not already in nums to nums (so, for numbers(0, 100, 5), the first loop iteration will try 0, 50, and 100, and only add 50 since that wasn't already present; the second iteration will try 0, 25, 50, 75, and 100, and only add 25 and 75).
    • If we have enough numbers in nums, we're done
  • Return nums

Andere Tipps

Ok, so your desired frames are kind of wierd, especially as the elements in the first example aren't evenly ranged, eg. 100-75=25, 75-50=25, 50-25=25 , 25-1=24.

But, if we assume that you always want the start and end value in the frame and want the evenly spaced values pegged against the maximum value, we can do this:

def numbers(start,stop,step=1):
    frame = [start]+range(y,stop,(start-stop)/(step-1))
    return frame

The random module includes a shuffle() method that takes an array and shuffles it in-place.

Which means the function becomes:

from random import shuffle
def numbers(start,stop,step=1):
    frame = [start]+range(y,stop,(start-stop)/(step-1))
    shuffle(frame)
    return frame

Giving us the following test runs:

>>> numbers(1,100,5)
[100, 50, 75, 1, 25]
>>> numbers(1,10,10)
[1, 3, 10, 9, 6, 5, 8, 4, 7, 2]

Original (but wrong) answer

The random module includes a shuffle() method that takes an array and shuffles it in-place.

For example:

from random import shuffle
def numbers(start,stop,step=1):
    frame = range(start,stop,step)
    shuffle(frame)
    return frame

Then calling this function we get:

>>> numbers(1,100,25)
[1, 51, 26, 76]
>>> numbers(1,100,25)
[76, 26, 1, 51]

Note, that as per the range() function, the step value is repeatedly added to start such that the final array is of the form [start, start+1*step, start+2*step ... start + n*step], where start+n*step is less than stop while start+(n+1)*step is greater than stop.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top