2025年1月15日 星期三

sorted

 students = [('john', 'A', 20), ('mary', 'B', 30), ('jack','C', 60)]


students = sorted(students,key=lambda x:x[0])

print(students)

students = sorted(students,key=lambda x:x[1])

print(students)

students = sorted(students,key=lambda x:x[2])

print(students)

students = sorted(students,key=lambda x:x[2],reverse=True)

print(students)


# Output:


# [('jack', 'C', 60), ('john', 'A', 20), ('mary', 'B', 30)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('john', 'A', 20), ('mary', 'B', 30), ('jack', 'C', 60)]

# [('jack', 'C', 60), ('mary', 'B', 30), ('john', 'A', 20)]


2025年1月14日 星期二

小群體

 n = int(input())

mapping = list(map(int,input().split()))

v = set()

cc = 0


for start in range(n):

  if start not in v:

    c = start

    while c not in v:

      v.add(c)

      c = mapping[c]

    cc += 1

print(cc)


# STDIN

# 5

# 1 2 0 4 3

# Output:

# 2

小群體

# 讀取輸入
n = int(input())
mapping = list(map(int, input().split()))

# 初始化變數
visited = set()  # 使用集合來記錄已訪問的節點
cycle_count = 0  # 記錄閉環數量

# 循環遍歷所有節點
for start_node in range(n):
    if start_node not in visited:  # 如果節點未被訪問
        current_node = start_node
        while current_node not in visited:
            visited.add(current_node)  # 標記當前節點為已訪問
            current_node = mapping[current_node]  # 跳到下一個節點
        cycle_count += 1  # 完成一個閉環

# 輸出結果
print(cycle_count)

改寫後的優化

  1. 可讀性提升

    • 改善了變數命名,例如:
      • mapping 替代原本的 f
      • visited 替代原本的 v
    • 使程式的邏輯更加直觀。
  2. 使用集合

    • set 結構在檢查和新增訪問節點時比 list 更高效,縮短了運行時間。
  3. 結構簡化

    • 去除了不必要的變數(如 tg)。
    • 使用 for 迴圈代替了 while 中的手動索引更新,簡化了控制流程。

範例測試

範例 1

輸入:

5
1 2 0 4 3

輸出:

2

範例 2

輸入:

4
1 0 3 2

輸出:

2

範例 3

輸入:

6
1 2 0 4 5 3

輸出:

3

這個改寫版本執行結果與原程式完全一致,但結構更加簡潔,易於理解和維護。

小群體

 # 小群體

n = int(input())

s = [i for i in range(n)]

f = [int(i) for i in input().split()]


v = []


c = 0

ct = 0

while len(v)<len(s):

  tg = [s[c]]

  p = s[c]

  while p not in v:

    v.append(p)

    

    q = f[s.index(p)]

    if q not in v:

      tg.append(q)

    else:

      break

    p = q

  

  ct +=1

  while c<len(s) and s[c] in v:

    c+=1

print(ct)


# STDIN

# 10

# 4 7 2 9 6 0 8 1 5 3

# Output:

# 4

apcs 秘密差_

 

d = input()
s1 = [int(d[i]) for i in range(len(d)) if i%2==1]
s2 = [int(d[i]) for i in range(len(d)) if i%2==0]
print(abs(sum(s1)-sum(s2)))

vr sample

 https://t945935t.blogspot.com/2024/12/vex-vr-sample-ii.html

apcs 紅綠灯

 # ans 1

# a,b = map(int,input().split())

# n = int(input())

# ts = [int(i) for i in input().split()] 

# sw = 0

# for t in ts:

#   r = t % (a+b)

#   if r<a:

#     w = 0

#   elif r==a:

#     w = b

#   else:

#     w = b - (r-a)

#   sw = sw + w

# print(sw)


# ans 2

# a,b = map(int,input().split())

# n = int(input())

# ts = [int(i) for i in input().split()] 

# sw = 0

# for t in ts:

#   r = t % (a+b)

#   if r<a:

#     w = 0

#   else:

#     w = b - (r-a)

#   sw = sw + w

# print(sw)


# ans 3

a,b = map(int,input().split())

n = int(input())

print(sum( [ 0 if t % (a+b)<a else b-(t % (a+b)-a) for t in [int(i) for i in input().split()]]))


# StdIn

# 4 3

# 3

# 12 16 25

# Output:

# 5

apcs 字串操作

 # 字串操作

def f1(s):

  ls = len(s)

  ss = ''

  for i in range(0,ls,2):

    ss = ss + s[i+1]+s[i]

  return ss


def f2(s):

  ls = len(s)

  ss = ''

  for i in range(0,ls,2):

    if s[i]>s[i+1]:

      ss = ss + s[i+1]+s[i]

    else:

      ss = ss + s[i]+s[i+1]

  return ss

  

def f3(s):

  ls = len(s)

  ss = ''

  m = ls//2

  s1 = s[:m]

  s2 = s[m:]

  for i in range(m):

    ss = ss + s1[i]+s2[i]

  return ss


s = input()  

k = int(input())

for i in range(k):

  x = int(input())

  if x==0:

    s =f1(s)

  elif x==1:

    s =f2(s)

  elif x==2:

    s =f3(s)

    

