Python-計算機科學導論(Udacity) 第2課:怎樣重複(update:2017/1/22)
一、第2課:怎樣重複
1.1 練習:有返回語句的Sum過程
1.2 練習:Abbasizeabba迴文小程序
# Define a procedure, abbaize, that takes# two strings as its inputs, and returns# a string that is the first input,# followed by two repetitions of the second input,# followed by the first input.# 正確的代碼def abbaize(a, b): con = str(a) + str(b) + str(b) + str(a) return con# 測試結果:#print abbaize(a,b)#>>> abba#print abbaize(dog,cat)#>>> dogcatcatdog
- 注意str()的使用,變數中存在邏輯變數TRUE和FALSE。如果不用,將報錯。
1.3 練習:找出第二個
# Define a procedure, find_second, that takes# two strings as its inputs: a search string# and a target string. It should return a# number that is the position of the second# occurrence of the target string in the# search string.# 自己的代碼def find_second(a, b): first_target = a.find(str(b)) second_target = a.find(str(b), first_target+1) return second_target # 測試結果:#danton = "De laudace, encore de laudace, toujours de laudace"#print find_second(danton, audace)#>>> 25#twister = "she sells seashells by the seashore"#print find_second(twister,she)#>>> 13
# Define a procedure, is_friend, that# takes a string as its input, and# returns a Boolean indicating if# the input string is the name of# a friend. Assume I am friends with# everyone whose name starts with D# and no one else. You do not need to# check for the lower case d#報錯:IndentationError: expected an indented blockdef is_friend(name): if name[0]==D: return True else: return False#正確的代碼def is_friend(name): if name[0]==D: return True else: return False#測試結果:#print is_friend(Diane)#>>> True#print is_friend(Fred)#>>> False
# Define a procedure, is_friend, that takes# a string as its input, and returns a# Boolean indicating if the input string# is the name of a friend. Assume# I am friends with everyone whose name# starts with either D or N, but no one# else. You do not need to check for# lower case d or n#錯誤的代碼def is_friend(name): if name[0] == D or N: return True else: return False#正確的代碼def is_friend(name): if name[0] == D or name[0] == N: return True else: return False#答案的代碼1def is_friend(name): if name[0] == D: return True if name[0] == N: return True else: return False#答案的代碼2def is_friend(name): if name[0] == D: return True else: if name[0] == N: return True else: return False測試結果:#print is_friend(Diane)#>>> True#print is_friend(Ned)#>>> True#print is_friend(Moe)#>>> False
# Define a procedure, biggest, that takes three# numbers as inputs and returns the largest of# those three numbers.#自己的代碼def biggest(a, b, c): if a > b and a > c: return a else: if b > c: return b else: return c#答案的代碼def biggest(a, b, c): if a > b: if a > c: return a else: return c else: if b > c: return b#測試結果:#print biggest(3, 6, 9)#>>> 9#print biggest(6, 9, 3)#>>> 9#print biggest(9, 3, 6)#>>> 9#print biggest(3, 3, 9)#>>> 9#print biggest(9, 3, 9)#>>> 9
# Define a procedure, print_numbers, that takes# as input a positive whole number, and prints # out all the whole numbers from 1 to the input# number.# Make sure your procedure prints "upwards", so# from 1 up to the input number.## 錯誤的代碼def print_numbers(n): i = 1 while i < 4: print i i = i + 1#測試結果:#print print_numbers(3)#>>>1#>>>2#>>>3#>>>None## 正確的代碼def print_numbers(n): i = 1 while i <= n: print i i = i + 1#print print_numbers(3)#print_numbers(3)#>>> 1#>>> 2#>>> 3
# Define a procedure, factorial, that# takes one number as its input# and returns the factorial of# that number.# 自己的代碼def factorial(n): i = 1 result = 1 while i <= n: result = result * i i = i + 1 return result#測試結果:#print factorial(4)#>>> 24#print factorial(5)#>>> 120#print factorial(6)#>>> 720
# Modify the get_next_target procedure so that# if there is a link it behaves as before, but# if there is no link tag in the input string,# it returns None, 0.# Note that None is not a string and so should# not be enclosed in quotes.# Also note that your answer will appear in# parentheses if you print it.def get_next_target(page): start_link = page.find(<a href=) #自己的代碼 if start_link == -1: return None, 0 else: start_quote = page.find(", start_link) end_quote = page.find(", start_quote + 1) url = page[start_quote + 1:end_quote] return url, end_quote
二、第2課:習題集
2.1 練習:Udacify
# Define a procedure, udacify, that takes as# input a string, and returns a string that# is an uppercase U followed by the input string.# for example, when you enter# print udacify(dacians)# the output should be the string Udacians# 自己的代碼def udacify(name): U_name = U + name return U_name# Remove the hash, #, from infront of print to test your code.#測試結果:#print udacify(dacians)#>>> Udacians#print udacify(turn)#>>> Uturn#print udacify(boat)#>>> Uboat
2.2 練習:Proc
下列選項中,何者與程序proc的作用相同
2.3 ☆練習:中位數# Define a procedure, median, that takes three# numbers as its inputs, and returns the median# of the three numbers.# Make sure your procedure has a return statement.def bigger(a,b): if a > b: return a else: return bdef biggest(a,b,c): return bigger(a,bigger(b,c))#自己的代碼def median(a,b,c): if a == biggest(a,b,c): return bigger(b,c) if b == biggest(a,b,c): return bigger(a,c) if c == biggest(a,b,c): return bigger(a,b)#答案的代碼def median(a,b,c): big = biggest(a,b,c) if big == a: return bigger(b,c) if big == b: return bigger(a,c) else: return bigger(a,b)#測試結果:#print(median(1,2,3))#>>> 2#print(median(9,3,6))#>>> 6#print(median(7,8,7))#>>> 7
# Define a procedure, countdown, that takes a# positive whole number as its input, and prints# out a countdown from that number to 1,# followed by Blastoff!# The procedure should not return anything.# For this question, you just need to call # the procedure using the line# countdown(3)# instead of print countdown(3).# 自己的代碼def countdown(n): while n > 0: print n n = n - 1 print Blastoff!#測試結果:#countdown(3)#>>> 3#>>> 2#>>> 1#>>> Blastoff!
# Define a procedure, find_last, that takes as input# two strings, a search string and a target string,# and returns the last position in the search string# where the target string appears, or -1 if there# are no occurrences.## Example: find_last(aaaa, a) returns 3# Make sure your procedure has a return statement.# 沒想出來,答案的代碼def find_last(s,t): last_pos = -1 while True: pos = s.find(t,last_pos+1) if pos == -1: return last_pos last_pos = pos #測試結果: #print find_last(aaaa, a)#>>> 3#print find_last(aaaaa, aa)#>>> 3#print find_last(aaaa, b)#>>> -1#print find_last("111111111", "1")#>>> 8#print find_last("222222222", "")#>>> 9#print find_last("", "3")#>>> -1#print find_last("", "")#>>> 0
2.6 ☆☆練習:乘法表
# 2 GOLD STARS# Define a procedure, print_multiplication_table,# that takes as input a positive whole number, and prints out a multiplication,# table showing all the whole number multiplications up to and including the# input number. The order in which the equations are printed matters.# Hint: You can not add an integer and a string, but in homework 1.9# you came across a method, str, for turning an integer into a string.# 自己的錯誤代碼def print_multiplication_table(n): i = 1 j = 1 while i <= n: while j <= n: k = i * j print str(i)+ * +str(j)+ = +str(k) j = j + 1 i = i + 1# 答案的正確代碼def print_multiplication_table(n): i = 1 while i <= n: j = 1 while j <= n: print str(i)+ * +str(j)+ = +str(i*j) j = j + 1 i = i + 1#測試結果:#print_multiplication_table(2)#>>> 1 * 1 = 1#>>> 1 * 2 = 2#>>> 2 * 1 = 2#>>> 2 * 2 = 4#print_multiplication_table(3)#>>> 1 * 1 = 1#>>> 1 * 2 = 2#>>> 1 * 3 = 3#>>> 2 * 1 = 2#>>> 2 * 2 = 4#>>> 2 * 3 = 6#>>> 3 * 1 = 3#>>> 3 * 2 = 6#>>> 3 * 3 = 9
三、第2課:習題集(可選)
3.1 練習:周末先看自己錯誤的代碼# Define a procedure weekend which takes a string as its input, and# returns the boolean True if its Saturday or Sunday and False otherwise.# wrong eodedef weekend(day): if day == Saturday: print True else: if day == Sunday: print True else: print False#測試結果:#print weekend(Monday)#>>> False#>>> None#print weekend(Saturday)#>>> True#>>> None#print weekend(July)#>>> False#>>> None
- 輸出的結果都有"None",是由print weekend(Monday)造成的。
- 單獨輸入weekend(Monday),結果是"False",沒有"None"。
- 正確操作是用"return"而不是用"print"
正確的代碼來一發
# Define a procedure weekend which takes a string as its input, and# returns the boolean True if its Saturday or Sunday and False otherwise.# 正確的代碼def weekend(day): if day == Saturday: return True else: if day == Sunday: return True else: return False# 測試結果:#print weekend(Monday)#>>> False#print weekend(Saturday)#>>> True#print weekend(July)#>>> False
# Define a procedure, stamps, which takes as its input a positive integer in# pence and returns the number of 5p, 2p and 1p stamps (p is pence) required # to make up that value. The return value should be a tuple of three numbers # (that is, your return statement should be followed by the number of 5p,# the number of 2p, and the nuber of 1p stamps).## Your answer should use as few total stamps as possible by first using as # many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as # needed to make up the total.## (No fair for USians to just say use a "Forever" stamp and be done with it!)##代碼def stamps(n): i = int(n / 5) # remainder = n % 5 # j = int(remainder / 2) k = remainder % 2 return ( + str(i) + , + str(j) + , + str(k) + )#測試結果:#print stamps(8)#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp#print stamps(5)#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps#print stamps(29)#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps#print stamps(0)#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps#print stamps(50)#>>> (10, 0, 0)
- 這有點懵,不懂錯哪兒了,按照提示測試沒有問題。
# The range of a set of values is the maximum value minus the minimum# value. Define a procedure, set_range, which returns the range of three input# values.# Hint: the procedure, biggest which you coded in this unit# might help you with this question. You might also like to find a way to# code it using some built-in functions.# 自己的代碼def bigger(a, b): if a > b: return a else: return bdef biggest(a, b, c): return bigger(a, bigger(b, c))def smaller(a, b): if a > b: return b else: return a def smallest(a, b, c): return smaller(a, smaller(b, c)) def set_range(i, j ,k): max = biggest(i, j, k) min = smallest(i, j, k) return max - min#測試結果 #print set_range(10, 4, 7)#>>> 6 # since 10 - 4 = 6#print set_range(1.1, 7.4, 18.7)#>>> 17.6 # since 18.7 - 1.1 = 17.6
推薦閱讀:
※類可以是另一個類的對象嗎?
※python2.7,python3.3,對於小白,到底從哪個版本入手比較好?
※Python 中為何沒有數字到字元串的自動轉換?
※Python 哪些可以代替遞歸的演算法?
TAG:Python入门 | Python | 优达学城Udacity |