CS 101 Unit 2 Notes-Updated
CS 101 Unit 2 Notes-Updated
Udacity CS101: Building a Search Engine Unit 2: How to repeat Finding all the links on a page
Procedures and Control (Introduction to Web Browsers) Procedural Abstraction (Motivating Procedures) Procedures (Introducing Procedures) Q-1: Quiz (Procedure Code) Q-2: Quiz (Output) Return Statement (Return Statement) Q-3: Quiz (Return Statment) Using Procedures (Using Procedures) Q-4: Quiz (Inc Procedure) Q-5: Quiz (Sum Procedure) Q-6: Quiz (Sum Procedure with a Return Statement) Q-7: Quiz (Square) Q-8: Quiz (Sum of Three) Q-9: Quiz (Abbaize) Q-10: Quiz (Find Second) Making Decisions (Equality Comparisons) Comparison Operators Q-11: Quiz (Equality Comparisons) If Statements (If Statements) Q-12: Quiz (If Statements) Else Expressions (Is Friend) Q-13: Quiz (Is Friend) Q-14: Quiz (More Friends) Or Expressions (Or) Q-15: Quiz (Biggest) Alan Turing# (Biggest) While Loops (While Loops) Q-16: Quiz (While Loops) Q-17: Quiz (While Loops-2) Q-18: Quiz (Print Numbers) Baby Blocks (Factoral) Q-19: Quiz (Factorial) Break (Break) Q-20: Quiz (Break) Multiple Assignment (Multiple Assignment) Q-21: Quiz (Multiple Assignments) No Links (No Links) Q-22: Quiz (No Links) Print All Links (Print All links) Q-23: Quiz (Print All Links) Answer Key 1 page 1
page = page[end_quote:] start_link = page.find('<a href=') start_quote = page.find('"', start_link) end_quote = page.find('"', start_quote + 1) url = page[start_quote + 1:end_quote] print url page = page[end_quote:] start_link = page.find('<a href=') start_quote = page.find('"', start_link) end_quote = page.find('"', start_quote + 1) url = page[start_quote + 1:end_quote] print url This code will print out the next two links on the web page. Clearly, this is tedious work. The reason we have computers is to avoid having to do tedious, mechanical work! In addition to being tedious, repeating the same code over and over again like this wont work well because some pages only have a few links while other pages will have more links than the number of repetitions. In this unit, you will learn three important programming constructs: procedures, if statements, and while loops. Procedures, also known in Python as functions, enable you to abstract code from its inputs; if statements allow you to write code that executes differently depending on the data; and while loops provide a convenient way to repeat the same operations many times. You will combine these to solve the problem of finding all of the links on a web page.
2 page 2
Answer to Q-1
Answer to Q-2
4 page 4
5 page 5
Procedures are a very important concept and the core of programming is breaking problems into procedures, and implementing those procedures.
Answer to Q-4
6 page 6
Nothing Takes two numbers as its inputs, and outputs their sum Takes two strings as its inputs, and outputs the concatenation of the two strings Takes two numbers as its inputs, and changes the value of the first input to be the sum of the two number
Answer to Q-5
Answer to Q-8
Comparison Operators
Python provides several operators for making comparisons: < > <= == != less than greater than less than or equal to equal to not equal to
All of these operators act on numbers, for example: <number> <operator> <number> The output of a comparison is a Boolean: True or False. Here are some examples: print 2 < 3
8 page 8
Answer to Q-11
10 page 10
Or Expressions (Or)
An or expression gives the logical or (disjunction) of two operands. If the first expression evaluates to True, the value is True and the second expression is not evaluated. If the value of the first expression evaluates to False then the value of the or is the value of the second expression. <Expression> or <Expression> Here are a few examples: print True print True print True print False True or False False or True True or True False or False
An important difference between an or expression and other operators is that an or expression does not necessarily evaluate both of its operand expressions. For example: print True or this_is_an_error True Even though this_is_an_error would produce an error because the variable is not defined, the or expression does not produce an error! This is because the second operand expression of an or is only evaluated if the first expression evaluates to False. When the first operand expression evaluates to True, the output of the or expression must be True regardless of the value of the second operand expression. The Python rules of evaluation require that the second operand expression is not even evaluated in cases where the value of the first operand is True.
11 page 11
12 page 12
With the start of World War II, Turing joined the highly secret British effort to break Nazi codes at Bletchley Park. Turing was instrumental in breaking the Enigma code which was used by the Nazi's to communicate with field units and submarines. Turing designed an electro-mechanical machine known as a bombe for efficiently searching possible keys to decrypt Enigma-encrypted messages. The machines used logical operations to search the possible rotor settings on the Enigma to find the settings that were most likely to have generated an intercepted encrypted message. Bletchley Park was able to break thousands of Enigma messages during the war. The Allies used the knowledge gained from them to avoid Nazi submarines and gain a tremendous tactical advantage. After the war, Turing continued to make both practical and theoretical contributions to computer science. Among other things, he worked on designing general-purpose computing machines and published a paper speculating on the ability of computers to exhibit intelligence. Turing introduced a test for machine intelligence (now known as the Turing Test and the inspiration behind the annoying CAPTCHA images that challenge you to prove you are a human before submitting a web form) based on a machines ability to impersonate a human and speculated that machines would be able to pass the test within 50 years (that is, by the year 2000). Turing also studied morphogenesis (how biological systems grow) including why Fibonacci numbers (to come in Unit 6) appear so often in plants. In 1952, Turing's house was broken into, and Turing reported the crime to the police. The investigation revealed that Turing was a homosexual, which at the time was considered a crime in Britain. Turing did not attempt to hide his homosexuality. He was convicted and given a choice between serving time in prison and taking hormone treatments. He accepted the treatments, and his security clearance was revoked. In 1954, at the age of 41, Turing was found dead in an apparent suicide, with a cynide-laced partially-eaten apple next to him. The codebreaking effort at Bletchley Park was kept secret for many years after the war (Turing's report on Enigma was not declassified until 1996), so Turing never received public recognition for his contributions to the war effort. In September 2009, instigated by an on-line petition, British Prime Minister Gordon Brown issued an apology for how the British government treated Alan Turing.
13 page 13
Answer to Q-16
14 page 14
Answer to Q-17
Think about the babys choices for the first block, she has four. Lets say she reaches for the red block first. When she reaches for the second block she is down to just three choices. If she stacks the green block on top of the red block, she has two choices left, the blue block and the purple block. Next, the baby picks the purple one. Therefore, for her fourth block, the baby only has one choice, the blue one. To figure out the total number of choice we want to multiply the number of choices for the first block by the number of choices for the second block, by the number of choices for the third block, all the way to the fourth block. The function that you are computing is factorial. For any input n, you compute the factorial, which is the number of ways of arranging n items.
15 page 15
Break (Break)
Break gives us a way to break out of a loop, even if the test condition is true. The typical structure of the loop with a break looks like this: while <TestExpression>: <Code> if <BreakTest>: break # stop executing the while loop <More Code> <After While> The break statement jumps out of the loop to <After While>. Here is an example showing how we could rewrite print_numbers using break: def print_numbers(n): i = 1 while True: if i > n: break print i i = i + 1 This has the same behavior as the previous implementation, except now the test condition for the while loop is True. This is a bad example: if there is a way to write a loop without using break, it is usually best to avoid it. We will see soon an example where it is more difficult to write the loop without using break, since it is not clear before executing part of the block if the loop should continue repeating.
16 page 16
17 page 17
18 page 18
s, t = t, s a. b. c. d. Nothing Makes s and t both refer to the original value of t Swaps the values of s and t Error
Answer to Q-21
19 page 19
Answer to Q-22
20 page 20
21 page 21
22 page 22
Answer Key
A-1: Answer c. a string giving contents of the rest of the web page. One way to see this is to look at the code you are trying to replace, and identify the values that must be known before running the code. In this case, the value of page must be known before this code is executed since it is used on the right side of an assignment statement before it is defined. A-2: Answer To determine the outputs, we need to think about what is needed after the procedure. Anything computed by the procedure that we want to use after the procedure finishes must be an output. To answer this question, look at the code after the procedure. print url page = page[end_quote:] Since we already know the value of page, as indicated by the fact that it is known before the procedure was called, then the best answer is c. The reason we want end_quote as an output is because knowing where the end of the quote is allows us to advance the page so that the next time we look for a link target, we wont find the same one. Instead, we assign a new value to page that is made up of the subsequent characters in the current value of page starting from the end_quote to skip over the link we just found. A-3: Answer We are looking to return two things, the value of url and the value of end_quote. Do this by just returning those two values: return url, end_quote In this example, the input to the procedure is a single string, and its outputs are a string (url) and a number (end_quote). The inputs and outputs of procedures can be anything you want, and nearly all the work in computing is done by passing inputs to procedures, and then using their outputs as the inputs to other procedures. For example, procedures in a self-driving cars use data sensed by laser range finders, cameras, and pressure sensors as inputs, and produce outputs that control the steering and brakes on the car. A-4: Answer b. there is one input with an output of the input value plus one A-5: Answer Nothing!
23 page 23
24 page 24
y = square(x) print square (y) 1874161 The last example is the same as: x = 37 print square(square(x)) 1874161 This is an example of procedure composition. We compose procedures by using the outputs of one procedure as the inputs of the next procedure. In this case, we use the output of square(x)as the next input to square. Connecting procedures using composition is a very powerful idea. Most of the work of programs is done by composing procedures. A-8: Answer def sum3 (a, b, c): return a + b + c A-9: Answer def abbaize(a, b) return a + b + b + a print abbaize('dog', 'cat') dogcatcatdog A-10: Answer def find_second(search, target): first = search.find(target) second = search.find(target, first + 1) return second You could eliminate the variable second: def find_second(search, target): first = search.find(target) return search.find(target, first + 1) You could even reduce this to one line by eliminating the variable first: def find_second(search, target): return search.find(target, search.find(target) + 1) A-11: Answer The correct answer is d. The meaning of = (assignment) and == (equality comparison) are very different:
25 page 25
i = 21 i == 21 A-12: Answer
def bigger(a, b): if a > b: return a return b A-13: Answer def is_friend(name): if name [0] == 'D' return True else: return False There is no real need to use if statement here, since we can just return the result of the comparison directly: def is_friend(name): return name [0] == 'D' A-14: Answer def is_friend(name): if name[0] == 'D': return True if name [0] == 'N': return True return False Another way to define this would be to use an else clause: def is_friend(name): if name[0] == 'D': return True else: if name [0] == 'N': return True else: return False Note how the inner if statement is intended inside the else clause. A third way of writing this would be to use an or expression, which we will describe next: def is_friend(name): if name[0] == 'D' or name[0] == 'N'
26 page 26
bigger The code below is a much shorter way of defining biggest, taking advantage of the earlier definition of bigger: def biggest(a, b, c): return bigger(bigger(a, b), c) An even simpler way to define bigger would be to use Pythons built-in max operator, which works on any number of inputs and outputs the maximum value. We could then define:
27 page 27
28 page 28
29 page 29
30 page 30