2012-01-24 36 views
-1

我是一個完整的Python初學者和一般編程人員。我做了一個程序Spotify's Best Before puzzle。它被接受了。我在網上看到了一個小問題,並且看到了解決問題的其他解決方案,並且我見過的每個人都導入了幾個模塊,包括日曆模塊。我明白這可能是一個很好的解決方案,但我想把自己的一切都當成練習。簡化Python程序

我真的很感激所有的提示和提示,但主要是不需要導入代碼。這主要是需要修改的printer(a)和det dataMaker()

normYear = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 
leapYear = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 
answerList = [] 

u''' Check if any of the integers are years ''' 

def yearCheck(): 
    for x in xrange(0, 3): 
     a = dataList[x] 
     if len(a) > 2: 
      if not len(a) == 4 and int(a) in xrange(2000,3000): 
       if int(a) in xrange(100,1000): 
        dataList[x] = int(a) + 2000 
       else: 
        print data + u" is illegal" 

u''' Make integers and sort ''' 

def integer(): 
    for x in xrange(0, 3): 
     dataList[x] = int(dataList[x]) 
    dataList.sort() 

u''' Check for possible leap years ''' 

def leapYears(): 
    global leapList 
    leapList = [] 
    for x in xrange(0, 3): 
     if dataList[x] % 4 == 0: 
      if dataList[x] % 100 == 0: 
       if dataList[x] % 400 == 0: 
        leapList.append(x) 
      else: 
       leapList.append(x) 

u''' Changes year type ''' 

def defYear(a): 
    global xYear 
    if a in leapList: 
     xYear = leapYear 
    else: 
     xYear = normYear 

u''' Printer ''' 

def printer(a): 
    if dataList[a] < 2000: 
     dataList[a] += 2000 
    year = dataList[a] 
    del dataList[a] 
    if not dataList[0] == 0: 
     month = dataList.pop(0) 
     day = dataList.pop(0) 
     answerList.append(unicode(year)) 
     answerList.append(unicode(u'%02d' % month)) 
     answerList.append(unicode(u'%02d' % day)) 
     print u'-'.join(answerList) 
    else: 
     print data + u" is illegal" 

u''' Looks for legal dates, first [Y<M<D] then [M<Y<D] then [M,D,Y] ''' 

def dateMaker(): 
    for x in xrange(0,4): 
     defYear(x) 
     if x == 0: 
      if dataList[1] <= 12 and dataList[2] <= xYear[dataList[1]-1]: 
       printer(x) 
       break 
     elif x == 1: 
      if dataList[0] <= 12 and dataList[2] <= xYear[dataList[0]-1]: 
       printer(x) 
       break 
     elif x == 2: 
      if dataList[0] <= 12 and dataList[1] <= xYear[dataList[0]-1]: 
       printer(x) 
       break 
     else: 
      print data + u" is illegal" 

u''' Program ''' 

data = raw_input() 
dataList = data.split(u"/") 
yearCheck() 
integer() 
leapYears() 
dateMaker() 
+1

嗨。這不是一個真正的問題。如果有問題,請指出預期結果和錯誤。如果它真的有效,那麼http://codereview.stackexchange.com/是這篇文章的地方。 –

+0

好的,謝謝! –

+0

我應該刪除它,還是讓它成爲? –

回答

1

我會考慮嘗試重新實現這個使用類。即使作爲一個練習,這可能是一個很好的做法。

你還應該考慮:

我會考慮導入第2個功能如下(有可能進一步改進辦法):

data = '8/5/32' 
data_list = data.split('/') 

def yearCheck(data_list): 
    # Years may be truncated to two digits and may in that case 
    # also omit the leading 0 (if there is one), so 2000 could 
    # be given as "2000", "00" or "0" (but not as an empty string). 
    # Further examples: 
    # if 2099, could be given as 99 
    # if 2005, could be given as 05 or 5 
    # 199 will not happen i.e. doesn't say that years may be 
    #  truncated to three digits 
    for index, item in enumerate(data_list): 
     if len(item) > 4: 
      # e.g. 30000 
      print item, '- Data is invalid' 
      return 
     if len(item) == 4 and int(item) not in xrange(2000, 3000): 
      # e.g. 3015 
      print item, '- Data is invalid' 
      return   
     if len(item) == 3: 
      # e.g. 199 
      print item, '- Data is invalid' 
      return 
     if len(item) < 3 and int(item) in xrange(32, 100): 
      data_list[index] = int(item) + 2000 
    return data_list 

def integer(data_list): 
    int_data_list = [int(item) for item in data_list] 
    return int_data_list.sort() 

yearCheck(data_list) 
integer(data_list) 
print data_list