iT邦幫忙

0

[D-354]Python 7/100天_練習4+5

  • 分享至 

  • xImage
  •  

=標題更新
昨天忘記打我學到課本的第幾天了,今天開始補上~
==今天的課題
課本:https://reurl.cc/ROvEv6
练习4:設計一個函數返回傳入的列表中最大和第二大元素的值
一樣
1.函數
2.讓使用者給一個list
3.回傳最大與第二大元素
感覺元素比較的話可以用list.sort然後split前面兩位,或者用if直接比較後對調排序
前者像這樣:

def biggie(list1):
    list.sort(list1)
    return list1[-2:]
biggie([2,3,11,5,8,1])

一次成功,好爽

def biggie(list1):
    for i in range(len(list1)):
        if list1[2]>list1[1]:
            list1.append(list1[2])
            list1.pop(2)
    print(list1)
biggie([5,4,12,6,2,9,12,3,55])#會卡住

後者寫不出來,感覺是邏輯出狀況,但基於我有寫出第一個方法,我允許自己直接看答案(?

def max2(x):
    m1, m2 = (x[0], x[1]) if x[0] > x[1] else (x[1], x[0])
#if-else跟變數寫在同一行,我直接沒了
    for index in range(2, len(x)):
        if x[index] > m1:
            m2 = m1
            m1 = x[index]
        elif x[index] > m2:
            m2 = x[index]
        print(x)
max2([1,55,6,7,22,44,2,123])

試著馬後砲一下:
1.不要試著去直接更動資料,如果像老師一樣另外寫兩個變數出來,就可以用for把整個list跑過一遍,就不會發生像剛剛一樣卡在第一次if執行完就不繼續跑
2.if-else可以寫在同一行。
剩下的就沒啥好說, ̶跟̶我̶的̶思̶路̶基̶本̶上̶一̶樣̶
=====
寫完居然還有一點時間,那就只好無情的離轉職更進一步ㄌ
練习5:计算指定的年月日是这一年的第几天。
這題沒有說要函數,但我覺得他只是忘記寫下要求,所以:
1.函數
2.讓使用者指定日期(年月日)儲存為list
3.識別閏年平年、其餘數字轉為天數加總
或是使用time模組來轉換成time再直接轉成天數?
總天數=日+往前的所有月分數*30+大月(1,3,5,7,8,10,12)的+1-(閏年-1or平年-2)
大月的概念這邊寫了段髒code解決

mon,count=12,0
for Big_mon in range(1,mon+1):
    if Big_mon<8and Big_mon %2!=0: count+=1
    elif Big_mon>=8 and Big_mon%2==0:count+=1
#不管變數多少,寫在同一行印出來就是666

再加入其他概念,code更髒了。

year,mon,big,total,date=20,9,0,0,11
if 12>mon>1: total=(mon-1)*30
for Big_mon in range(1,mon+1):
    if mon!=1 and Big_mon<8 and Big_mon %2!=0: big+=1
    elif mon!=1 and Big_mon>=8 and Big_mon%2==0:big+=1
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):total-=1 
else: total-=2#閏年公式
if mon==1:total=0
if mon==12:total=(mon-1)*30
print(total+big+date)
#超多if有沒有

如果說重複是代碼最壞的味道,我大概成功端出了餿水的世界冠軍
不過自己寫的程式,不含秤
最重要的是今天已經超額完成,加上我有點想睡了,明天起床再重構+看答案

=心得
今天的題目感覺都偏簡單(跟我昨天搞錯的副檔名還有前天自己加戲的跑馬燈比)
但今天有個最大的突破就是我發現漸漸能夠直接在腦中規劃這個程式碼要怎麼弄,也許有發現不知道能不能動或者需要去查詢相關功能的部分再用筆記記起來,這樣回去就只要處理每個問題,可以節省本來就不多的時間
希望能因此讓我的進度變快~~(真的不想再做服務業了OTL)

==還債
起床之後把昨天寫的東西整理了一下,主要是把潤年判斷與大小月判斷給發包出去變成另外的函數,然後在main的地方留下日子輸入跟輸出
發包之後才發現有些小地方有錯誤,只能說昨天運氣不錯才執行出對的數字(?

def is_leap_year(x=0):
    if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
        x-=1
    else:
        x-=2
    return x
def Big_mon(x=0):
    for Big_mon in range(1,mon):#原本這邊多+了1,害我怎麼算都多一天
        if mon!=1 and Big_mon<8 and Big_mon %2!=0: 
            x+=1
        elif mon!=1 and Big_mon>=8 and Big_mon%2==0:
            x+=1
    return x
def what_days_today(year,mon,date):
    if mon==1 :days=0
    else:days=(mon-1)*30
    return days+Big_mon(0)+is_leap_year(0)+date
what_days_today(2000,8,31)#244

看答案時間

def is_leap_year(year):
    """
    判断指定的年份是不是闰年

    :param year: 年份
    :return: 闰年返回True平年返回False
    """
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0

def which_day(year, month, date):
    """
    计算传入的日期是这一年的第几天

    :param year: 年
    :param month: 月
    :param date: 日
    :return: 第几天
    """
    days_of_month = [
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    ][is_leap_year(year)]
    total = 0
    for index in range(month - 1):
        total += days_of_month[index]
    return total + date


def main():
    print(which_day(1980, 11, 28))
    print(which_day(1981, 12, 31))
    print(which_day(2018, 1, 1))
    print(which_day(2016, 3, 1))


if __name__ == '__main__':
    main()

反省:
1.leap_year與分為幾個函數執行的思考是一樣的
2.完全沒想到可以用list來寫,或許之後的練習我該優先使用當章所教的東西來發想


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言