zoukankan      html  css  js  c++  java
  • 面试题整理

    编写函数,实现功能:将[1,2,[3,[4,5]],6,[7,]] 转换成[1,2,3,4,5,6,7]

    # 方法一
    li = [1,2,[3,[4,5]],6,[7,]]
    def func(args):
        lis = []
        for i in args:
            if isinstance(i,list):
                lis.extend(func(i))
            else:
                lis.append(i)
        return lis
    
    # 方法二
    def func(args):
        if isinstance(args,list):
            return [y for x in args for y in func(x)]
    
        else:
            return [args]

    [1,2,[3,[4,5]],6,[7,]]  用生成器将其生成[1,2,3,4,5,6,7]

    # 方法一
    li = [1,2,[3,[4,5]],6,[7,]]
    
    def func(args):
        for i in args:
            if isinstance(i,list):
                for j in func(i):
                    yield j
            else:
                yield i
    
    ret = func(li)
    print(list(ret))
    # 方法二
    li = [1,2,[3,[4,5]],6,[7,]]
    
    def func(argv):
        for i in argv:
            if isinstance(i,list):
                yield from func(i)
            else:
                yield i
    
    ret = func(li)
    print(list(ret))

    编写代码实现func函数,使其实现以下效果:
    foo = func(8)
    print(foo(8)) # 输出64
    print(foo(-1)) # 输出-8

    # 方法一
    def func(x):
        def inner(y):
            return x*y
        return inner
    # 方法二
    def func(x):
       return lambda y:x*y

    编写Python脚本,分析xx.log文件,按域名统计访问次数

    xx.log文件内容如下:
    https://www.sogo.com/ale.html
    https://www.qq.com/3asd.html
    https://www.sogo.com/teoans.html
    https://www.bilibili.com/2
    https://www.sogo.com/asd_sa.html
    https://y.qq.com/
    https://www.bilibili.com/1
    https://dig.chouti.com/
    https://www.bilibili.com/imd.html
    https://www.bilibili.com/

    脚本输出:
    www.bilibili.com
    www.sogo.com
    1 www.qq.com
    y.qq.com
    dig.chouti.com
     
    import re
    from collections import Counter
    
    file = r'xx.log'
    def readweb(file):
        com = re.compile('http[s]?://(.*?)/')
        with open(file,encoding='utf-8') as f:
            for line in f:
                yield com.search(line).group(1)
    
    
    g = readweb(file)
    ret = Counter(g)
    for url,count in ret.most_common():
        print(count,url)

    通过代码实现如下转换:

    二进制转换成十进制:v = “0b1111011”

    十进制转换成二进制:v = 18
    八进制转换成十进制:v = “011”
    十进制转换成八进制:v = 30
    十六进制转换成十进制:v = “0x12”
    十进制转换成十六进制:v = 87

     # 二进制转换成十进制:v = “0b1111011”
    v1 = "0b1111011"
    print(int(v1,2))
    
    # 十进制转换成二进制:v = 18
    v2 = 18
    print(bin(v2))
    
    # 八进制转换成十进制:v = “011”
    v3 = "011"
    print(int(v3,8))
    
    # 十进制转换成八进制:v = 30
    v4 = 30
    print(oct(v4))
    
    # 十六进制转换成十进制:v = “0x12”
    v5 = "0x12"
    print(int(v5,16))
    
    # 十进制转换成十六进制:v = 87
    v6 = 87
    print(hex(v6))

    请编写一个函数实现将IP地址转换成一个整数。

    如 10.3.9.12 转换规则为:
            10            00001010
             3            00000011
             9            00001001
           12            00001100
    再将以上二进制拼接起来计算十进制结果:00001010 00000011 00001001 00001100 = ?

    ip = "10.3.9.12"
    
    def get_int(argv):
        ret = "".join([bin(int(i)).lstrip("0b").zfill(8) for i in argv.strip().split(".")])
        return int(ret,2)
    
    print(get_int(ip))  

    python递归的最大层数?

    999

    求结果:
        v1 = 1 or 3
        v2 = 1 and 3
        v3 = 0 and 2 and 1
        v4 = 0 and 2 or 1
        v5 = 0 and 2 or 1 or 4
        v6 = 0 or Flase and 1

    1 3 0 1 1 False

    字节码和机器码的区别

    机器码(machine code),学名机器语言指令,有时也被称为原生码(Native Code),是电脑的CPU可直接解读的数据。
    
    字节码(Bytecode)是一种包含执行程序、由一序列 op 代码/数据对 组成的二进制文件。字节码是一种中间码,它比机器码更抽象,需要直译器转译后才能成为机器码的中间代码。

    用一行代码实现数值交换:
          a = 1
          b = 2

    a,b = b,a

     一行代码实现1--100之和

    a = sum(range(1,101))

    列表[1,2,3,4,5],请使用map()函数输出[1,4,9,16,25],并使用列表推导式提取出大于10的数,最终输出[16,25]

    l = [1,2,3,4,5]
    print([i for i in map(lambda x: x * x, l) if i > 10])

    python2和python3区别?列举5个

        1、Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi')

        Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比        如 print 'hi'

         2、python2 range(1,10)返回列表,python3中返回迭代器,节约内存

        3、python2中使用ascii编码,python中使用utf-8编码

        4、python2中unicode表示字符串序列,str表示字节序列

          python3中str表示字符串序列,byte表示字节序列

        5、python2中为正常显示中文,引入coding声明,python3中不需要

        6、python2中是raw_input()函数,python3中是input()函数

  • 相关阅读:
    CMD 已存在的表, 没有主键的 添加主键属性
    回调函数 call_back
    在Ubuntu下安装MySQL,并将它连接到Navicat for Mysql
    F查询和Q查询,事务及其他
    Djabgo ORM
    Diango 模板层
    Django视图系统
    Django简介
    Web 框架
    HTTP协议
  • 原文地址:https://www.cnblogs.com/st-st/p/9789162.html
Copyright © 2011-2022 走看看