2024年12月19日 星期四

tree height

 from collections import defaultdict


def insert(tree,current,value):

  if value < current:

    if not tree[current][0]:

      tree[current][0]=value

    else:

      insert(tree,tree[current][0],value)

  else:

    if not tree[current][1]:

      tree[current][1]=value

    else:

      insert(tree,tree[current][1],value)

      

def height(tree,node):

  if not node:

    return 0

  l_height = height(tree,tree[node][0])

  r_height = height(tree,tree[node][1])

  return max(l_height,r_height)+1


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


tree = defaultdict(lambda:[None,None])

root = inputs[0]

for value in inputs[1:]:

  insert(tree,root,value)

print(height(tree,root))

    

# STDIN

# 3 1 2 4 5


# Output:


# 3

賓果遊戲

 def cp(r,c):

  if d[r][c] >0:

    d[r][c] = -d[r][c] 

    h =[0]*5

    for i in range(5):

      cn = 0

      for j in range(5):

        if d[i][j]<0:cn+=1

      if cn==5:h[i]=1

    

    v =[0]*5

    for i in range(5):

      cn = 0

      for j in range(5):

        if d[j][i]<0:cn+=1

      if cn==5:v[i]=1

    

    x =[0]*2

    cn = 0

    for i in range(5):

      for j in range(5):

        if i==j and d[i][j]<0:cn+=1

      if cn==5:x[0]=1

    cn = 0

    for i in range(5):

      for j in range(5):

        if 4==i+j and d[i][j]<0:cn+=1

      if cn==5:x[1]=1

    d[r][c] = -d[r][c] 

    return sum(h)+sum(v)+sum(x)

  else:

    return 0

  


d = []

for i in range(5):

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

  d.append(dd)

# print(d)



dd = int(input())

while dd!=-1:

  for i in range(5):

    for j in range(5):

      if d[i][j]==dd:

        d[i][j]=-d[i][j]

  dd = int(input())


# for i in d:

#   for j in i:

#     print(j,end='\t')

#   print()

  

q =[ [0]*5 for _ in range(5)]

mr,mc = 0,0

mq = 0

for r in range(5):

  for c in range(5):

    q[r][c]=cp(r,c)

    if q[r][c]>mq:

      mq = q[r][c]

      mr,mc =r,c

    if q[r][c]==mq and d[r][c]<d[mr][mc]:

      mq = q[r][c]

      mr,mc =r,c

     

# for i in q:

#   print(i)

# print(mr,mc)

print(d[mr][mc])


# Output:

# 6

2024年12月18日 星期三

跑長編碼資料壓縮

 n = int(input())

for j in range(n):

  d = input()

  check = [i for i in d if i not in '01']

  if check:

    print(-1)

    continue

  

  pre = '2'

  c = 0

  oc = 0

  isfirst = True

  for i in d:

    if i!=pre:

      if isfirst:

        isfirst = False

      else:

        if c>7:

          print(f'{bin(7)[2:]:>03}',end=' ')

          oc +=4

          c = c - 7

          i=str(1-int(i))

          print(i,end='')

          i=str(1-int(i))

          

          print(f'{bin(c)[2:]:>03}',end=' ')

          oc +=4

          c = 0

        else:

          print(f'{bin(c)[2:]:>03}',end=' ')

          oc +=4

      print(i,end='')

      c = 1

    else:

      c+=1

    pre = i

  

  print(f'{bin(c)[2:]:>03}',end=' ')

  oc +=4

  

  print(f'{int(oc/len(d)*100+0.5)}%')

  

# STDIN

# 4

# 00010000000111111101111111

# 11111100000000000000111111111111110000

# 0011  1111100000101010

# HINET0800000123


# Output:


# 0011 1001 0111 1111 0001 1111 92%

# 1110 0111 0111 1111 1111 0100 63%

# -1

# -1

99 Game

 d = []

for i in range(4):  # 輸入四位玩家的牌組

    d.append(input().split()[1:])


rFlag = False  # 遊戲結束旗標

s = [0, 0, 0, 0]  # 各玩家累加數字

direction = 1  # 出牌方向 (1: 正向, -1: 反向)

j = 0  # 玩家索引


