zoukankan      html  css  js  c++  java
  • some 算法

    矩阵变换::

    请用一条语句将:
    arr = [[1, 2, 3, 'a'], [4, 5, 6, 'b'], [7, 8, 9, 'c']]
    转换装置矩阵为: [[1, 4, 7], [2, 5, 8], [3, 6, 9], ['a', 'b', 'c']]
    
    [   [ each[i] for each in arr]   for i in range(len(arr[0])) ]            
    

    排序bin

    请写一个函数将一个字符串列表进行排序, 这些字符串都含有数字的子串.
    若按默认的排序, 'foo10.txt'会排在'foo2.txt'之前(因为'1'比'2'小), 现在需要将'foo2.txt'排在'foo10.txt'之前,
    因为2比10小. 请写出这个函数(可以额外多写其他辅助函数,可以用python的各种库)
    
    输入比如: files = 'file3.txt file11.txt file7.txt file4.txt file15.txt'.split()
    调用函数: result = sort_strings(files)
    result就应该是列表: ['file3.txt', 'file4.txt', 'file7.txt', 'file11.txt', 'file15.txt']   
    
    sort(key = lambda item: int(item[:-4][4:]))
    

    托管类

    设计一个简单通用的托管类Proxy, 创建实例(p)时传入其他类实例(orig),即可以通过p调用所有orig的方法.
    例如:
    
        class Hello(object):
            def __init__(self, name):
                self.name = name
    
            def hello(self):
                print 'hello', self.name
    
        class Proxy(object):
            ...
    
    
        h = Hello('world')
        p = Proxy(h)
        p.hello()             # 应该间接调用h.hello(), 输出"hello world"
        print p.name          # 应该打印:world
        p.name  = 'jobs'
        p.hello()             # 应该间接调用h.hello(), 输出"hello jobs"
    
    
        请设计Proxy类. 注:不能简单为Proxy写简单hello方法.
    
    
    class Proxy(object):
    
        def __init__(self, some_obj):
            method_of_proxy_obj = [(method,getattr(some_obj, method)) for method in dir(some_obj) if callable(getattr(some_obj, method)) and not method.startswith('__')]
            for method_tuple in method_of_proxy_obj:
                name = method_tuple[0]
                method_obj = method_tuple[1]
                setattr(self, name, method_obj)
    

    找到 100 万个数字中的第100个大的数字

    1. split 100万数字为100份
    2. 找出每一份中的前100个
    3. 把这些数字组合起来排序,找出前100个
    
    这里的逻辑是,前100个数字,必定包含在每一份的前100个中。要证明这个可以用反证法,
    假设 n 属于前100 但是n 不在某一份的前100个数中。 
    那么, 该份的前100都比n大,所以n就不可能是这100万个数的前一百。
  • 相关阅读:
    2020/3/21 简单的学习
    2020/3/7 A-B
    2020/3/6 旋转骰子
    2020/3/6 美丽数组
    面向对象程序设计寒假作业2
    自我介绍
    深度优先搜索-迷宫问题(走迷宫题解)
    开机方案题解
    好吃的巧克力题解
    数楼梯题解
  • 原文地址:https://www.cnblogs.com/kramer/p/6021268.html
Copyright © 2011-2022 走看看