CS61A是美國伯克利分校給他們大一相關專業學生的一門必修課,可謂是神課
從開始學習到CS61A的第一週,我開始深刻感受到這門課和我們隨便在B站或者知乎上看到的視頻教程完全不同。以往的學習編程視頻教程,就是錄了一大堆然而都是一個一個語法進行介紹,系統學習好像也沒什麼不妥,但是看完發現好像並沒有學到什麼,而且還花費了很多的時間。然而,上CS61A給我的感受就是無處不動手,無處不實踐。
即使是我已經會python的基本語法知識,這門課的學習還是讓我覺得有些喫力,學的第二天就讓學生寫一個Hog(擲骰子)的小遊戲,國內基本沒有這樣的教學模式。相信只要能夠堅持上完這門課,對你的編程能力一定是質的飛躍。
課程的話可以在B站找到Spring2018版本的,不過是英文字幕,是時候逼逼自己練英語聽力了。這門課程還提供了作業網址,也不需要你翻牆就可以實現基本完整的學習。
下面是我在CS61A做的第一個工程 HOG:
涉及的知識點記錄一下:
高階函數:
之前從來沒見過這種騷操作,在函數裏面嵌套函數,再在外層函數返回構成閉包。
例子就拿我在工程中遇到的舉:
def announce_highest(who, previous_high = 0, previous_score = 0) :
"""Return a commentary function that announces when WHO's score
increases by more than ever before in the game.
>> > f0 = announce_highest(1) # Only announce Player 1 score gains
>> > f1 = f0(11, 0)
>> > f2 = f1(11, 1)
1 point!That's the biggest gain yet for Player 1
>> > f3 = f2(20, 1)
>> > f4 = f3(5, 20) # Player 1 gets 4 points, then Swine Swap applies
19 points!That's the biggest gain yet for Player 1
>> > f5 = f4(20, 40) # Player 0 gets 35 points, then Swine Swap applies
20 points!That's the biggest gain yet for Player 1
>> > f6 = f5(20, 55) # Player 1 gets 15 points; not enough for a new high
"""
assert who == 0 or who == 1, 'The who argument should indicate a player.'
# BEGIN PROBLEM 7
"*** YOUR CODE HERE ***"
def say(score0, score1) :
#判斷玩家
if not who :
score = score0
else:
score = score1
#記錄本次差值
sub = score - previous_score
if sub > previous_high:
if sub == 1 :
print(sub, "point! That's the biggest gain yet for Player", who)
else :
print(sub, "points! That's the biggest gain yet for Player", who)
return announce_highest(who, sub, score)
else:
return announce_highest(who, previous_high, score)
return say
# END PROBLEM 7
益智遊戲,可以玩一下
最近沒那麼多時間發博客了,先繼續刷這門課吧!
希望對讀者有所幫助,喜歡的話可以關注一下我的公衆號,我會把學習筆記發在上面,大家可以一起共同學習!