MIT6 0001F16 String Manipulation, Guess-and-Check .

2y ago
15 Views
3 Downloads
582.83 KB
21 Pages
Last View : 16d ago
Last Download : 3m ago
Upload by : Mara Blakely
Transcription

STRING ION(download slides and .py files ĂŶĚ follow along!)6.0001 LECTURE 36.0001 LECTURE 31

LAST TIME strings branching – if/elif/else while loops for loops6.0001 LECTURE 32

TODAY string manipulation guess and check algorithms approximate solutions bisection method6.0001 LECTURE 33

STRINGS think of as a sequence of case sensitive characters can compare strings with , , etc. len() is a function used to retrieve the length of thestring in the parenthesess "abc"len(s) evaluates to 36.0001 LECTURE 34

STRINGS square brackets used to perform indexing into a stringto get the value at a certain index/positions "abc"index:index:0 1 2 indexing always starts at 0-3 -2 -1 last element always at index -1s[0]s[1]s[2]s[3]s[-1]s[-2]s[-3] evaluates to "a"evaluates to "b"evaluates to "c"trying to index out of bounds, errorevaluates to "c"evaluates to "b"evaluates to "a"6.0001 LECTURE 35

STRINGS can slice strings using [start:stop:step] if give two numbers, [start:stop], step 1 by default you can also omit numbers and leave just colonss "abcdefgh"s[3:6] evaluates to "def", same as s[3:6:1]s[3:6:2] evaluates to "df"s[::] evaluates to "abcdefgh", same as s[0:len(s):1]s[::-1] evaluates to "hgfedbca", same as s[-1:-(len(s) 1):-1]s[4:1:-2] evaluates to "ec"6.0001 LECTURE 36

STRINGS strings are “immutable” – cannot be modifieds "hello"s[0] 'y's 'y' s[1:len(s)] gives an error is allowed,s bound to new object"hello""yello"s6.0001 LECTURE 37

for LOOPS RECAP for loops have a loop variable that iterates over a set ofvaluesfor var in range(4): expressions var iterates over values 0,1,2,3 expressions inside loop executedwith each value for varfor var in range(4,6): var iterates over values 4,5 expressions range is a way to iterate over numbers, but a for loopvariable can iterate over any set of values, not just numbers!6.0001 LECTURE 38

STRINGS AND LOOPS these two code snippets do the same thing bottom one is more “pythonic”s "abcdefgh"for index in range(len(s)):if s[index] 'i' or s[index] 'u':print("There is an i or u")for char in s:if char 'i' or char 'u':print("There is an i or u")6.0001 LECTURE 39

CODE EXAMPLE:ROBOT CHEERLEADERSan letters "aefhilmnorsxAEFHILMNORSX"word input("I will cheer for you! Enter a word: ")times int(input("Enthusiasm level (1-10): "))i 0for char in word:while i len(word):char word[i]if char in an letters:print("Give me an " char "! " char)else:print("Give me a " char "! " char)i 1print("What does that spell?")for i in range(times):print(word, "!!!")6.0001 LECTURE 310

EXERCISEs1 "mit u rock"s2 "i rule mit"if len(s1) len(s2):for char1 in s1:for char2 in s2:if char1 char2:print("common letter")break6.0001 LECTURE 311

GUESS-AND-CHECK the process below also called exhaustive enumeration given a problem you are able to guess a value for solution you are able to check if the solution is correct keep guessing until find solution or guessed all values6.0001 LECTURE 312

GUESS-AND-CHECK– cube rootcube 8for guess in range(cube 1):if guess**3 cube:print("Cube root of", cube, "is", guess)6.0001 LECTURE 313

GUESS-AND-CHECK– cube rootcube 8for guess in range(abs(cube) 1):if guess**3 abs(cube):breakif guess**3 ! abs(cube):print(cube, 'is not a perfect cube')else:if cube 0:guess -guessprint('Cube root of ' str(cube) ' is ' str(guess))6.0001 LECTURE 314

APPROXIMATE SOLUTIONS good enough solution start with a guess and increment by some small value keep guessing if guess3-cube epsilonfor some small epsilon decreasing increment size slower program increasing epsilon less accurate answer6.0001 LECTURE 315

