码迷,mamicode.com
首页 > 其他好文 > 详细

3道面试题

时间:2018-12-25 14:31:52      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:...   rev   bsp   lis   exce   else   函数   code   pre   

1. 写一个函数, 输入一个字符串, 返回倒序排列的结果: 如: string_reverse(‘abcdef’),
返回: ‘fedcba’ (请采用多种方法实现)
s = abcdef
l = list(s)
l.sort(reverse=True)
s = ‘‘.join(l)
print(s)

s = abcdef
s1 = ‘‘
for i in s[::-1]:
    s1 += i
print(s1)

s = abcdef
l = []
[l.append(i) for i in s[::-1]]
s = ‘‘.join(l)
print(s)
2. 请用自己的算法, 合并如下两个list按大小排序, 并去除重复的元素:
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
l = []
list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
l = list1 + list2
print(list(set(l)))

list1 = [2, 3, 8, 4, 9, 5, 6]
list2 = [5, 6, 10, 17, 11, 2]
[list1.append(i) for i in list2 if i not in list1]
print(list1)

l = [2, 3, 8, 4, 9, 5, 6, 10, 17, 11]
print(sorted(l))

l = [2, 3, 8, 4, 9, 5, 6, 10, 17, 11]
l.sort()
print(l)
3.python 如何捕获异常
def num(s):
    try:
        for i in s:
            print(i)
    except Exception as e:
        print(e)
    finally:
        print(执行完毕)
num(kkk)

def num1(s):
    if type(s) == int:
        raise ValueError(s is not int)
    else:
        for i in s:
            print(i)

try:
    num1(wang)
except ValueError as e:
    raise  TypeError(type error)


def num2(s):
    assert type(s) != int, s is not int....
    for i in s:
        print(i)
num2(wang)

 

3道面试题

标签:...   rev   bsp   lis   exce   else   函数   code   pre   

原文地址:https://www.cnblogs.com/wangshijie95/p/10173571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!