菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
6
0

some 算法

原创
05/13 14:22
阅读数 3224

矩阵变换::

请用一条语句将:
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万个数的前一百。

发表评论

0/200
6 点赞
0 评论
收藏