2023年12月25日 星期一

使用遞迴控制列印次數

 def f(n):

print('i love python')

if n==1:return 

return f(n-1)


n = int(input())

f(n)

2023年12月22日 星期五

Python sys.getsizeof()

 import sys

print('bool:',sys.getsizeof(True) )

print('char:',sys.getsizeof('a') )

print('short:',sys.getsizeof(1) )

print('int:',sys.getsizeof(65537) )

print('float:',sys.getsizeof(1048577) )

print('double:',sys.getsizeof(1.1) )


bool: 28↵\r\n

char: 42↵\r\n

short: 28↵\r\n

int: 28↵\r\n

float: 28↵\r\n

double: 24↵\r\n

2023年12月21日 星期四

graph has loop ?

 #d = [[0,1],[1,2],[2,3],[3,4]] # no loop

d = [[0,1],[1,2],[2,3],[3,2]]

node = []

edge = []

for i in d:

    node.append(i[0])

    node.append(i[1])

    edge.append([i[0],i[1]])

  

node = set(node)

#edge = set(edge)

links = []

for i in node:

    t =str(i)

    for j in edge:

        if j[0]==i:

           t = t + str(j[1]) 

    links.append(t)


links1 = []

for i in links:

    t = i

    for j in edge:

        if i[-1] == str(j[0]):

            t = t + str(j[1])

    links1.append(t)

links2 = []

for i in links1:

    t = i

    for j in edge:

        if i[-1] == str(j[0]):

            t = t + str(j[1])

    links2.append(t)

    

for i in links2:

    if len(list(i))!=len(set(i)):

        print(i,':loop')

        

2023年6月14日 星期三

字數統計

d = '''

Full Transcript: NVIDIA CEO Jensen Huang's Commencement Address at National Taiwan University

Ladies and gentlemen, esteemed faculty members, distinguished guests, proud parents, and above all, the 2023 graduating class of the National Taiwan University. Today is a very special day for you, and a dream come true for your parents.

...

...
 You will endure pain and suffering needed to realize your dreams. And you will make sacrifices to dedicate yourself to a life of purpose and doing your life's work.

Class of 2023, I extend my heartfelt congratulations to each one of you.  Jiayou!

'''

d = d.replace(':',' ').replace('.',' ').replace('\'',' ').replace(',',' ')

d1= [ i for i in d.split() if i> '9' and len(i)>1]


s = set(d1)

r = []

for i in s:

    r.append( [d1.count(i),i])

r.sort()

r.reverse()

#for i in r[:100]:

#    print(i[0],':',i[1])

#

#r100 = r[0:100]

#s100 =0

#for i in r100:

#    s100 = s100 + i[0]

#p = s100/len(d1)*100

#p = int(p*100+0.5)/100

#print('會前', len(r100),'個字, 就可看懂整篇1781字文章的',p,'%')

#

#r200 = r[0:200]

#s200 =0

#for i in r200:

#    s200 = s200 + i[0]

#p = s200/len(d1)*100

#p = int(p*100+0.5)/100

#print('會前', len(r200),'個字, 就可看懂整篇1781字文章的',p,'%')

#

#r300 = r[0:300]

#s300 =0

#for i in r300:

#    s300 = s300 + i[0]

#p = s300/len(d1)*100




#p = int(p*100+0.5)/100

#print('會前', len(r300),'個字, 就可看懂整篇1781字文章的',p,'%')

for n in range(100,800,100):

    rr = r[0:n]

    s =0

    for i in rr:

        s = s + i[0]

    p = s/len(d1)*100

    p = int(p*100+0.5)/100

    print('會前', len(rr),'個字, 就可看懂整篇1781字文章的',p,'%')


#執行結果

#會前 100 個字, 就可看懂整篇1781字文章的 53.23 %

#會前 200 個字, 就可看懂整篇1781字文章的 67.66 %

#會前 300 個字, 就可看懂整篇1781字文章的 76.59 %

#會前 400 個字, 就可看懂整篇1781字文章的 82.2 %

#會前 500 個字, 就可看懂整篇1781字文章的 87.82 %

#會前 600 個字, 就可看懂整篇1781字文章的 93.43 %

#會前 700 個字, 就可看懂整篇1781字文章的 99.05 %

2023年2月24日 星期五

21世紀13日星期五

 import datetime


year = 2000

month = 1

day = 1


c = 0

date = datetime.date(year, month, day)

for i in range(200000):

    weekday = date.weekday()

    day = date.day

    month = date.month

    year = date.year

    

    if year>2099:

        break

    

    weekday_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    if day==13 and weekday_names[weekday] == 'Friday':

        print(f"{c}: {year}/{month}/{day} is a {weekday_names[weekday]}")

        c = c + 1     

    date = date + datetime.timedelta(days=1)

2023年1月29日 星期日

ChatGPT 竟然有道歉能力

 


2023年1月6日 星期五

矩陣轉置

 m = [[1,2],[3,4],[5,6]]

t = [[r[i] for r in m] for i in range(2)]

print(t)

2023年1月5日 星期四

python Date

 import datetime


print(datetime.date.today().strftime('%d/%m/%Y'))

mDate = datetime.date(1966,4,18)

print(mDate.strftime('%m/%d/%Y'))


mDateNextDay = mDate + datetime.timedelta(days=1)

print(mDateNextDay.strftime('%m/%d/%Y'))


mFirstBirthday = mDate.replace(year=mDate.year+60)

print(mFirstBirthday.strftime('%d/%m/%Y'))