• 全国绿色计算大赛 模拟赛第一阶段(Python)


    第1关求和

    class Task:
        def getSum(self, num1, num2):
            sum = 0
            for i in range(num1, num2 + 1):
                while (i != 0):
                    sum += i % 10
                    i /= 10
                    i = int(i)
            return sum
    
    
    if __name__ == '__main__':
        task = Task
        a = task.getSum(task, 15, 19)
        print(a)
    
    

    第2关文件查看器

    import os, sys
    
    
    class Task:
        def showDirTree(self, path):
            print("+--" + path.split('\')[-1])
            for dir in os.listdir(path):
                if os.path.isdir(path):
                    print(" " * 2 + "+--" + dir)
                    self.dealDir(task,path + "\" + dir, 4)
                else:
                    print(" " * 2 + "--" + dir)
    
        def dealDir(self, path, step):
            if (os.listdir(path) != None):
                for dir in os.listdir(path):
                    new_path = path + "\" + dir
                    if os.path.isdir(new_path):
                        print(" " * step + "+--" + dir)
                        self.dealDir(self, path + "\" + dir, step + 2)
                    else:
                        print(" " * step + "--" + dir)
    
    
    if __name__ == '__main__':
        path = "C:\Users\486\Desktop\test\root"
        task = Task
        task.showDirTree(task, path)
    
    

    第3关图片查看器

    import os, sys
    
    
    class Task:
        def showDirTree(self, path):
            print("+--" + path.split('\')[-1])
            for dir in os.listdir(path):
    
                if os.path.isdir(path + "\" + dir):
                    print(" " * 2 + "+--" + dir)
                    self.dealDir(task, path + "\" + dir, 4)
                else:
                    if (self.judge(self, dir)):
                        print(" " * 2 + "--" + dir)
    
        def dealDir(self, path, step):
            if os.path.isfile(path):
                if (self.judge(self, os.path.basename(path))):
                    print(" " * step + "--" + os.path.basename(path))
            else:
                if (os.listdir(path) != None):
                    for dir in os.listdir(path):
                        new_path = path + "\" + dir
    
                        if os.path.isdir(new_path):
                            print(" " * step + "+--" + dir)
                            self.dealDir(self, path + "\" + dir, step + 2)
                        else:
                            if (task.judge(self, dir)):
                                print(" " * step + "--" + dir)
    
        def judge(self, dir):
            if os.path.splitext(dir)[1] == ".jpg" or os.path.splitext(dir)[1] == ".png" or os.path.splitext(dir)[
                1] == ".bmp":
                return True
            else:
                return False
    
    
    if __name__ == '__main__':
        path = "C:\Users\486\Desktop\test\dir"
        task = Task
    
        task.showDirTree(task, path)
    
    
  • 相关阅读:
    设计模式20-观察者模式
    设计模式19-备忘录模式
    设计模式18-中介者模式
    设计模式17-迭代器模式
    设计模式16-解释器模式
    Wireshark基本介绍和学习TCP三次握手
    Jmeter CSV 参数化/检查点/断言
    Jmeter运营活动并发测试—巧用集合点
    一个简单的性能测试
    Jmeter对HTTP请求压力测试、并发测试
  • 原文地址:https://www.cnblogs.com/somliy/p/9868944.html
Copyright © 2020-2023  润新知