→ gaowei16: 推 04/19 01:26
推 hardy18: 推 04/22 01:51
推 kelly2643: 推 05/01 22:04
目前卡在hog project Problem 5 (3 pt)...
※ 編輯: Farrenhi (162.227.165.109 美國), 05/20/2023 00:59:37
who = 0 # Who is about to take a turn, 0 (first) or 1 (second)
# BEGIN PROBLEM 5
"*** YOUR CODE HERE ***"
while (score0 < goal) and (score1 < goal):
if who == 0:
score0 += take_turn(strategy0(score0, score1), score1, dice)
if extra_turn(score0, score1):
who = other(who)
else:
score1 += take_turn(strategy1(score1, score0), score0, dice)
if extra_turn(score1, score0):
who = other(who)
who = other(who)
# END PROBLEM 5
這邊就是要注意 一次只能用跑一次take_turn 這樣才會跑出來while迴圈去驗證是否
滿足goal
※ 編輯: Farrenhi (162.227.165.109 美國), 05/20/2023 01:22:04
推 gaowei16: 大大 你進度到哪了 06/28 16:20
中間荒廢了一陣子
目前結束了 Week3 0911 Lecture 7: decorator
回來做Hog project
Problem 4a: done
※ 編輯: Farrenhi (111.255.28.234 臺灣), 08/04/2023 15:09:01
lab02 有一題的答案很詭異 必須輸入關鍵字
>>> print_lambda = lambda z: print(z) # When is the return expression of a
lambda expression executed?
>>> print_lambda
______
>>> one_thousand = print_lambda(1000)
______
>>> one_thousand
nothing
nothing就是答案
※ 編輯: Farrenhi (111.255.28.234 臺灣), 08/04/2023 21:43:35
HOG
Problem 6 這題不容易 原來是要寫成這樣子 才會把n
遞迴繼承到下一個commentary function
say = say(score0, score1) # call "say", and then "say" becomes the new
commentary function
如果我只寫
say(score0, score1) 會發現只有部分cases能通過 到n + 1那個case就過不了
因為每次都重新讀一次say(...), n+1並沒有被丟到下一層去
※ 編輯: Farrenhi (111.255.56.140 臺灣), 08/09/2023 16:26:02
HOG
Problem 8
Question 8 > Suite 2 > Case 2
(cases remaining: 4)
>>> from hog import *
>>> dice = make_test_dice(3, 1, 5, 6)
這題有點小陷阱
如果沒有看仔細 會忽略了def roll_dice裡面的 pigout_count
因此會變成這個結果
4 11 4 11 總共1000個數去做平均
然而因為pigout_count
所以是
1 11 1 11 總共1000個數去平均
※ 編輯: Farrenhi (111.255.56.140 臺灣), 08/10/2023 11:38:19
HOG Problem11
OK這邊需要使用到 extra_turn()這個函數 注意放進去這個函數
的第一個argument 應該是當前分數
那當前分數是甚麼呢?
是score?
還是score + ?
因為擲出0就會有bacon score 所以要再加上這一項 才放進去extra_turn
if extra_turn(score + bacon_score, opponent_score):
return 0
※ 編輯: Farrenhi (111.255.40.96 臺灣), 08/11/2023 13:42:20
hw02: Recursion
Q2: Ping-pong
注意這一題 index + 1 這邊很特別 注意甚麼時候要扭轉
※ 編輯: Farrenhi (111.255.40.96 臺灣), 08/13/2023 20:53:09
※ 編輯: Farrenhi (111.255.33.15 臺灣), 09/05/2023 09:51:04
Cat project
problem 5
solution 1: 這個會通過
solution 2: 明明邏輯依樣卻不會通過...
solution 1:
def autocorrect(user_word, valid_words, diff_function, limit):
"""Returns the element of VALID_WORDS that has the smallest difference
from USER_WORD. Instead returns USER_WORD if that difference is greater
than LIMIT.
"""
# BEGIN PROBLEM 5
"*** YOUR CODE HERE ***"
for item in valid_words:
if item == user_word:
return item
min_valid_word = min(valid_words, key=lambda item:
diff_function(user_word, item, limit))
if diff_function(user_word, min_valid_word, limit) > limit:
return user_word
else:
return min_valid_word
# END PROBLEM 5
Solution 2:
def autocorrect(user_word, valid_words, diff_function, limit):
"""Returns the element of VALID_WORDS that has the smallest difference
from USER_WORD. Instead returns USER_WORD if that difference is greater
than LIMIT.
"""
# Initialize output and diff_value
output = user_word
diff_value = limit
for item in valid_words:
if item == user_word:
return item
current_diff = diff_function(user_word, item, limit)
if current_diff < diff_value:
diff_value = current_diff
output = item
return output
※ 編輯: Farrenhi (111.255.14.144 臺灣), 09/22/2023 23:11:22
CAT project
Problem 7:
def pawssible_patches(start, goal, limit):
"""A diff function that computes the edit distance from START to GOAL."""
# assert False, 'Remove this line'
if start == goal:
return 0
elif start == "" or goal == "":
return max(len(start), len(goal))
elif limit == 0:
return 10000
else:
if start[0] == goal[0]:
return pawssible_patches(start[1:], goal[1:], limit)
else:
add_diff = pawssible_patches(start[0:], goal[1:], limit - 1)
remove_diff = pawssible_patches(start[1:], goal[0:], limit - 1)
substitute_diff = pawssible_patches(start[1:], goal[1:], limit -
1)
return (min(add_diff, remove_diff, substitute_diff)) + 1
※ 編輯: Farrenhi (111.255.14.144 臺灣), 09/23/2023 19:09:01