APPROXIMATE SOLUTION– cube rootcube 27epsilon 0.01guess 0.0increment 0.0001num guesses 0while abs(guess**3 - cube) epsilon: and guess cube :guess incrementnum guesses 1print('num guesses ', num guesses)if abs(guess**3 - cube) epsilon:print('Failed on cube root of', cube)else:print(guess, 'is close to the cube root of', cube)6.0001 LECTURE 316

BISECTION SEARCH half interval each iteration new guess is halfway in between to illustrate, let’s play a game!GUESSGUESSGUESS6.0001 LECTURE 317

BISECTION SEARCH– cube rootcube 27epsilon 0.01num guesses 0low 0high cubeguess (high low)/2.0while abs(guess**3 - cube) epsilon:if guess**3 cube :low guesselse:high guessguess (high low)/2.0num guesses 1print 'num guesses ', num guessesprint guess, 'is close to the cube root of', cube6.0001 LECTURE 318

BISECTION SEARCHCONVERGENCE search space first guess: second guess: kth guess:N/2N/4N/2k guess converges on the order of log2N steps bisection search works when value of function variesmonotonically with input code as shown only works for positive cubes 1 – why? challenges modify to work with negative cubes! modify to work with x 1!6.0001 LECTURE 319

x 1 if x 1, search space is 0 to x but cube root is greaterthan x and less than 1 modify the code to choose the search spacedepending on value of x6.0001 LECTURE 320

MIT OpenCourseWarehttps://ocw.mit.edu6.0001 Introduction to Computer Science and Programming in PythonFall 2016For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.

print(guess, 'is close to the cube root of', cube) and guess cube : 6.0001 LECTURE 3 16. BISECTION SEARCH half interval each iteration new guess is halfway in between to illustrate, let’s play a game! 6.0001 LECTURE 3 17 GUESS GUESS GUESS. BISECTION SEARCH –cu

Related Documents:

You can also tune your guitar to a keyboard or piano. The open strings of a guitar correspond to certain notes on a keyboard. SESSION 1 3 Starting Off Right Learn &Master Guitar E A D G B E B 6th string 5th string 4th string 3rd string 2nd string 1st string 5th Fret 1st string 6th string 5th string 4th string 3rd string 2nd string E A D GB E .

You can also tune your guitar to a keyboard or piano. The open strings of a guitar correspond to certain notes on a keyboard. SESSION 1 3 Starting Off Right Learn &Master Guitar E A D G B E B 6th string 5th string 4th string 3rd string 2nd string 1st string 5th Fret 1st string 6th string 5th string 4th string 3rd string 2nd string E A D GB E .

Barber, Samuel String Quartet No.1, Op.11 Bartok, Bela String Quartet No.2, Op.17 String Quartet No.4 Beethoven, Ludwig van String Quartet No.1 in F major, Op.18 No.1 String Quartet No.2 in G major, “Compliments” Op.18 No.2 String Quartet No.6 in B-flat major, Op.18 No.6 String Quartet No.7 in F major, “Rasumovsky 1” Op.59 No.1

String Quartet n. 15 op. 144 Anton Webern String Quartet op. 28 Five Movements for String Quartet Six Bagatelles for String Quartet Alexander Von Zemlinsky String Quartet n. 2 op. 15 2) Toshio Hosokawa UTA-ORI. Weaving Song for string quartet (2020) New composition for String Quartet

query string. Given a query string and a string tuple , the similarity score of and in this class of predicates is of the form weight of the token,where is the query-based in string and weight of the token is the tuple-based in string . 3.2.1 Tf-idf Cosine Similarity The tf-idf cosine similarity [24] between a query string and a string tuple

Alternatively, you can use the operator as follows: a a b; which is equivalent to: a "string A" " and string B"; and equivalent to: a "string A" " " "and string B"; where the middle string is a string with a single whitespace character. Comparing Strings Comparing string values in

3 string 4 string (double melody) 5 string (double melody and bass usually) 6 string (every course doubled). Doubling a string provides more volume for the notes sounded on that string compared to the notes on the other courses. Another string arrangement seen among more advanced players

STM32 MCUs listed in Table 1. Outsourcing of product manufacturing enables original equipment manufacturers (OEMs) to reduce their direct costs and concentrate on high added-value activities such as research and development, sales and marketing. However, contract manufacturing puts the OEM's proprietary assets at risk, and since the contract manufacturer (CM) manipulates the OEM's intellectual .