util.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:Projects 作者: it2school 项目源码 文件源码
def majorize(values):
    """Filter sequence to return only major considered numbers"""
    sorted_values = sorted(values)
    if len(values) <= 3 or (
            abs(2 * sorted_values[1] - sorted_values[0] - sorted_values[2]) >
            abs(1.5 * (sorted_values[1] - sorted_values[0]))):
        return []
    values_step = sorted_values[1] - sorted_values[0]
    full_range = sorted_values[-1] - sorted_values[0]
    step = 10 ** int(log10(full_range))
    if step == values_step:
        step *= 10
    step_factor = 10 ** (int(log10(step)) + 1)
    if round(step * step_factor) % (round(values_step * step_factor) or 1):
        # TODO: Find lower common multiple instead
        step *= values_step
    if full_range <= 2 * step:
        step *= .5
    elif full_range >= 5 * step:
        step *= 5
    major_values = [
        value for value in values if value / step == round(value / step)]
    return [value for value in sorted_values if value in major_values]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号