2012-03-21

Re: #5 Practice Midterm.

You should have a sentence or two explaining what this code does.

Best,
Chris
You should have a sentence or two explaining what this code does.<br><br>Best,<br>Chris

-- #5 Practice Midterm
Originally Posted By: masonw
added sentences describing the above code
'''Originally Posted By: masonw''' added sentences describing the above code

-- #5 Practice Midterm
Originally Posted By: stilsons
Hi,

I believe that the line c=f() should be all the way over to the left, with no indention. You have it indented, which puts it inside the function. The function can't call itself.

Thank you.

Steve Stilson SCJA
'''Originally Posted By: stilsons''' Hi,<br><br>I believe that the line c=f() should be all the way over to the left, with no indention. You have it indented, which puts it inside the function. The function can't call itself.<br><br>Thank you.<br><br>Steve Stilson SCJA

#5 Practice Midterm
Originally Posted By: masonw
Generators: Generators are used to generate sequences one element at a time using the yield construct. The next() method runs to the next yield statement. The following function, f(), uses a generator to give the current iteration of the for loop. "print c.next()" runs through another iteration and prints out the number of the iteration.

def f():
....for i in range(10):
........yield i
c = f()
print c.next()
_____________________________________________________________________

Coroutine: A coroutine receives a sequence of values one at a time and computes something. The following code uses a coroutine function to attempt to match the text passed as the function parameter to text that is sent to the coroutine function using the send() function. We create a print_match instance p with the text "hello". "p.next()" runs up to and including the "line = (yield)" statement. "p.send("some words here")" sends the line to the coroutine. If any of this line of text matches with the text passed to the function when it was declared, then the function prints the line. If not, then nothing is printed.

def print_matches(text):
....print "Trying to find", text
....while True:
.........line = (yield)
.........if text in line:
.............print line
p = print_matches("hello")
p.next()
p.send("no") #doesn't print anything
p.send("hello there world") #prints hello there world
_____________________________________________________________________

Lambda: Lambda is used to create anonymous functions which are often used to write "one-off" functions like callback handlers in GUI's and can be used to test features without defining a function. The following function adds 3 to the number passed as a parameter

f = lambda x: x + 3
f(4)


by: Mason Winner, Thomas Nystrand, Kristian Andreasen
'''Originally Posted By: masonw''' Generators: Generators are used to generate sequences one element at a time using the yield construct. The next() method runs to the next yield statement. The following function, f(), uses a generator to give the current iteration of the for loop. &quot;print c.next()&quot; runs through another iteration and prints out the number of the iteration.<br><br>def f():<br>....for i in range(10):<br>........yield i<br>c = f()<br>print c.next()<br>_____________________________________________________________________<br><br>Coroutine: A coroutine receives a sequence of values one at a time and computes something. The following code uses a coroutine function to attempt to match the text passed as the function parameter to text that is sent to the coroutine function using the send() function. We create a print_match instance p with the text &quot;hello&quot;. &quot;p.next()&quot; runs up to and including the &quot;line = (yield)&quot; statement. &quot;p.send(&quot;some words here&quot;)&quot; sends the line to the coroutine. If any of this line of text matches with the text passed to the function when it was declared, then the function prints the line. If not, then nothing is printed.<br><br>def print_matches(text):<br>....print &quot;Trying to find&quot;, text<br>....while True:<br>.........line = (yield)<br>.........if text in line:<br> .............print line<br>p = print_matches(&quot;hello&quot;)<br>p.next()<br>p.send(&quot;no&quot;) #doesn't print anything<br>p.send(&quot;hello there world&quot;) #prints hello there world<br>_____________________________________________________________________<br><br>Lambda: Lambda is used to create anonymous functions which are often used to write &quot;one-off&quot; functions like callback handlers in GUI's and can be used to test features without defining a function. The following function adds 3 to the number passed as a parameter<br><br>f = lambda x: x + 3<br>f(4)<br><br><br>by: Mason Winner, Thomas Nystrand, Kristian Andreasen

-- #5 Practice Midterm
Originally Posted By: masonw
stilsons wrote:
Hi,

I believe that the line c=f() should be all the way over to the left, with no indention. You have it indented, which puts it inside the function. The function can't call itself.

Thank you.

Steve Stilson SCJA


Thanks, I've corrected that.
'''Originally Posted By: masonw''' stilsons wrote:<br>Hi,<br><br>I believe that the line c=f() should be all the way over to the left, with no indention. You have it indented, which puts it inside the function. The function can't call itself.<br><br>Thank you.<br><br>Steve Stilson SCJA<br><br><br>Thanks, I've corrected that.
X