while not rFlag:  # 主遊戲迴圈

    if len(d[j]) == 0:  # 若玩家手牌耗盡

        print('ABCD'[j])  # 該玩家為贏家

        print(s[j])  # 輸出累加數字

        break


    card = d[j].pop(0)  # 取出玩家當前要出的牌


    # 處理卡牌效果

    if card not in ['A', '4', '5', '10', 'J', 'Q', 'K']:

        s[j] += int(card)

    elif card == 'A':  # 歸零

        s[j] = 0

    elif card == 'K':  # 設為 99

        s[j] = 99

    elif card == '4':  # 改變出牌方向

        direction *= -1

    elif card == '10':  # 加減 10

        s[j] += 10 if s[j] + 10 <= 99 else -10

    elif card == 'Q':  # 加減 20

        s[j] += 20 if s[j] + 20 <= 99 else -20


    # 檢查遊戲是否結束

    if s[j] > 99:  # 若累加數字超過 99

        print('ABCD'[j])  # 該玩家為輸家

        print(len(d[j]))  # 輸出剩餘手牌數

        break


    # 計算下一位玩家索引

    j = (j + direction + 4) % 4


# STDIN

# A 8 9 K 7 5 J Q 2 A 10 4 J 5

# B 2 3 J 5 7 4 4 10 7 9 8 J A

# C 7 3 4 A 9 10 9 6 8 K 10 Q 2

# D 6 3 Q 9 8 3 5 K Q K 2 A 10


# Output:


# A

# 9

2024年12月13日 星期五

dfs sample

 def dfs(graph,cur,vd):

  if cur not in vd:

    vd.append(cur)

    for nbr in graph[cur]:

      dfs(graph,nbr,vd)

  return vd


graph = [[],[2,3],[4,5],[6,7],[8,],[9,],[],[],[],[]]

vd = []

print(dfs(graph,1,vd))


Output:

[1, 2, 4, 8, 5, 9, 3, 6, 7]     


2024年12月11日 星期三

資料分析簡例 II

 d = '''

visual basic 2008 (157) 訊息分享 (111) Visual Studio 2008 (64) wpf (37) 每日一句 (35) cpp (30) python (26) Silverlight (22) C++ (18) Network Security (15) 全國技藝競竇 (14) 好文分享 (11) .Net (10) Blogger (10) 名詞解釋 (10) 研討會 (10) Excel (9) 書籍介紹 (9) 每日一小品 (9) 電腦黑白講 (8) Visual Studio 201X (7) 分享 (7) 網頁設計 (7) CSS (5) Algorithm (4) Network (3) PHP (3) Access (2) SA (2) VB.Net (2) VBA (2) WireShark (2) Word (2) php html (2) 其他好東東 (2) 分類整理 (2) Asp.Net (1) Batch (1) Blockly (1) IT News (1) OpenAI (1) SE (1) W7 (1) Writer (1) app inv2 (1) dfs (1) vex vr sample (1) 影像處理練習 (1) 黑白講 (1)

  2024 (143)

►  2023 (21)

►  2022 (49)

►  2021 (6)

►  2020 (7)

►  2019 (24)

►  2018 (17)

'''


d = d.strip()

d1 = []

for i in d.split():

  if i[0]=='(' and i[-1]==')':

    d1.append(int(i[1:-1]))

print(sum(d1))

    

# Output:


# 949

資料分析簡例

 d = '''

visual basic 2008 (157) 訊息分享 (111) Visual Studio 2008 (64) wpf (37) 每日一句 (35) cpp (30) python (26) Silverlight (22) C++ (18) Network Security (15) 全國技藝競竇 (14) 好文分享 (11) .Net (10) Blogger (10) 名詞解釋 (10) 研討會 (10) Excel (9) 書籍介紹 (9) 每日一小品 (9) 電腦黑白講 (8) Visual Studio 201X (7) 分享 (7) 網頁設計 (7) CSS (5) Algorithm (4) Network (3) PHP (3) Access (2) SA (2) VB.Net (2) VBA (2) WireShark (2) Word (2) php html (2) 其他好東東 (2) 分類整理 (2) Asp.Net (1) Batch (1) Blockly (1) IT News (1) OpenAI (1) SE (1) W7 (1) Writer (1) app inv2 (1) dfs (1) vex vr sample (1) 影像處理練習 (1) 黑白講 (1)

'''

# ans1:

# d = d.strip().split('(')

# d = [i.split(')') for i in d]

# dd = []

# for i in d:

#   for j in i:

#     dd.append(j)

# dd = [int(i) for i in dd if (i >='0' and i<='9')]

# # print(dd)

# print(sum(dd))



# ans2

d = d.strip()

d1 = []

for i in d.split():

  if i[0]=='(' and i[-1]==')':

    d1.append(int(i[1:-1]))

print(sum(d1))

    

# Output:


# 682