電腦科學和程式設計導論 week2 簡單演算法

2021-07-29 06:50:10 字數 3715 閱讀 1806

week2

簡單演算法

迭代:多次重複方法,一遍重複利用計算多次執行。

迴圈結構

逐次逼近法

二分演算法

當型迴圈

while bool值測試

:bool值為真,順序執行指令

迴圈外設定

迭代變數

,測試變數

,改變迴圈內變數

命令break在乙個迴圈中被執行,它會在這個位置停止計算這個迴圈,然後傳遞控制權到下乙個表示式

num = 0

while num <= 5:

print num

num += 1

print "outside of loop"

print num

0, 1, 2, 3, 4, 5, 'outside of loop', 6

注意看題

n = 0

s = 0

while end > 0:

end -= 1

n += 1

s += n

print s

_________________

total = 0

current = 1

while current <= end:

total += current

current += 1

print tota

abs( )絕對值

猜測和檢測:對有限可能的問題有效。

窮舉:從可能值域的一端開始,順序嘗試每個值。

for 識別符號

in 序列:

**塊

range(n) = [0,1,2,3,...,n-1]

range(m,n) = [m,m+1,m+2,...,n-1]

school = 'massachusetts institute of technology'

numvowels = 0

numcons = 0

for char in school:

if char == 'a' or char == 'e' or char == 'i' \

or char == 'o' or char == 'u':

numvowels += 1

elif char == 'o' or char == 'm':

print char

else:

numcons -= 1

print 'numvowels is: ' + str(numvowels)

print 'numcons is: ' + str(numcons)

注意空格

正負數

十進位制二進位制

比較x,y 利用絕對差

x = 23

epsilon = 0.01

step = 0.1

guess = 0.0

while abs(guess**2-x) >= epsilon:

if guess <= x:

guess += step

else:

break

if abs(guess**2 - x) >= epsilon:

print 'failed'

else:

print 'succeeded: ' + str(guess)

#x=23 情況下,

guess^2-x

永遠大於

0.01

_____________________________

x = 25

epsilon = 0.01

step = 0.1

guess = 0.0

while guess <= x:

if abs(guess**2 -x) >= epsilon:

guess += step

if abs(guess**2 - x) >= epsilon:

print 'failed'

else:

print 'succeeded: ' + str(guess)

沒有設定break while部分無法返回

二分查詢

最大值,最小值

減少運算次數

擁有排序屬性的資料

x = 

#待猜測

epsilon = 0.01

#判斷偏差

low = 0.0

#最小值

high = x

#最大值

ans = (high + low)/2.0

#猜測值

=(最小值

+最大值)

/2while abs(ans**2 - x) >= epsilon:

if ans**2 < x:

#猜測值

^2小於

xlow = ans

#最小值是猜測值

else:

high = ans

#最大值是猜測值

ans = (high + low)/2.0

#重新生成猜測值

print(str(ans) + ' is close to square root of ' + str(x))

———————————————————————

hige = 100

low = 0

num = (hige +low)/2

print "please think of a number between 0 and 100!"

while not false:

print "is your secret number ?",

print str(num)

print "enter 'h' to indicate the guess is too high.",

print "enter 'l' to indicate the guess is too low.",

print "enter 'c' to indicate i guessed correctly."

guess = raw_input(">")

if guess == 'c':

break

elif guess == 'h':

hige = num

elif guess == 'l':

low = num

else:

print "sorry, i did not understand your input."

num = (hige +low)/2

print "game over. your secret number was:" + str(num)

猜測數字,可以設定安全值

guseeed = false

while not guessed:

if guess == 『c

』:guessed = true

牛頓拉夫遜演算法

epsilon = 0.01

y = 

#猜測值

guess = y/2.0

while abs(guess*guess - y) >= epsilon:

guess = guess - (((guess**2) - y)/(2*guess))

print(guess)

print('square root of ' + str(y) + ' is about ' + str(guess))

電腦科學和程式設計導論 week2 作業

num 0 for x in s if x in aeiou num 1 print number of vowels str num 單詞出現次數 or只記一次,利用 in重複計算 num 0 i 0 for i in range len s if s i i 3 bob num 1 print ...

MIT 電腦科學和Python 程式設計導論

其實上一本書 笨辦法學python 嚴格意義上來說,算不上是入門書,因為它主要目的是讓你去找到敲 的感覺,基本不涉及計算機的內容。說到真正的入門,mit的 電腦科學和python程式設計導論 這門課程是個非常好的選擇。對於想要入門python的人來說,這門課程不會讓你失望,但是它能給你的不止於此,其...

電腦科學導論課後總結 2

1 圖靈機的等價機器 繼續上節課沒講完的內容,我知道了,除了圖靈機以外,人們還發明了很多其它的計算模型。包括 暫存器機 遞迴函式 演算 生命遊戲 馬爾可夫演算法。感悟 根據圖靈機的工作原理,可想圖靈機在日常生活中的應用之廣泛,特別是將圖靈機應用於人工智慧,將會取代不少勞動力,另一方面,假設在圖靈機的...