print(s)


# STDIN

# apcsntnu

# 1

# 2

# Output:


# anptcnsu

apcs 成績指標

  # 成績指標

n = int(input())

d = [int(i) for i in input().split()]

d.sort()

print(*d)


hashighlost = False

for i in d[::-1]:

  if i<60:

    print(i)

    hashighlost = True

    break

if hashighlost == False:print('best case')


haslowpass = False

for i in d:

  if i>=60:

    print(i)

    haslowpass = True

    break

if haslowpass == False:print('worst case')


# STDIN

# 2

# 73 83


# Output:

# 73 83

# best case

# 73

在 Windows Server 上使用 Flask 架設網站

 在 Windows Server  上使用 Flask 架設網站,您可以按照以下步驟進行:


步驟 1:安裝 Python

  1. 下載並安裝 Python

    • Python 官方網站 下載適合 Windows 的版本。
    • 安裝時勾選 Add Python to PATH
  2. 檢查 Python 是否安裝成功

    • 在命令提示字元中運行:
      python --version
      
    • 顯示版本號表示安裝成功。

步驟 2:安裝 Flask

  1. 使用 pip 安裝 Flask:
    pip install flask
    
  2. 確認安裝成功:
    pip show flask
    

步驟 3:編寫 Flask 應用

  1. 在工作目錄創建一個名為 app.py 的文件:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return '<h1>Hello, Flask on Windows Server!</h1>'
    
    if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000)
    
  2. 測試應用:

    • 在命令提示字元中運行:
      python app.py
      
    • 打開瀏覽器訪問 http://<server-ip>:5000,應顯示 Hello, Flask on Windows Server!

步驟 4:設置 Windows 防火牆

  1. 開啟 TCP 5000 埠:

    • 打開 控制面板 > 系統和安全 > Windows 防火牆
    • 點擊 高級設置 > 入站規則 > 新規則
    • 選擇 ,並指定 TCP 5000。
    • 命名規則並保存。
  2. 測試外部連線:

    • 從其他設備訪問 http://<server-ip>:5000

步驟 5:以服務形式運行 Flask 應用

  1. 安裝 Gunicorn 或 Waitress

    • Gunicorn 是專業的 WSGI 服務器,但在 Windows 上建議使用 Waitress。
    pip install waitress
    
  2. 使用 Waitress 運行 Flask

    • 修改命令啟動應用:
      waitress-serve --host=0.0.0.0 --port=5000 app:app
      
  3. 將 Flask 應用設為後台服務

    • 使用 Task Scheduler(工作排程器)或 NSSM(Non-Sucking Service Manager)將命令行設為持續運行。

步驟 6:升級到生產環境

  1. 使用域名與 SSL(可選)

    • 配置域名(例如通過 DNS 設定)指向伺服器 IP。
    • 安裝並配置 SSL(例如使用 Let’s Encrypt 提供的證書)。
  2. 反向代理(可選)

    • 使用 Nginx 或 IIS 作為反向代理,將 HTTP 請求轉發至 Flask 應用。
    • 配置文件範例(IIS 上):
      • 安裝 ARR(Application Request Routing)與 URL Rewrite 模組。
      • 配置轉發到 Flask 應用的反向代理規則。

2025年1月7日 星期二

APCS 字串操作

 def f1(s):

   ss = ''

   for i in range(1,len(s),2):

      ss = ss + s[i]+s[i-1]

   return ss 

      

def f2(s):

  ss = ''

  for i in range(1,len(s),2):

    if s[i-1]>s[i]:

      ss = ss +s[i]+s[i-1]

    else:

      ss = ss +s[i-1]+s[i]

  return ss  

def f3(s):

   m = len(s)//2

   s1 = s[:4]

   s2 = s[4:]

   ss = ''

   for i in range(len(s1)):

      ss = ss +s1[i]+s2[i]

   return ss

      


s = input()

k = int(input())

result = ''

tem = ''

for i in range(k):

  if i ==0:

    tem = s

  a = int(input())

  if a==0:

    tem = f1(tem)

  if a==1:

    tem = f2(tem)

  if a==2:

    tem = f3(tem)

result = tem    

print(result)


# STDIN

# apcsntnu

# 1

# 2

# 2

# 1


# Output:


# anptcnsu


# STDIN

# facebook

# 4

# 2

# 0

# 2

# 1

# 2

# 2

# 1


# Output:


# bocfkoae

紅綠灯

 a,b = map(int,input().split())

n = int(input())

ts =[ int(i) for i in  input().split()]

sw = 0

for t in ts:

  r = t % (a+b)

  if r < a:

    w = 0

  elif r == a:

    w = b

  else:

    w = b - (r-a)

    

  sw = sw + w

print(sw)


data:

10 10

1

14

Output:

6

2025年1月2日 星期四

子集合

 from itertools import combinations

d = [1,2,3]

dl = []

for i in range(len(d)+1):

cs = list(combinations(d,i))

for j in cs:

dl.append(j)

for i in dl:

print(','.join(map(str,i)))


1

2

3

1,2

1,3

2,3

1,2,3