zoukankan      html  css  js  c++  java
  • python_求相邻数

    什么是相邻数?

      比如5,相邻数为4和6,和5相差1的数,连续相差为1的一组数

    需求:

      遍历inputList 所有数字,取出所有数字,判断是否有相邻数, 不相邻数字 和 相邻数字 都以 “数组”形式 添加到 outputList 中, 并且 每个“数组” 里 第一位 递减 补全两位数,末位 递增 补全两位数, 每一个数不能小于0, 不能大于 400

      ( 提示: 在inputList 中 "12,13" 是相邻的数字,视为一组, 需要以[10, 11, 12, 13, 14, 15] 数组形式添加到outputList 中,而 “3”没有相邻的数,也视为一组,需要以[1, 2, 3, 4, 5]数组形式添加到outputList中 )

      输入:
      inputList = [0, 3, 5, 6, 7, 9, 12, 13, 15, 16, 17, 19, 20, 21, 22, 25, 27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54, 57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82, 84, 85, 86, 89, 95, 96, 97, 98, 103, 104, 107, 108, 110, 111, 114, 116, 117, 118,   120, 121, 122, 124, 127, 132, 135, 137, 138, 139, 140, 145, 146, 148, 149, 150, 151, 155, 156, 160, 161, 166, 167, 170, 171, 172, 175, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 190, 193, 195, 196, 198, 202, 205, 208, 210, 211, 213, 214, 215, 217,   221, 226, 227, 228, 233, 234, 235, 240, 241, 246, 247, 249, 255, 257, 258, 261, 262, 263, 267, 268, 269, 270, 271, 272, 275, 278, 280, 282, 283, 284, 286, 287, 289, 291, 292, 295, 296, 298, 300, 302, 303, 304, 305, 306, 310, 315, 317, 319, 320, 321, 322,   323, 324, 325, 326, 328, 331, 336, 339, 341, 342, 344, 346, 349, 354, 355, 356, 362, 363, 365, 366, 367, 368, 371, 374, 376, 378, 382, 383, 388, 390, 393, 396, 399]

      输出 :
      outputList = [[0, 1, 2] , [1, 2, 3, 4, 5], [3, 4, 5, 6, 7, 8, 9], [7, 8, 9, 10, 11],[10, 11, 12, 13, 14, 15] , ........此处省略]

    那,如何解决这个问题?

      1. 设置一个值,指向index=0, start_index = 0

      2. 初始化一个中间列表median = [ ] , 一个保存结果列表 result_l = [ ]

      3. for循环开始, start_index 指向每一个相邻数的开头

      4. 通过索引指向的值和索引后指向的值进行差值比较,步长不为1的,start_index移动到这个值上

      5. 循环往复,获得相邻列表

      6. 通过map函数,对每一个相邻列表进行前后各插入两个相邻数

      7. 通过列表解析, 剔除不满足条件的相邻数

    #!/usr/bin/python3
    
    __author__ = 'beimenchuixue'
    __blog__ = 'http://www.cnblogs.com/2bjiujiu/'
    
    
    def go_cha_ru(new_l):
        """往列表中前后个插入两个相邻数,通过列表解析去除小于0的和大于400的数"""
        new_l.insert(0, new_l[0] - 1)
        new_l.insert(0, new_l[0] - 1)
        new_l.append(new_l[len(new_l) - 1] + 1)
        new_l.append(new_l[len(new_l) - 1] + 1)
        return [i for i in new_l if 0 <= i <= 400]
    
    
    def go_xiang_lin(raw_l):
        """获取相邻数"""
        start_index = 0
        result_l = []
        median = []
        
        # 索引从start_index起,到最后
        for raw_index in range(len(raw_l)):
            # 判断是否for循环到指定位置
            if start_index == raw_index:
                # 初始移动位置参数
                index = 0
                while True:
                    # 指针指向的起始值
                    start_value = raw_l[start_index]
                    # 如果指针指向最后一个位置,开始值=最后一个值
                    if start_index == len(raw_l)-1:
                        end_value = start_value
                    else:
                        # 最后一个值 = 初始值 + 位置参数值
                        end_value = raw_l[start_index + index]
                    # 通过初始值 + 位置参数值 是否等于 最后一个值,判断是否为相邻数,如果是,添加到中间列表
                    if start_value + index == end_value:
                        median.append(end_value)
                        # 位置参数 + 1
                        index += 1
                    else:
                        # 如果不是,初始指针指向 移动位置参数个单位
                        start_index += index
                        # 把每主相邻数添加到结果列表
                        result_l.append(median)
                        median = []
                        break
        # 通过高阶函数,对结果集中每个相邻数列表进行插值操作
        return map(go_cha_ru, result_l)
    
    if __name__ == '__main__':
        input_list = [0, 3, 5, 6, 7, 9,
                      12, 13, 15, 16, 17, 19, 20, 21, 22, 25,
                      27, 29, 30, 32, 33, 36, 39, 40, 43, 44, 46, 47, 48, 53, 54,
                      57, 58, 60, 62, 64, 65, 66, 67, 72, 74, 75, 76, 77, 78, 80, 82,
                      84, 85, 86, 89, 95, 96, 97, 98, 103, 104, 107, 108, 110, 111, 114,
                      116, 117, 118, 120, 121, 122, 124, 127, 132, 135, 137, 138, 139, 140,
                      145, 146, 148, 149, 150, 151, 155, 156, 160, 161, 166, 167, 170, 171,
                      172, 175, 178, 179, 180, 181, 182, 183, 184, 186, 188, 189, 190, 193,
                      195, 196, 198, 202, 205, 208, 210, 211, 213, 214, 215, 217, 221, 226,
                      227, 228, 233, 234, 235, 240, 241, 246, 247, 249, 255, 257, 258, 261,
                      262, 263, 267, 268, 269, 270, 271, 272, 275, 278, 280, 282, 283, 284,
                      286, 287, 289, 291, 292, 295, 296, 298, 300, 302, 303, 304, 305, 306,
                      310, 315, 317, 319, 320, 321, 322, 323, 324, 325, 326, 328, 331, 336,
                      339, 341, 342, 344, 346, 349, 354, 355, 356, 362, 363, 365, 366, 367,
                      368, 371, 374, 376, 378, 382, 383, 388, 390, 393, 396, 399]
        # 结果
        output_list = list(go_xiang_lin(input_list))
        print(output_list)
    

      

  • 相关阅读:
    js 字符串indexOf方法封装
    js 冒泡排序
    CSS定位 position的三个属性 elative 、absolute、fixed :
    让父元素能感知浮动的子元素 #用伪元素清除浮动
    三个路由器的连接,中间路由的配置(静态路由)
    IDEA 添加tomcat出错: Error: Environment variable name is not set 我的解决方法
    通过基于AspectJ 注解的方式实现Spring AOP报 can't find referenced pointcut myPointCut 错误,我的解决方法
    C语言fopen函数打开文本文件与二进制文件的区别
    位运算的奇技淫巧 系列1
    位运算例子(以后会逐渐补充)
  • 原文地址:https://www.cnblogs.com/2bjiujiu/p/7384318.html
Copyright © 2011-2022 走看看