Try   HackMD

03/18 python 練習解答

暖身題(slice)

raw = list(map(int,input().split())) raw=raw[::-1] print(raw)

小練習1(list)

time= int(input()) for time in range(1, time+1): number = int(input()) walls = list(map(int, input().split())) low = 0 high = 0 current = -1 for i in range(number): if current >= 0: if walls[i] > current: high += 1 elif walls[i] < current: low += 1 current = walls[i] print("Case "+str(time)+':',high,low)

小練習2(二維list)

a=[] b=[] c = [[0 for i in range(2)] for j in range(3)] for i in range (3): row=list(map(int,input().split())) a.append(row) input() for i in range (3): row=list(map(int,input().split())) b.append(row) for i in range(3): for j in range(2): for n in range (3): c[i][j]+=a[i][n]*b[n][j] for i in range (3): for j in range(2): print(c[i][j],end=' ') print(' ')

矩陣乘法更快的方法(偷教)請不要用來解OJ

python 裡面其實有很多很酷的套件^^
下禮拜瑋嘉老師會詳細介紹~

import numpy as np #加入這個library~ a=[] b=[] c = [[0 for i in range(2)] for j in range(3)] for i in range (3): row=list(map(int,input().split())) a.append(row) input() for i in range (3): row=list(map(int,input().split())) b.append(row) #將a b list 轉成numpy~ a = np.array(a) b= np.array(b) #利用np.dot進行矩陣乘法 c= np.dot(a, b) for i in range (3): for j in range(2): print(c[i][j],end=' ') print(' ')

利用np.dot()進行矩陣乘法 酷吧

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

小練習3(tuple)

nums = list(map(int, input().split())) answer9=[] for i in range(len(nums)-1): for j in range(i+1,len(nums)): if nums[i]+nums[j] ==9: n=(nums[i],nums[j]) answer9.append(n) print(answer9)

小練習4(dictionary)

dict={1:["Big Mac",530], 2:["Fries(Regular)",330],3:["Cola(Regular)",210],4:["McNuggets(4 pieces)", 230]} number= list(dict.keys()) price = list(dict.values()) count_kcal=0 for i in range(4): print(str(number[i])+price[i][0]) print() while True: wan = int(input()) if wan == 0: break if wan > 4: print("please enter again") continue print("You'd ordered:",dict[wan][0]) p = dict[wan][1] count_kcal+=p print("Order received,total calories:",count_kcal,"kcal")