Skip to content

Instantly share code, notes, and snippets.

@FGFW
Created March 7, 2016 23:33
Show Gist options
  • Select an option

  • Save FGFW/4b0ec52986bf94ca198a to your computer and use it in GitHub Desktop.

Select an option

Save FGFW/4b0ec52986bf94ca198a to your computer and use it in GitHub Desktop.
python解无忧公主数学题108回文.py
"""
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
"""
#利用到了集合的特性,进行集合交集运算
#2016年3月8日 05:59:41
def 方法1():
f=lambda x,y: True if str(x*y)==str(x*y)[::-1] else None
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()
#2016年3月8日 06:23:32
def ff2():
def f(x,y):
if str(x*y)==str(x*y)[::-1]:
return x
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)
#2016年3月8日 06:40:33
return results
ff2()
#2016年3月8日 06:59:08
#用正则匹配写一个
import re
def ff3():
def f(x,y):
if re.match(r"""^(\d?)(\d?)(\d?)(\d?)\4\3\2\1$""",str(x*y)):
return True
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)
#2016年3月8日 07:29:33
ff3()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment