print vs. return
- What is the difference between print and return?
- What value does a function return when the function ONLY has a print statement and no return statement??
- If a method does not execute a return statement, what value does it return?
- What do each of the following functions do when called
on the list [1,2,3,4,5]?
def f1(list):
for x in list:
if (x % 2 == 1):
print x
|
def f2(list)
for x in list:
if (x % 2 == 1):
return x
|
- What values do f1 and f2 return for the input [2,4,6,8]?
- Fill in the blanks:
- When we want to go through a list to search for an element that
passes a test, and stop searching as soon as we have found it, we
should use _________ to exit the loop and deliver the element as the
result of the function.
- When we want to go through a list and display all the elements that
pass a test, we should use _________ to display each element.
- The _________ statement always produces output on the console.
- The _________ statement specifies the result of the function
containing it. That result may or may not be displayed on the console,
depending on the context in which the function is being called.
For loop vs While loop
- What is the difference between for loop and while loop?
- Write a function that takes a positive integer n as an argument and add up all the positive numbers upto n
using a for loop. The function should return the sum.
- Write a function that takes a positive integer n as an argument and adds up all the positive numbers upto n
using a while loop. The function should return the sum.
- Write a function that a takes a list of strings as an argument and prints all the words starting with the
letter "a" using a for loop. If s is a string you can use s[i] to access the symbol at index i.
- Write a function that a takes a list of strings as an argument and prints all the words starting with the
letter "a" using a while loop.
- Write a function that a takes a list of strings as an argument and counts the number of words starting with the
letter "a" using a while or a for loop.
Recursion
- What is the base case? This question asks how will we
recognize an input for which we should not make a recursive call? Is
it zero? The empty list? A non-list value? Pick the simplest base
case you can. Often, for list problems, this will be the empty list,
but that's not always the case.
- How can I derive a smaller problem from this one? For
numbers, this often means subtracting one from the input. For flat
(non-nested) lists, it may mean removing the first element from the
input and recursing on what's left. For nested lists (e.g., trees) it
may mean recursing on the first element of the input, and then perhaps
recursing again on everything except the first element.
- How can I make progress with each recursive call? Making
progress might mean doing something with the input, or with the result
you got back from the recursive call, or both.
Let's try an example: write a recursive function f(n) to
print n rows of one asterisk, and return
nothing. Example:
>> f(8)
*
*
*
*
*
*
*
*
>>
Let's work through our three questions:
-
What should the base case be? ___________
- Given an input n, how can you make a smaller
problem from this one? _________________
- How can you make progress with each recursive call? Think about
it this way: if your job is to print eight asterisks, and you can get
somebody else to print the last seven of them, then what do you need
to do yourself? __________________
Here is Python code to solve the problem. For each of the three
questions above, write that number on the line that implements your
answer to that question.
def print_asterisks(n):
if (n == 0): _____
pass
else:
print ("*") _____
print_asterisks(n-1) _____
Recursion on Lists
- Write a recursive function to print out each element of a
list on a line by itself, in reverse order (the last element is
printed first). Hint: do the recursive call first, then print the
first element.
- Write a recursive function to add up all the elements of a
list of numbers.
|