想了想,可以完全可以省掉if语句。 | """ | | python解无忧公主数学题108回文.py | | 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==&mid=402360973&idx=1&sn=31014f87b8e65c9cd1d40c625c9c3d90&3rd=MzA3MDU4NTYzMw==&scene=6#rd | | 2016年3月7日 14:59:17 codegay | | """ | | | | | | | | def 方法1(): | | f=lambda x,y: str(x*y)==str(x*y)[::-1] | | x91={r for r in range(1,100) if f(r,91)} | | x93={r for r in range(1,100) if f(r,93)} | | x95={r for r in range(1,100) if f(r,95)} | | x97={r for r in range(1,100) if f(r,97)} | | print("方法1结果:",x91&x93&x95&x97) | | return (x91&x93&x95&x97) | | | | 方法1() | | | | | | def ff2(): | | | | def f(x,y): | | return str(x*y)==str(x*y)[::-1] | | | | results=[r for r in range(1,100) if f(r,91) and f(r,93) and f(r,95) and f(r,97)] | | print("方法2结果:",results) | | | | return results | | | | ff2() | | | | | | | | import re | | def ff3(): | | def f(x,y): | | return re.match(r"""^(\d?)(\d?)(\d?)(\d?)\4\3\2\1$""",str(x*y)) | | | | x91=[r for r in range(1,100) if f(r,91)] | | results=[r for r in x91 if f(r,93) and f(r,95) and f(r,97)] | | print("方法3结果:",results) | | | | | | ff3()COPY |
|