电脑面试两道问题(python+shell)

最近面试电脑代码面试遇到两个问题,供大家参考一下
一、python脚本:

手写一个函数,实现两个数相加,并使用unittest与pytest工具测试函数正确性。

1.unnitest进行测试:

复制代码
import unittest


def sum_two_number(a, b):
    return a + b


class Test_unittest(unittest.TestCase):
    """
    创建测试类
    """

    def test_function1(self):
        x = sum_two_number(2, 3)
        self.assertEqual(x, 5)

    def test_function2(self):
        x = sum_two_number(3, 10)
        self.assertEqual(x, 15)
复制代码

运行结果如下:

 

 2.pytest进行测试:

复制代码
import pytest


def sum_two_number(a, b):
    return a + b


def test_add_01():
    assert sum_two_number(2, 3) == 5


def test_add_02():
    assert sum_two_number(5, 3) == 10


if __name__ == '__main__':
    pytest.main(['-v', 'test_a_function.py'])
复制代码

查看运行结果:

 

二、shell脚本:

编写一个脚本,输出当前文件夹内所有文件非目录的文件名与大小,分别打印出来,第一行打印所有文件,第二行打印大小

原始文件如下:

echo `ls -lh  | grep "^-" | awk   '{print $9}'`;echo `ls -lh  | grep "^-" | awk   '{print $5}'`

查看运行结果:

 扩展:如果需要打印所有目录文件,及其大小如下:

echo `ls -lh  | grep "^d" | awk   '{print $9}'`;echo `ls -lh  | grep "^-" | awk   '{print $5}'`

查看运行结果:

 

 

posted @   Mrwhite86  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示