1. 程式人生 > >11道python程式設計題

11道python程式設計題

1. 給定一個四位數字的集合,能組成多少個互不相同且無重複數字的三位數?各是多少?

def getCombine(lst):
    for i in lst:
        for j in lst:
            for k in lst:
                if (i != k) and (i != j) and (j != k):
                    print i, j, k

2. 一個1000以內的整數,它加上100和加上268後都是一個完全平方數,請問該數是多少?

def completeNum():
    import math
    for
i in range(1000): x = int(math.sqrt(i + 100)) y = int(math.sqrt(i + 268)) if (x * x == i + 100) and (y * y == i + 268): return i

3. 給定一個日期字串(eg:2013-12-31),判斷輸入日期是當年中的第幾天?

def getDayno(dateStr):
    year,month,day = [int(x) for x in dateStr.split('-')]
    months = (0
, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334) if 0 < month <= 12: sum = months[month - 1] else: raise 'month out of range' sum += day leap = False if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)): leap = True if leap and month > 2
: sum += 1 return sum