Lec 4 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008
Lecture 4: Decomposition and abstraction through functions; introduction to recursion
Instructors: Prof. Eric Grimson, Prof. John Guttag
View the complete course at: ocw.mit.edu/6-00F08
License: Creative Commons BY-NC-SA
More information at ocw.mit.edu/terms
More courses at ocw.mit.edu
Пікірлер: 213
Thank you MIT for teaching the world. What an amazing time to live in when people all over the world with Internet Access can virtually go to MIT.
thank you to all from the professor to the cameramn to who ever edited the video to who ever upload it to who ever made the playlist
It's such a wonderful gift MIT is giving to the world of knowledge
Mr grimson, thank you for this, and all who provided this knowledge to the public I virtually almost completely finished computer science with MIT so thankful
@geinezhang7030
7 жыл бұрын
can i know how many month u have committed and how many courses u have taken?
This is really what I've been looking for. You are great Mr. Eric, Mr. John
This is such a great thing you guys are doing. Im at my senior capstone and these videos are still showing me things
merry christmas mit, enjoying this a ton :) each time i watch it returns more value. ans. :)
Lecture 1: 807 000 views Lecture 2: 190 000 views Lecture 3: 91 000 views Lecture 4: 76 000 views Lecture 5: 48 000 views
@percih70
6 жыл бұрын
and a thumbs up for the 0.01% who spotted that the fibonacci seed values are wrong.......
@abhinandandas6466
5 жыл бұрын
Yeah noticed that... I was about to comment the same
@94D33M
5 жыл бұрын
wow 4th video and already in the 10% imagine the last video ( although sometimes i do see last video also gets a little more views than the middle views ), like seriously, how do the majority watch these courses lol
@cjonwickham1933
4 жыл бұрын
Lectures ...professor and ta...dean ceo or president of college got richer with bs ripoff....student left 40k in debt no job.
@adiflorense1477
3 жыл бұрын
@@percih70 random.seed(0)
This is the 4th lecture I've heard. It's very interesting that decomposition is the process of breaking down a complex computer problem into a simpler one to fix. I'm doing this to help me get a computer job one day, since I like playing video games.
@mazterarius in object oriented programming languages, variables can also refer to objects. in java, for example, the value "null" is assigned to a variable to show that the variable doesnt refer to any object. this is essentially the same as "none". it means that the variable or function is not associated with any value.
this helps me to understand and practice code, GREAT HELP MIT
Cool. Thanks a lot. I have completed udacity cs101 and l'm watching this just for fun. Good stuff and good professor. I'm very enjoy.
Super nerds, athletically tossing candy. Best. Show. Ever.
How is he typing the code on one screen and running it immediately on the other? Is there something on Python 3.4.3 to do that? If I open a new file and do the code don't I have to import it on the other screen? If I do that, it gets all messed up. Just not sure how he's doing that.
0:00 Summary of previous lecture 3:00 Decomposition/Abstraction 4:20 Functions 6:20 A boring analogy about "abstraction" by Eric 7:33 Thanks god we are going on the lecture again (Functions) 16:06 Local Bindings 24:24 Using Functions (Farmyard Problem) 33:32 Eric throws somethings to students' heads and thinks it's funneh 😡😡 34:14 Warnings about List and Tuples by John 34:45 Expanding the Farmyard Problem (nested loops) 39:35 Recursion 42:42 Palindrome Function 47:30 Fibonacci Function
brilliant professors
Thank you so much for this.
Recursive vs iterative... Lotsa good stuff in this lecture to ponder. Great little bit of history with Fibonacci and rabbits (nicely following the counting pigs & chickens functions!) I was suprised only one student knew what a Fibonacci sequence was... Somehow it made me feel old tho that I know what it means to "grok" something. :P Another great lecture MIT!
I have moved into the problem solving area, I think that will produce the best results. Dont get bored with the tuples,lists,dictionarys cause data manipulation is one of the most important areas of coding. IMHO.
i feel this is the kind of course where the readings are the biggest help and the lectures are more for clarification. i have an intro book by guttag but they don't have the list of readings :/
question i have windows and i cant figure out how he uses two windows, one with the program created and the other activating the typed program using base key words, how do i set up the multi windows?
The way this professor says "again" always gets me LOL But regardless, thank you so much for these lectures!
ahaa! love these lectures
@Channypogosticks I think they are only using Python for demonstration purposes since it's close to writing pseudo code because this is an introduction course. There other courses use a much lower level language.
Unfortunately the problem with 'Spiders' will never work, because the inner loop will finish first (with pigs and chickens) and return before the outside loop has a chance to run. This is due to the number of legs of spiders is an exact multiple of pig's (2 pigs = 1 spider). Spiders will always be 0 (or None).
@mazterarius No, "none" is not a boolean value. Im not sure how you could arrive at this conclusion...there are only two boolean values: "true" and "false". "none" is a special term that indicates that no specific value is being represented. for example, imagine we have "x = none". this essentially makes x a useless value, it specifies that x is not equal to any meaningful value that can be represented by a type (like ints, floats, strings, booleans, etc).
I have a question. In the spider problem set, how is it gonna find all the solutions that could possibly happen? I thought if we find a solution (we get to the line "solutionfound = true"), is it gonna stop there and print out the answer? Someone help me pls, tks!
I just did the second week's assignment for this course (finding nth prime, product of primes) as a python newb. Whew. Great intro to knocking the cobwebs out about Python.
@26:03 Hopefully the farm is not near the body of water that produced the three-eyed fish from "The Simpsons" LOL Thank you guys very much for making this series of videos public! They are wonderful!
very nice lectur
When printing x, after ans*ans!= x, shouldn't x be a perfect square? Why the professor wrote the contrary? Thank you.
for all that were curious fib(36) was 24157817
Why professor uses ' instead of " in programs? Is any differences between quotes type used for strings?
Great lesson thank you teach
Why my print wont work? It goes in purple instead of yellow and always says syntax error at the end of a quote?
They uses the 2.7 version of Python right? I was using the 3.2 and had some problems.
44:18 why do you call the function to checkif everything else in the string is a palindrome?
love the vids. great teacher. my only prob is im finding it very hard to follow the actual computer programming because its very hard to see the screen. still, 10/10
@codygman91 "These people saying that python is turing complete therefore anything is possible are missing a rather important thing. Being turing complete means that you can compute every turing-computable function, not that you can do everything in the environment you're running in that's possible. You would have to have special runtime support for python built in to the system to allow that." -- StackOverflow
@0121ryanh117 is their a big difference between c++ and java?
@Faffel You young'ns have it SO easy. Multics had 256k memory for 64 terminals. Dec writers 30 cps, and ;you had to wait 5 sec for the head to move out of the way. JCF Interdata computers were punch card batch jobs, with hand written instructions on which instructions to hand-toggle in to enable paper tape boot. We had to hand in hand-written problem sets, NO WORD PROCESSING. NO EMAIL AUGH.
What programme is this Prof. using on his Mac.
what ide is being used, pls ?
Go to @2:10 to see the correct screen for @22:23
What is happening at 09:57 ?
In the sqrt(x) function what does the != means?
@07Sudarshan
7 жыл бұрын
not equal to
The main reason i took to these lecs is to learn python. I know C and Java and have been longing to learn python. I am happy now!!!! Python is a lot easier than C, especially its flexibility. That's probably the reason why we are taught C and Java, so that we find other languages easy!!!!
The Fibonacci program is wrong... You must add to 2nd line "or x == 2"
where can i get the answer of assignment?? i want to know the answer of ASSN3
@mitocw
7 жыл бұрын
If there are no solutions included on the OCW site, then, unfortunately, they are not available to OCW users. In some cases, solutions to homework assignments, quizzes, and exams are only discussed and presented in the classroom, and not made available in print or electronic format to the MIT students - or to the worldwide community of visitors to the OCW website. In other cases, the instructors plan to re-use in their MIT classroom the assignments, quizzes, and exams, and so they do not wish to widely publish their solutions.
Does anyone know if he's an old-time LISP hacker?
Question regarding the palindrome function: def isPalindrome(s): if len(s)
@ashutoshpuri4967
2 жыл бұрын
-1 is taking the last place in the new tuple
recursion at 39:33
I just say thank you
Interpreter is it's own p versus np?
I can't understand what he did to unindent the print in palindrome exercise.
26:02 Sir, how do we get the equation 4 * numpig + 2 * numchicken = 56 from the previous equation?
@lemn1sk856
3 жыл бұрын
It's not from the previous equation (They are 2 different linear equations) The first equation is for the total number of heads (both pigs and chickens have one). The second equation is for the total number of legs, since chickens have 2 and pigs have 4.
@thegettokidZz
2 жыл бұрын
so we have 20 heads total ~ h we have 56 legs total ~ totalLegs we have some chickens, and some pigs ~c, p h = c + p total number of heads = chickens + pigs (because each have 1 head) totalLegs = (2 * c) + (4 * p) total legs = chickens * 2 + pigs * 2 (because chickens have 2 legs and pigs have 4) the number of heads, 20 and the number of legs, 56 are arbitrary values given or passed into the equation. They could be anything, but the equations themselves will remain constant regardless of those numbers.
I've been watching it on my Roku until now...finally I'm counted within the 10%
@Channypogosticks I don't know what makes you think python is useless. But to me, it makes my life easy. I program with python almost everyday.
Does anyone use the Visual Logic program?
I was going along fine but this lecture tripped me up. Pigs and Chickens--how hard could that be?-- well, I don't get it. could someone explain how and why this code was structured? and/or do I need to understand these mechanics to move forward?...
import string from random import * characters = string.ascii_letters+string.digits password = "".join(choice(characters) for x in range(randint(8,10))) myFile = open("password.txt","w") myFile.write(password) myFile.close() print(password) the code above will create an 8-10 digit password, and create a word doc with it on (paste code into python gui)
@biukucanoe yea we do... java has become so convenient
"no mutant spiders here" - LMFAO this guy is hilarious.
@94D33M
5 жыл бұрын
what ? where ?
@affirmonego5202
4 жыл бұрын
@@94D33M 37:25
how to restart ? 50:41
If anyone else is wondering how to apply the farm problem in C++: #include using namespace std; main() { int Heads = 20; int Legs = 56; int Chickens; int SumLegs; for(int Spiders = 0;Spiders
i got the barnYard to work in java public class first { public static void main(String[] args) { int numPigs = 0,numChickens = 0,numHeads = 20,totLegs = 56,numLegs = 0; for ( int i = 0; i System.out.println("chicken check "+numChickens); numPigs=numHeads - numChickens ; numLegs=4*numPigs+2*numChickens; System.out.println(numLegs); numChickens++; if (numLegs==totLegs){ i=56; System.out.println("there are "+numPigs+" pigs and "+numChickens); }}}
Thats because the problem is irrevelant, he is teaching brute force algorithm and how do you use loop in loops. You can see at the next stage he doesn't even care about the accuracy of the result of the problem when he adds spiders to it. Brute force should be the last resort, but its also the easiest one to understand. Thats why he starts from it.
39:25 recursion
@Juefawn != it means if its not equal.
Dear sir, you look like Mr. Narendra Modi. Anyways, thank you MIT for this lovely content.
his jokes may be bad(but at least he has a sense of humor) but it does get you in the right mind frame and provide as excellent examples.
just check on ocw.mit.edu
@loko95ftp i think he means Square root when he says that
Back in my day we didn't have all these fancy windows and mice pointers. We progammed in ones and zeros, and sometimes we didn't even have ones.
@voyagers5026
2 жыл бұрын
it's great how the vedio is 10 years old and then here your are even older and telling us things
@biukucanoe You sound like a nerdy version of Grandpa Simpson, senor. These Opencourse lectures are great. Thanks MIT...
holy shit class 4 and at functions?!
where can i get the notes for this
@mitocw
Жыл бұрын
The course materials are available on MIT OpenCourseWare at: ocw.mit.edu/6-00F08. We also recommend you look at the 2011 version of this course. It has more supporting materials: ocw.mit.edu/courses/6-00sc-introduction-to-computer-science-and-programming-spring-2011/. * Please note this course is for the earlier version of Python 2.5.x (which is still used in some places). If you would like a course using Python 3.5.x, see 6.0001: ocw.mit.edu/courses/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/. Best wishes on your studies!
I too have read this book. I too, grok, mister martian.
I've the same question. When you know the answer, tell it, please.
Who’s here in 2020?
@LateNightPerson
3 жыл бұрын
Me!
@nutsberserk1
3 жыл бұрын
clearly you
I think i'm gonna have to watch this one multiple times
why does he keep saying ''squirt'' i thought it was ""square"
@Channypogosticks no. it's named after a snake.
I downloaded the whole lot 20+ vids onto iTunes from iTunes University. Very convenient, and all still free. On iTunes University you can search by course name and all the vids are there for you to download.
It's Python, either can be used. Generally personal preference.
Im here, and I hardly understand how most of what he says or shows. Hopefully it gets easier to understand.
@Geebsee this class is easy.. no supriszing 90% drop out
Watch out, we got a badass over here @ThePrivateJoker
The fibonacci function is wrong. The seed values ar 0 and 1, not 1 and 1. This mistake results in the values being offset by +1. At x=12 the result should be 144. 233 is the result for x=13. The correct function would therefor be: def fib(x): """Return fibonacci of x, where x is a non-negative int""" if x == 0: return 0 elif x == 1: return 1 else: return fib(x-1) + fib(x-2)
@SpielenwirLP
8 жыл бұрын
SpielenWir A better and way to calculate fibonacci would be this: fibmem = {0:0,1:1} def fib(x): """Return fibonacci of x, where x is a non-negative int""" if not x in fibmem: fibmem[x] = fib(x-1) + fib(x-2) return fibmem[x] This way, values that where already calculated, are saved in the dictionary "fibmem" and won't have to be calculated again. This speeds up the process immensly and even the calculation of fib(500) just takes a split-second. This way it is possible to calculate fibonacci way beyond x=5000 in miliseconds, but you would have to split the calculations into multiple runs of fib(), to work around the "maximum recursion depth"-error. For example if you try fib(1500) you get: "RuntimeError: maximum recursion depth exceeded" But if you run: fib(500) fib(1000) fib(1500) after one another you get the correct end result, which is: 13551125668563101951636936867148408377786010712418497242133543153221487310873528750612259354035717265300373778814347320257699257082356550045349914102924249595997483982228699287527241931811325095099642447621242200209254439920196960465321438498305345893378932585393381539093549479296194800838145996187122583354898000 If you're into that kind of thing, that is...
@SpielenwirLP
8 жыл бұрын
SpielenWir You could automate the runs of fib(), by spliting x into fractions. For example: u = int(input("Which fibonacci number do you want to calculate? ")) #ask for userinput if u>=500 and u=500 AND 0: #Until this loop repeated 10 times z=z+y #add a 10th of the user input to z fib(z) #and run it through fib() count=count-1 #Offset the counter by -1 print(fib(u)) #Until it has reached 0 and output the fibonacci the user entered elif u>=9000: #This is the same as above, y=int(u/100) #but it splits the input into 100 fractions count=100 #for values above 9000. z=0 while count > 0: #You could repeat this for even higher values z=z+y #Split into 1000 fractions for values below 90000 for example fib(z) count=count-1 print(fib(u)) else: print(fib(u)) #If the value is below 500 it is not necessary to split it, so we just output fib(u) You should refrain from splitting the value to if it's not necessary though, because multiple runs of fib() take longer to calculate. If you would split the calculation for fib(9000) into 1000, instead of 100, the time it takes to calculate increases noticably. I could calculate up to fib(207000), with python using almost exactly 2GB of RAM to store this number. It took about 22 seconds. This is where the calculating-fun ends for me though, because I have the 32-bit version of Python installed.
@ashennell
8 жыл бұрын
+SpielenWir Both 0 and 1 are valid as inital values of the fibonacci sequence.
@percih70
6 жыл бұрын
thank you
@loko95ftp because the code for it is "sqrt" .. almost spells squirt lol... its a lot easier to say too, I guess. =P
I read somewhere that only 1 out of every two hundred computer science majors still dont understand programing
9:28, "In this case, squirt,..." ........wait, what? LOL I'm taking notes, and enjoying this a lot. ty for the great vids
@igorseveric310
4 жыл бұрын
davidadrm D I am doing the same thing here! 😁
@MaxRoudriges Almost all in college studying something technical know about Fibonacci I suppose
I have a problem. I wrote the solve function completely correctly, exactly like he did, but I keep getting values of [None, None] returned so the barnYard function always comes back with 'There is no solution'. I have everything done completely correctly just like he has it and I'm using 2.7. Why is this happening? Edit: If go into the solve function and I take the "return[None, None]" and delete it all together, it works, but I still don't fully understand why. I feel like even though the if statement still tells it to return values for the numPigs and numChicks if it finds a solution, the function is still going to return [None, None] anyway after it goes through the entire range and ends the loop. So I have two questions. What is the point of this? and why does this example work for him and not me? If somebody could help that would be awesome. Thanks in advance.
@Drewzdev
10 жыл бұрын
32:39 and he actually says "What is the point of this?" 50:39 rhetorically at the end.
@collin.wesley
10 жыл бұрын
1. The point of including return(None,None) in this particular example is to weed out inputs that aren't possible. For example, if you were to input 16 heads and 100 legs, this is impossible due to the fact that even if the animal with the most legs, the pig, were to take up all 16 heads, the number of legs would only amount to 64(less than the given total of legs). Thus it would return (None,None) which then results in "There is no solution" printing to the user. 2. I had the same problem you had at first, however I realized that it was a result of a misplaced indentation. Here is my first code: def solve(numLegs, numHeads): for numChicks in range(0,numHeads+1): numPigs=numHeads-numChicks totLegs=4*numPigs+2*numChicks if totLegs==numLegs: return(numPigs, numChicks) return(None,None) Here is my successful code: def solve(numLegs, numHeads): for numChicks in range(0,numHeads+1): numPigs=numHeads-numChicks totLegs=4*numPigs+2*numChicks if totLegs==numLegs: return(numPigs, numChicks) return(None,None) As you can see, my first code had an extra indentation before return(None,None) due to the interpreter's automatic indentation feature I was using to write the code. Because of this, I would check to make sure you do not have this same indentation. The reason my first code doesn't work but my other code does is because python is a language that renders one line at a time going top to bottom. So, when the return(None,None) command is in the location from my first code, the compiler renders the entire block of code which results in it producing "(None,None)" almost 100% of the time. However, when the indentation problem is fixed, the compiler reads everything up to return(numPigs, numChicks) and if something is indeed returned, it skips the last return statement and moves on to the barnYard() function. -Hope I helped anyone who's confused!
@salihsenses6153
6 жыл бұрын
CollinSpeight you saved my day
at 26:00 funny how only at MIT would they think that's a grammar school problem. I learned how to do that in the 9th grade lmao
@thegettokidZz
2 жыл бұрын
grammar school?
@Channypogosticks Python useless? Haven't you been listening to the lectures? You can program anything in a turing complete language. Python is turing complete and more.
👏👏👍
great way, but the Fibonacci number is not accurate, it computed the Fibonacci of 13 instead 1, this because the Fib of 0 not 1. However, the idea of recursion is clear, Thanks.
I get that he was using it to teach a concept, but wouldn't it be easier to use something like this to check if it's a palindrome? word = input() if word == word[::-1] : print("Word is a palindrome") else: print("Word is not a palindrome")
@thegettokidZz
2 жыл бұрын
maybe it's because he didn't go over the process of steps when using slices. it's a process we haven't learned yet in this course and there's probably a reason he didn't mention it at this point in his lectures. Why don't you go teach at MIT smartass?
I can see now the difference between an MIT student and someone who ISNT an MIT student... What takes me months (or I should say about a month) to catch on to something takes them weeks. Interesting... If only I grew up with a little bit more discipline.