九宫格
#!/usr/bin/env python
#!--coding:utf-8 --
#!shenjie :2018/1/28 22:58
#[email protected] :shenjie
#[email protected]: 2.py
#!/usr/bin/env python
#-- coding:utf-8 --
class NinePaper(object):
def init(self):
self.numbers = list()
for i in range(1, 10):
self.numbers.append(i)
print("numbers = {0}".format(self.numbers))
def run(self):
lines = [(x,y,z) for x in self.numbers for y in self.numbers if x!=y for z in self.numbers if x!=z and y!=z and x+y+z == 15]
for line1 in lines:
for line2 in lines:
if set(line1) & set(line2):
continue
for line3 in lines:
if set(line1) & set(line2) & set(line3):
continue
if line1[0] + line2[0] + line3[0] != 15:
continue
if line1[1] + line2[1] + line3[1] != 15:
continue
if line1[2] + line2[2] + line3[2] != 15:
continue
if line1[0] + line2[1] + line3[2] != 15:
continue
if line1[2] + line2[1] + line3[0] != 15:
continue
print(‘‘‘
|_{0}_|_{1}_|_{2}_|
|_{3}_|_{4}_|_{5}_|
|_{6}_|_{7}_|_{8}_|
‘‘‘.format(line1[0], line1[1], line1[2], line2[0], line2[1], line2[2], line3[0], line3[1], line3[2],))
def main():
ninePaper = NinePaper()
ninePaper.run()
if name == ‘main‘:
main()
函数入门
判断某天为某年的第几天:
分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天
#!/usr/bin/env python
year = int(raw_input(‘year:\n‘))
month = int(raw_input(‘month:\n‘))
day = int(raw_input(‘day:\n‘))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 <= month <= 12:
sum = months[month - 1]
else:
print ‘data error‘
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print ‘it is the %dth day.‘ % sum
原文地址:http://blog.51cto.com/jacksoner/2066211