Discuss / Python / 交作业

交作业

Topic source
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':0.1}

def reverse(result,lst):
    if len(lst)==0:
        return result 
    result.append(lst.pop());
    return reverse(result,lst);

def is_palindrome(n):
    #把数字反过来
    m = reduce(lambda x,y:y+x*10,reverse([],list(map(lambda x:DIGITS[x],str(n)))))
    return n==m;

output = filter(is_palindrome, range(1, 200))
print('1~200:', list(output))
if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]:
    print('测试成功!')
else:
    print('测试失败!')

  • 1

Reply