基于时间的轮换
我正在尝试找出执行以下操作的最佳方法:
我有一个值列表:L
我想选择此列表的一个大小为 N 的子集,并每 X 分钟获取一个不同的子集(如果列表有足够的成员)。
我希望顺序或随机地选取这些值,只要使用所有值即可。
例如,我有一个列表:[google.com, yahoo.com, gmail.com]
我想选择 X(本例中为 2)个值,并每 Y(现在为 60)分钟轮换这些值:
第 0-59 分钟:[google.com, yahoo.com]
第 60-119 分钟:[gmail.com、google.com
第 120-179 分钟:[google.com、yahoo.com]
等等,
随机挑选也可以,即:
第 0-59 分钟:[google.com, gmail.com]
分钟60-119:[yahoo.com, google.com]
注意:当用户设置旋转向上时,时间纪元应为0,即0点可以是任何时间点。 最后:如果可能的话,我不想存储一组“已使用”值或类似的值。即,我希望这尽可能简单。
随机挑选实际上优于顺序挑选,但两者都可以。 解决这个问题的最佳方法是什么? Python/伪代码或C/C++ 都可以。
谢谢你!
I'm trying to figure out the best way of doing the following:
I have a list of values: L
I'd like to pick a subset of this list, of size N, and get a different subset (if the list has enough members) every X minutes.
I'd like the values to be picked sequentially, or randomly, as long as all the values get used.
For example, I have a list: [google.com, yahoo.com, gmail.com]
I'd like to pick X (2 for this example) values and rotate those values every Y(60 for now) minutes:
minute 0-59: [google.com, yahoo.com]
minute 60-119: [gmail.com, google.com
minute 120-179: [google.com, yahoo.com]
etc.
Random picking is also fine, i.e:
minute 0-59: [google.com, gmail.com]
minute 60-119: [yahoo.com, google.com]
Note: The time epoch should be 0 when the user sets the rotation up, i.e, the 0 point can be at any point in time.
Finally: I'd prefer not to store a set of "used" values or anything like that, if possible. i.e, I'd like this to be as simple as possible.
Random picking is actually preferred to sequential, but either is fine.
What's the best way to go about this? Python/Pseudo-code or C/C++ is fine.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
itertools
标准模块来帮助:编辑:根据您在评论中的澄清,以下建议可能会有所帮助。
您正在寻找的是 [0, N) 范围内的最大长度整数序列。您可以使用以下方法在 Python 中生成此值:
给定一个整数
n
和一个素数p
(它不是n
的因子,使得p
大于n
保证了这一点),您将获得从 0 到n
-1 的所有整数的序列:从那里,您可以过滤此列表以仅包含包含所需数量的 1 位集的整数(参见最佳算法计算 32 位整数中设置位的数量? 获取建议)。然后根据设置为 1 的位从集合中选择元素。在您的情况下,您将使用传递
n
作为 2N ifN
是集合中元素的数量。给定时间 T(从中可以找到序列中的位置)、元素数量 N 和素数 P,该序列是确定性的。
You can use the
itertools
standard module to help:EDIT: Based on your clarification in the comments, the following suggestion might help.
What you're looking for is a maximal-length sequence of integers within the range [0, N). You can generate this in Python using something like:
Given an integer
n
and a prime numberp
(which is not a factor ofn
, makingp
greater thann
guarantees this), you will get a sequence of all the integers from 0 ton
-1:From there, you can filter this list to include only the integers that contain the desired number of 1 bits set (see Best algorithm to count the number of set bits in a 32-bit integer? for suggestions). Then choose the elements from your set based on which bits are set to 1. In your case, you would use pass
n
as 2N ifN
is the number of elements in your set.This sequence is deterministic given a time T (from which you can find the position in the sequence), a number N of elements, and a prime P.