面试题
''' 一个list,里面的数字偶数在左边,奇数在右边,不借助其他列表 ''' def userlist(add_list): if type(add_list)==list: if len(add_list)==1 and type(add_list[0])=='int': return add_list for item in add_list: try: if item%2==0: add_list.remove(item) add_list.insert(0,item) except : return False return add_list else: return False
测试用例:
from ceshi import userlist import unittest,ddt data=[{'list':[0],'result':[0]}, {'list': ["a"], 'result':False}, {'list': 0, 'result': 0}, {'list': [1,1], 'result': [1,1]}, {'list': [1,2], 'result': [2,1]}, {'list': [1,2,3], 'result': [2,1,3]}] @ddt.ddt class Unit_Tes(unittest.TestCase): def setUp(self): pass def tearDown(self): pass @ddt.data(*data) def test1(self,data): self.assertEqual(userlist(data['list']),data['result']) if __name__=='__main__': unittest.main()